Repository navigation
Expand file tree
/
Copy pathSyncService.cs
More file actions
59 lines (49 loc) · 2.17 KB
/
Copy pathSyncService.cs
File metadata and controls
59 lines (49 loc) · 2.17 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
using Common;
namespace TestClient
{
internal class SyncService
{
private readonly IHttpClientFactory clientFactory;
private Uri? syncUri;
public SyncService(IHttpClientFactory clientFactory)
{
this.clientFactory = clientFactory;
}
private HttpClient GetClient() => clientFactory.CreateClient();
public async Task Init(string baseUrl)
{
var response = await GetClient().GetStringAsync($"{baseUrl}Relay/GetSyncUrls?uuid=123");
syncUri = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SyncUrl>>(response)?.First().Url;
if (syncUri?.IsAbsoluteUri == false)
throw new Exception($"Sync URI incomplete: {syncUri}");
}
public async Task<HttpResponseMessage> Post(object content, string? bearerToken = null)
{
if (syncUri == null)
throw new Exception("Init() has not been called");
var request = new HttpRequestMessage(HttpMethod.Post, syncUri.AbsoluteUri)
{
Content = new StringContent(content is string str ? str : System.Text.Json.JsonSerializer.Serialize(content)),
};
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken ?? GenerateToken());
return await GetClient().SendAsync(request);
}
private string GenerateToken(string signingKey = "somereallyreallyreallylongkeygoeshere", string audience = "logsink_client", string pipeline = "problemsource")
{
var (_, tokenString) = TokenHelper.CreateToken(
signingKey,
new TokenHelper.CreateTokenParams
{
Issuer = "jwmb",
Audience = audience,
Expiry = DateTime.UtcNow.AddHours(1),
ClaimsDictionary = new Dictionary<string, string> {
{ "sub", "klingberglab" },
{ "pipeline", pipeline },
}
});
return tokenString;
}
public readonly record struct SyncUrl(Uri Url) { }
}
}