Skip to content

Commit caf3940

Browse files
committed
Refresh getting started docs
Add verification preferences Comment out proxy hardcoding in various samples.
1 parent 684d2ab commit caf3940

18 files changed

+228
-64
lines changed

docs/docfx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
{
3030
"files": [
3131
"**/images/**",
32-
"**/media/**"
32+
"**/media/**",
33+
"**/code/*.cs"
3334
]
3435
},
3536
{

docs/docs/code/createASession.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using idunno.Bluesky;
2+
3+
using BlueskyAgent agent = new();
4+
var loginResult = await agent.Login(handle, password);

docs/docs/code/helloWorld.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using idunno.Bluesky;
2+
3+
using BlueskyAgent agent = new();
4+
var loginResult = await agent.Login(handle, password);
5+
await agent.Post("Hello World from idunno.Bluesky");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using idunno.AtProto;
2+
using idunno.AtProto.Repo;
3+
using idunno.Bluesky;
4+
5+
using BlueskyAgent agent = new();
6+
7+
AtProtoHttpResult<bool> loginResult = await agent.Login("handle", "password");
8+
9+
if (loginResult.Succeeded)
10+
{
11+
AtProtoHttpResult<CreateRecordResult> postResult = await agent.Post("Hello World from idunno.Bluesky");
12+
13+
if (!postResult.Succeeded)
14+
{
15+
Console.WriteLine($"Post failed with HTTP Status Code {postResult.StatusCode}");
16+
}
17+
}
18+
else
19+
{
20+
Console.WriteLine($"Login failed with HTTP Status Code {loginResult.StatusCode}");
21+
if (loginResult.AtErrorDetail is not null)
22+
{
23+
Console.Write($"API Error {loginResult.AtErrorDetail.Error} {loginResult.AtErrorDetail.Message}")
24+
}
25+
}

docs/docs/connecting.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
## <a name="usernamesAndPasswords">Authenticating with handles and passwords</a>
44

5-
As you can see from the [Hello World](../index.md#gettingStarted) example connecting to Bluesky consists of creating an instance of a `BlueskyAgent`
5+
As you can see from the [Hello World](../index.md) example connecting to Bluesky consists of creating an instance of a `BlueskyAgent`
66
and then calling the login method.
77

88
```c#
99
using (BlueskyAgent agent = new ())
1010
{
11-
var loginResult = await agent.Login(handles, password);
12-
if (loginResult.Succeeded && agent.Session is not null)
11+
var loginResult = await agent.Login(handle, password);
12+
if (loginResult.Succeeded)
1313
{
1414
// Do your Bluesky thing
1515
}
24.5 KB
Loading
49.5 KB
Loading

docs/docs/requestsAndResponses.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# <a name="makingRequests">Making requests to Bluesky</a>
2+
3+
Making requests to Bluesky is done though the `BlueskyAgent` class. The `BlueskyAgent` class takes care of session management for you, once
4+
you authenticate using `agent.Login()` or via OAuth the tokens necessary to make authenticated requests are stored, refreshed automatically
5+
and added to any authenticated API requests.
6+
7+
## <a name="understandingResults">Understanding responses from Bluesky</a>
8+
9+
Almost every API call through an agent returns an `AtProtoHttpResult<T>`. This approach, which you may recognize from ASP.NET,
10+
avoids the use of exceptions should the HTTP call fail, and allows you to view any extra error information the Bluesky APIs may return.
11+
12+
`AtProtoHttpResult<T>` includes properties to help you determine the success or failure of the call. These include
13+
14+
* The `Succeeded` property, a `boolean` indicated whether the API call was sucessful or not,
15+
* The `StatusCode` property containing the HTTP status code from the API call,
16+
* The `Result` property, containing the result of the API call. This may be null if a call was unsucessful,
17+
* The `AtErrorDetail` property, containing any detailed error messages from the API if any were returned.
18+
19+
If a request is **successful** the `Succeeded` property on the returned result will be `true`, the `Result` property will not be null, and
20+
the `StatusCode` property will be `HttpStatusCode.OK`.
21+
22+
If a request has **failed**, either at the HTTP or the API layer then the `Succeeded` property on the returned result will be `false`, and
23+
the `Result` property will likely be `null`. The `StatusCode` property will contain the HTTP status code that returned by API call, and,
24+
if the API call reached the API endpoint the `Error` property will probably contain any error message returned by the endpoint.
25+
26+
For example, a login call returns an `AtProtoHttpResult<bool>`. To check the login succeeded you would
27+
28+
1. Check the that the `Succeeded` property is true, which indicates the underlying request returned a `HttpStatusCode.OK` status code, and an available result.
29+
2. If `Succeeded` is `true` you can continue on your way (the actual result from a call to `Login()` is handled by the agent class)
30+
31+
If `Succeeded` is `false` you use the `StatusCode` property to examine the HTTP status code returned by the API, then
32+
1. If the `StatusCode` property is `HttpStatusCode.OK` then the API call succeeded but no result was returned.
33+
2. If the `Error` property to view any extended error information returned by the API, which may have an `Error` and a `Message` set.
34+
35+
Let's add error checking to the Hello World code you wrote in [getting started.](../index.md)
36+
37+
[!code-csharp[](./code/helloWorldErrorChecked.cs?highlight=9,13-16,19-25)]
38+
39+
* Line 9 you can see a call to `loginResult.Succeeded`, ensuring that any attempt to post only happens if the call to `Login` was successful,
40+
* Lines 13-16 show a reaction if the call to `Post()` failed, and
41+
* Lines 19-25 show how you can check for any API errors that might be returned.
42+
43+
You can download [helloWorldErrorChecked.cs](./code/helloWorldErrorChecked.cs).
44+
45+
> [!TIP]
46+
> `AtProtoHttpResult<T>` has an `EnsureSucceeded()` method which will throw an `AtProtoHttpRequestException` if the `Succeeded` property is false.
47+
> This can be used while testing code, or even during development before you implement error handling.

docs/docs/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ items:
33
items:
44
- name: Getting Started
55
href: ../index.md
6-
- name: Common Terms & Concepts
6+
- name: Requests, responses and errors
7+
href: requestsAndResponses.md
8+
- name: Common Terms and Concepts
79
href: commonTerms.md
810
- name: Connecting to Bluesky
911
href: connecting.md

docs/index.md

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,111 @@
1-
# <a name="gettingStarted">Hello World</a>
1+
# Get started
22

3-
```c#
4-
using idunno.Bluesky.
3+
Let's make your first post to Bluesky via the API in under 5 minutes.
54

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
136

14-
if (response.Succeeded)
15-
{
16-
// It worked.
17-
}
18-
}
19-
}
20-
```
7+
# [Command Line](#tab/commandlineProjectCreate)
218

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+
```
2315

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)
2517

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+
![The Create a new project dialog in Visual Studio 2022, with C# selected as the language and Console App project type highlighted.](docs/media/vs-CreateNewConsoleApp.png)
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+
![The Additional information dialog in Visual Studio 2022, with .NET 8.0 selected as the framework and Do not use top level statements unchecked.](docs/media/vs-CreateNewConsoleApp.png)
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.
2826

29-
## <a name="understandingResults">Understanding AtProtoHttpResult&lt;T&gt;</a>
27+
# [Visual Studio Code](#tab/vsCodeProjectCreate)
3028

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).
3330

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.
3640

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+
---
4142

42-
For example, a login call returns an `AtProtoHttpResult<bool>`. To check the operation succeeded you would
43+
### Create a session
4344

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)
4646

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.
5051

51-
When calling an API you use the following pattern to check for errors.
52+
# [Visual Studio](#tab/visualStudioSessionCreate)
5253

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.
5658

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)
6860

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)
70111

0 commit comments

Comments
 (0)