-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathActivityPubClient.cs
133 lines (106 loc) · 4.4 KB
/
ActivityPubClient.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
using System.Net.Http.Headers;
using System.Text.Json;
using ActivityPub.Common.Util;
using ActivityPub.Types.AS;
using ActivityPub.Types.Conversion;
using ActivityPub.Types.Util;
namespace ActivityPub.Client;
/// <summary>
/// Default implementation of <see cref="IActivityPubClient" />.
/// </summary>
public class ActivityPubClient : IActivityPubClient
{
private readonly ActivityPubOptions _apOptions;
private readonly HttpClient _httpClient = new();
private readonly IJsonLdSerializer _jsonLdSerializer;
/// <summary>
/// Constructs a new ActivityPub client
/// </summary>
/// <param name="apOptions">Options for the ActivityPub protocol</param>
/// <param name="jsonLdSerializer">JSON-LD serializer</param>
public ActivityPubClient(ActivityPubOptions apOptions, IJsonLdSerializer jsonLdSerializer)
{
_apOptions = apOptions;
_jsonLdSerializer = jsonLdSerializer;
// The fuck?
// https://stackoverflow.com/questions/47176104/c-sharp-add-accept-header-to-httpclient
foreach (var mimeType in apOptions.RequestContentTypes)
{
var mediaType = new MediaTypeWithQualityHeaderValue(mimeType);
_httpClient.DefaultRequestHeaders.Accept.Add(mediaType);
}
}
/// <inheritdoc />
public int DefaultGetRecursion { get; set; }
/// <inheritdoc />
public async Task<T> Get<T>(Uri uri, int? maxRecursion = null, CancellationToken cancellationToken = default)
where T : ASType
=> (T)await Get(uri, typeof(T), maxRecursion, cancellationToken);
/// <inheritdoc />
public int DefaultResolveRecursion { get; set; } = 1;
/// <inheritdoc />
public async Task<T> Resolve<T>(Linkable<T> linkable, int? maxRecursion = null, CancellationToken cancellationToken = default)
where T : ASObject
{
// short-circuit the easy case
if (linkable.HasValue)
return linkable.Value;
// Get and recursively populate
maxRecursion ??= DefaultResolveRecursion;
return await Get<T>(linkable.Link, maxRecursion - 1, cancellationToken);
}
/// <inheritdoc />
public async Task<List<T>> Resolve<T>(LinkableList<T> linkables, int? maxRecursion = null, CancellationToken cancellationToken = default)
where T : ASObject
{
var list = new List<T>();
foreach (var linkable in linkables)
{
var value = await Resolve(linkable, maxRecursion, cancellationToken);
list.Add(value);
}
return list;
}
private async Task<ASType> Get(Uri uri, Type targetType, int? maxRecursion, CancellationToken cancellationToken = default)
{
var resp = await _httpClient.GetAsync(uri, cancellationToken);
if (!resp.IsSuccessStatusCode)
throw new ApplicationException($"Request failed: got status {resp.StatusCode}");
var mediaType = resp.Content.Headers.ContentType?.MediaType;
if (mediaType == null || !_apOptions.ResponseContentTypes.Contains(mediaType))
throw new ApplicationException($"Request failed: unsupported content type {mediaType}");
var json = await resp.Content.ReadAsStringAsync(cancellationToken);
var jsonObj = _jsonLdSerializer.Deserialize(json, targetType);
if (jsonObj is not ASType obj)
throw new JsonException($"Failed to deserialize object - parser returned unsupported object {jsonObj?.GetType()}");
// If its a link, then recursively follow it.
maxRecursion ??= DefaultGetRecursion;
if (maxRecursion > 0 && obj is ASLink link)
obj = await Get(link.HRef.Uri, targetType, maxRecursion - 1, cancellationToken);
return obj;
}
#region Dispose
/// <inheritdoc />
public void Dispose()
{
// Dispose of unmanaged resources.
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose the object's resources
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
_httpClient.Dispose();
_disposed = true;
}
private bool _disposed;
#endregion
}