-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoderApiClient.cs
More file actions
103 lines (85 loc) · 3.05 KB
/
Copy pathCoderApiClient.cs
File metadata and controls
103 lines (85 loc) · 3.05 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
using System.Text.Json.Serialization;
namespace Coder.Desktop.CoderSdk.Coder;
public interface ICoderApiClientFactory
{
public ICoderApiClient Create(string baseUrl);
public ICoderApiClient Create(ICoderApiClientCredentialProvider provider);
}
public class CoderApiClientCredential
{
public required Uri CoderUrl { get; set; }
public required string ApiToken { get; set; }
}
public interface ICoderApiClientCredentialProvider
{
public CoderApiClientCredential? GetCoderApiClientCredential();
}
public class CoderApiClientFactory : ICoderApiClientFactory
{
public ICoderApiClient Create(string baseUrl)
{
return new CoderApiClient(baseUrl);
}
public ICoderApiClient Create(ICoderApiClientCredentialProvider provider)
{
var cred = provider.GetCoderApiClientCredential();
if (cred == null)
throw new InvalidOperationException(
"Cannot create Coder API client with invalid credential provider: credential is null");
var client = Create(cred.CoderUrl);
client.SetSessionToken(cred.ApiToken);
return client;
}
public ICoderApiClient Create(Uri baseUrl)
{
return new CoderApiClient(baseUrl);
}
}
public partial interface ICoderApiClient
{
public void SetSessionToken(string token);
}
[JsonSerializable(typeof(AgentConnectionInfo))]
[JsonSerializable(typeof(BuildInfo))]
[JsonSerializable(typeof(Response))]
[JsonSerializable(typeof(User))]
[JsonSerializable(typeof(ValidationError))]
[JsonSerializable(typeof(WorkspaceAgent))]
[JsonSerializable(typeof(WorkspaceApp))]
public partial class CoderApiJsonContext : JsonSerializerContext;
/// <summary>
/// Provides a limited selection of API methods for a Coder instance.
/// </summary>
public partial class CoderApiClient : ICoderApiClient
{
private const string SessionTokenHeader = "Coder-Session-Token";
private readonly JsonHttpClient _httpClient;
public CoderApiClient(string baseUrl) : this(new Uri(baseUrl, UriKind.Absolute))
{
}
public CoderApiClient(Uri baseUrl)
{
if (baseUrl.PathAndQuery != "/")
throw new ArgumentException($"Base URL '{baseUrl}' must not contain a path", nameof(baseUrl));
_httpClient = new JsonHttpClient(baseUrl, CoderApiJsonContext.Default);
}
public CoderApiClient(string baseUrl, string token) : this(baseUrl)
{
SetSessionToken(token);
}
public void SetSessionToken(string token)
{
_httpClient.RemoveHeader(SessionTokenHeader);
_httpClient.SetHeader(SessionTokenHeader, token);
}
private async Task<TResponse> SendRequestNoBodyAsync<TResponse>(HttpMethod method, string path,
CancellationToken ct = default)
{
return await SendRequestAsync<object, TResponse>(method, path, null, ct);
}
private Task<TResponse> SendRequestAsync<TRequest, TResponse>(HttpMethod method, string path,
TRequest? payload, CancellationToken ct = default)
{
return _httpClient.SendRequestAsync<TRequest, TResponse>(method, path, payload, ct);
}
}