|
1 | | -# <a name="gettingStarted">Hello World</a> |
| 1 | +# Get started |
2 | 2 |
|
3 | | -```c# |
4 | | -using idunno.Bluesky. |
| 3 | +Let's make your first post to Bluesky via the API in under 5 minutes. |
5 | 4 |
|
6 | | -using (BlueskyAgent agent = new ()) |
7 | | -{ |
8 | | - var loginResult = await agent.Login(username, password); |
9 | | - if (loginResult.Succeeded) |
10 | | - { |
11 | | - var response = |
12 | | - await agent.Post("Hello World"); |
| 5 | +## Create a .NET project and add the idunno.Bluesky |
13 | 6 |
|
14 | | - if (response.Succeeded) |
15 | | - { |
16 | | - // It worked. |
17 | | - } |
18 | | - } |
19 | | -} |
20 | | -``` |
| 7 | +# [Command Line](#tab/commandlineProjectCreate) |
21 | 8 |
|
22 | | -## <a name="makingRequests">Making requests to Bluesky</a> |
| 9 | +1. At the command line run the following commands |
| 10 | + ```PowerShell |
| 11 | + dotnet new console -n HelloBluesky |
| 12 | + cd HellowBluesky |
| 13 | + dotnet add package idunno.Bluesky --prerelease |
| 14 | + ``` |
23 | 15 |
|
24 | | -All the supported Bluesky operations are contained in the `BlueskyAgent` class, which takes care of session management for you. |
| 16 | +# [Visual Studio](#tab/visualStudioProjectCreate) |
25 | 17 |
|
26 | | -Most requests to Bluesky are made over [HTTP](https://docs.bsky.app/docs/category/http-reference) and the results are wrapped up in an `HttpResult` instance, |
27 | | -which contains the HTTP status code returned by the API, and any result or error returned. |
| 18 | +1. Create a new .NET Command Line project by opening the File menu, and choosing **New ▶ Project**. |
| 19 | +1. In the "**Create a new project**" dialog select C# as the language, choose **Console App** as the project type then click Next. |
| 20 | +  |
| 21 | +1. In the "**Configure your new project**" dialog name the project `HelloBluesky` and click Next. |
| 22 | +1. In the "**Additional information**" dialog choose a Framework of .NET 8.0, uncheck the "Do not use top level statements" check box then click **Create**. |
| 23 | +  |
| 24 | +1. Under the **Project** menu Select **Manage nuget packages**, select the *Browse* tab, ensure that the Include prelease checkbox is checked. Search for `idunno.Bluesky`, and click **Install**. |
| 25 | +1. Close the **Manage nuget packages** dialog. |
28 | 26 |
|
29 | | -## <a name="understandingResults">Understanding AtProtoHttpResult<T></a> |
| 27 | +# [Visual Studio Code](#tab/vsCodeProjectCreate) |
30 | 28 |
|
31 | | -Every API call through an agent returns an `AtProtoHttpResult<T>`. This approach, which you may recognize from ASP.NET, |
32 | | -avoids the use of exceptions should the HTTP call fail, and allows you to view any extra error information the Bluesky APIs may return. |
| 29 | +First configure VS Code to [allow pre-release nuget packages](https://code.visualstudio.com/docs/csharp/package-management#_include-prerelease-package-versions). |
33 | 30 |
|
34 | | -If a request is successful the API return value will be in the `Result` property of the `AtProtoHttpResult<T>` and |
35 | | -the `Succeeded` property will be `true`. The `StatusCode` property will be `HttpStatusCode.OK`. |
| 31 | +1. Create a new .NET Command Line project by opening the Command Palette (**Ctrl + Shift + P**) and search for **.NET New Project** |
| 32 | +1. In the Create a new .NET Project template search for and select **Console App** |
| 33 | +1. Select the folder you want to save your poject in |
| 34 | +1. Name your project `HelloBlusky` |
| 35 | +1. Choose the solution format you prefer. |
| 36 | +1. Press *Enter* to create the solution. |
| 37 | +1. Select the HelloWorld.csproj file in Explorer window. |
| 38 | +1. Opening the Command Palette (Ctrl + Shift + P) and search for **Nuget: Add** |
| 39 | +1. Enter `idunno.Bluesky` in the package search dialog and choose the latest version. |
36 | 40 |
|
37 | | -If a request has failed, either at the HTTP or the API layer then the `Succeeded` property will be `false`, and |
38 | | -the `Result` property will likely be `null`. You can check the `StatusCode` property to see what HTTP status code was |
39 | | -encountered during the API call, and, if the API call reached the API endpoint the `Error` property will contain any |
40 | | -error message returned by the endpoint. |
| 41 | +--- |
41 | 42 |
|
42 | | -For example, a login call returns an `AtProtoHttpResult<bool>`. To check the operation succeeded you would |
| 43 | +### Create a session |
43 | 44 |
|
44 | | -1. Check the that the `Succeeded` property is true, which indicates the underlying request returned a `HttpStatusCode.OK` status code, and an available result. |
45 | | -2. If `Succeeded` is `true`, you can use the `Result` property and continue on your way. |
| 45 | +# [Command Line](#tab/commandlineSessionCreate) |
46 | 46 |
|
47 | | - If `Succeeded` is `false` you use the `StatusCode` property to examine the HTTP status code returned by the API, then |
48 | | - 1. If the `StatusCode` property is `HttpStatusCode.OK` then the API call succeeded but no result was returned. |
49 | | - 2. If the `Error` property to view any extended error information returned by the API, which may have an `Error` and a `Message` set. |
| 47 | +1. Open the `Program.cs` file in your editor of choice and change its contents to the following code, replacing |
| 48 | +the `"handle"` and `"password"` parameters in the `agent.Login()` call with your Bluesky handle and password. |
| 49 | + [!code-csharp[](docs/code/createASession.cs?highlight=4)] |
| 50 | +2. Save the changed file. |
50 | 51 |
|
51 | | -When calling an API you use the following pattern to check for errors. |
| 52 | +# [Visual Studio](#tab/visualStudioSessionCreate) |
52 | 53 |
|
53 | | -```c# |
54 | | -// Make an API call to get the timeline for the current user. |
55 | | -var result = await agent.GetTimeline(); |
| 54 | +1. Open the `Program.cs` file from the Solution Explorer window and change its contents to the following code, replacing |
| 55 | +the `"handle"` and `"password"` parameters for in the `agent.Login()` call with your Bluesky handle and password. |
| 56 | + [!code-csharp[](docs/code/createASession.cs?highlight=4)] |
| 57 | +2. Save the changed file. |
56 | 58 |
|
57 | | -if (result.Succeeded) |
58 | | -{ |
59 | | - // Everything was successful, continue on with your code |
60 | | -} |
61 | | -else |
62 | | -{ |
63 | | - // React to the error. |
64 | | - // Check the StatusCode property to check if there were any HTTP errors |
65 | | - // And the Error property to check if the API returned any errors. |
66 | | -} |
67 | | -``` |
| 59 | +# [Visual Studio Code](#tab/vsCodeSessionCreate) |
68 | 60 |
|
69 | | -The `EnsureSucceeded()` method on `AtProtoHttpResult<T>` will throw an `AtProtoHttpRequestException` if the `Succeeded` property is false. |
| 61 | +1. Open the `Program.cs` file from the Explorer window and change its contents to the following code, replacing |
| 62 | +the `"handle"` and `"password"` parameters for the `agent.Login()` method with your Bluesky handle and password. |
| 63 | + [!code-csharp[](docs/code/createASession.cs?highlight=4)] |
| 64 | +2. Save the changed file. |
| 65 | + |
| 66 | +___ |
| 67 | + |
| 68 | +> [!TIP] |
| 69 | +> You can create and use an [App password](https://bsky.app/settings/app-passwords) instead of your login password. |
| 70 | +> |
| 71 | +> App Passwords are safer as they allow sign in without granting full access to your Bluesky account. |
| 72 | +
|
| 73 | +### Create a post |
| 74 | + |
| 75 | +# [Command Line](#tab/commandlinePostCreate) |
| 76 | + |
| 77 | +1. Continue to change `Program.cs` by adding an additional call to make a post. |
| 78 | + [!code-csharp[](docs/code/helloWorld.cs?highlight=5)] |
| 79 | +1. Save the changed file and exit your editor. |
| 80 | +1. Compile and run your project with the following command |
| 81 | + ```PowerShell |
| 82 | + dotnet run |
| 83 | + ``` |
| 84 | + |
| 85 | +# [Visual Studio](#tab/visualStudioPostCreate) |
| 86 | +1. Continue to change `Program.cs` by adding an additional call to make a post. |
| 87 | + [!code-csharp[](docs/code/helloWorld.cs?highlight=5)] |
| 88 | +1. Save the changed file. |
| 89 | +1. Run the project by pressing **F5** or choosing **Start Debugging** under the Debug menu. |
| 90 | + |
| 91 | +# [Visual Studio Code](#tab/vsCodePostCreate) |
| 92 | +1. Continue to change `Program.cs` by addint an additional call to make a post. |
| 93 | + [!code-csharp[](docs/code/helloWorld.cs?highlight=5)] |
| 94 | +1. Save the changed file. |
| 95 | +1. Run the project by pressing **F5** or choosing **Start Debugging** under the Run menu. |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +The program should run without any errors and if you check your own profile (click the Profile link in the app, or on [bsky.app](https://bsky.app/)) |
| 100 | +you should see a post that says "Hello World from idunno.Bluesky". |
| 101 | + |
| 102 | +Congratulations, you've just posted from code! |
| 103 | + |
| 104 | +You can @ someone in the post text, add hashtags, or http/https links and they will all get turned into the right type of link. Try it and check in the app. |
| 105 | + |
| 106 | +### Next Steps |
| 107 | + |
| 108 | +* [Understand how to make API calls and check their results and handle errors](docs/requestsAndResponses.md) |
| 109 | +* [Make richer posts](docs/posting.md) |
| 110 | +* [Read your timeline](docs/timeline.md) |
70 | 111 |
|
0 commit comments