-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBlueskyApiClient.Actor.cs
More file actions
108 lines (94 loc) · 3.12 KB
/
Copy pathBlueskyApiClient.Actor.cs
File metadata and controls
108 lines (94 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Bluesky.NET.Constants;
using Bluesky.NET.Models;
using FluentResults;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Bluesky.NET.ApiClients;
partial class BlueskyApiClient
{
public async Task<Author?> GetAuthorAsync(string accessToken, string identifier)
{
var timelineUrl = $"{_baseUrl}/{UrlConstants.ProfilePath}?actor={identifier}";
HttpRequestMessage message = new(HttpMethod.Get, timelineUrl);
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
try
{
var httpResponse = await _httpClient.SendAsync(message);
if (httpResponse.IsSuccessStatusCode)
{
using Stream contentStream = await httpResponse.Content.ReadAsStreamAsync();
Author? response = JsonSerializer.Deserialize(contentStream, ModelSerializerContext.CaseInsensitive.Author);
return response ?? null;
}
}
catch (Exception e)
{
Debug.WriteLine(e);
throw;
}
return null;
}
/// <inheritdoc/>
public async Task<Result<FeedResponse>> GetSuggestedPeopleAsync(
string accessToken,
CancellationToken ct,
int count = 10,
string? cursor = null)
{
if (count < 1)
{
count = 1;
}
else if (count > 100)
{
count = 100;
}
var url = $"{_baseUrl}/{UrlConstants.SuggestedPeoplePath}?limit={count}";
if (cursor is { Length: > 0 })
{
url += $"&cursor={cursor}";
}
HttpRequestMessage message = new(HttpMethod.Get, url);
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return await SendMessageAsync(
message,
ModelSerializerContext.CaseInsensitive.FeedResponse,
ct);
}
/// <inheritdoc/>
public async Task<bool> FollowActorAsync(
string accessToken,
string userHandle,
string subjectDid,
CancellationToken ct)
{
var url = $"{_baseUrl}/{UrlConstants.CreateRecordPath}";
FollowRecordBody body = new()
{
Repo = userHandle,
Record = new FollowRecord
{
CreatedAt = DateTime.Now,
Subject = subjectDid
},
Collection = RecordType.Follow.ToStringType()
};
HttpRequestMessage message = new(HttpMethod.Post, url)
{
Content = new StringContent(JsonSerializer.Serialize(body, ModelSerializerContext.CaseInsensitive.FollowRecordBody), Encoding.UTF8, "application/json"),
};
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var result = await SendMessageAsync(
message,
ModelSerializerContext.CaseInsensitive.CreateRecordResponse,
ct);
return result.IsSuccess;
}
}