tgstation-server 6.12.3
The /tg/station 13 server suite
Loading...
Searching...
No Matches
RestServerClientFactory.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Net.Http.Headers;
5using System.Threading;
6using System.Threading.Tasks;
7
11
13{
16 {
20 internal static IApiClientFactory ApiClientFactory { get; set; }
21
25 readonly ProductHeaderValue productHeaderValue;
26
34
40 {
41 this.productHeaderValue = productHeaderValue ?? throw new ArgumentNullException(nameof(productHeaderValue));
42 }
43
45 public ValueTask<IRestServerClient> CreateFromLogin(
46 Uri host,
47 string username,
48 string password,
49 IEnumerable<IRequestLogger>? requestLoggers = null,
50 TimeSpan? timeout = null,
51 bool attemptLoginRefresh = true,
52 CancellationToken cancellationToken = default)
53 {
54 if (host == null)
55 throw new ArgumentNullException(nameof(host));
56 if (username == null)
57 throw new ArgumentNullException(nameof(username));
58 if (password == null)
59 throw new ArgumentNullException(nameof(password));
60
61 var loginHeaders = new ApiHeaders(productHeaderValue, username, password);
62 return CreateWithNewToken(
63 host,
64 loginHeaders,
65 requestLoggers,
66 timeout,
67 attemptLoginRefresh,
68 cancellationToken);
69 }
70
72 public ValueTask<IRestServerClient> CreateFromOAuth(
73 Uri host,
74 string oAuthCode,
75 OAuthProvider oAuthProvider,
76 IEnumerable<IRequestLogger>? requestLoggers = null,
77 TimeSpan? timeout = null,
78 CancellationToken cancellationToken = default)
79 {
80 if (host == null)
81 throw new ArgumentNullException(nameof(host));
82 if (oAuthCode == null)
83 throw new ArgumentNullException(nameof(oAuthCode));
84
85 var loginHeaders = new ApiHeaders(productHeaderValue, oAuthCode, oAuthProvider);
86 return CreateWithNewToken(
87 host,
88 loginHeaders,
89 requestLoggers,
90 timeout,
91 false,
92 cancellationToken);
93 }
94
97 {
98 if (host == null)
99 throw new ArgumentNullException(nameof(host));
100 if (token == null)
101 throw new ArgumentNullException(nameof(token));
102 if (token.Bearer == null)
103 throw new InvalidOperationException("token.Bearer should not be null!");
104
105 var serverClient = new RestServerClient(
107 host,
108 new ApiHeaders(
110 token),
111 null,
112 false));
113 return serverClient;
114 }
115
117 public async ValueTask<ServerInformationResponse> GetServerInformation(
118 Uri host,
119 IEnumerable<IRequestLogger>? requestLoggers = null,
120 TimeSpan? timeout = null,
121 CancellationToken cancellationToken = default)
122 {
123 await using var api = ApiClientFactory.CreateApiClient(
124 host,
125 new ApiHeaders(
127 new TokenResponse
128 {
129 Bearer = "unused",
130 }),
131 null,
132 true);
133
134 if (requestLoggers != null)
135 foreach (var requestLogger in requestLoggers)
136 api.AddRequestLogger(requestLogger);
137
138 if (timeout.HasValue)
139 api.Timeout = timeout.Value;
140
141 return await api.Read<ServerInformationResponse>(Routes.ApiRoot, cancellationToken).ConfigureAwait(false);
142 }
143
154 async ValueTask<IRestServerClient> CreateWithNewToken(
155 Uri host,
156 ApiHeaders loginHeaders,
157 IEnumerable<IRequestLogger>? requestLoggers,
158 TimeSpan? timeout,
159 bool attemptLoginRefresh,
160 CancellationToken cancellationToken)
161 {
162 requestLoggers ??= Enumerable.Empty<IRequestLogger>();
163
164 TokenResponse token;
165 await using (var api = ApiClientFactory.CreateApiClient(host, loginHeaders, null, false))
166 {
167 foreach (var requestLogger in requestLoggers)
168 api.AddRequestLogger(requestLogger);
169
170 if (timeout.HasValue)
171 api.Timeout = timeout.Value;
172 token = await api.Update<TokenResponse>(Routes.ApiRoot, cancellationToken).ConfigureAwait(false);
173 }
174
175 var apiHeaders = new ApiHeaders(productHeaderValue, token);
176 var client = new RestServerClient(
178 host,
179 apiHeaders,
180 attemptLoginRefresh ? loginHeaders : null,
181 false));
182 if (timeout.HasValue)
183 client.Timeout = timeout.Value;
184
185 foreach (var requestLogger in requestLoggers)
186 client.AddRequestLogger(requestLogger);
187
188 return client;
189 }
190 }
191}
Represents the header that must be present for every server request.
Definition ApiHeaders.cs:25
Represents a JWT returned by the API.
Routes to a server actions.
Definition Routes.cs:9
const string ApiRoot
The root of API methods.
Definition Routes.cs:13
IApiClient CreateApiClient(Uri url, ApiHeaders apiHeaders, ApiHeaders? tokenRefreshHeaders, bool authless)
Create an IApiClient.A new IApiClient.
static RestServerClientFactory()
Initializes static members of the RestServerClientFactory class.
RestServerClientFactory(ProductHeaderValue productHeaderValue)
Initializes a new instance of the RestServerClientFactory class.
readonly ProductHeaderValue productHeaderValue
The ProductHeaderValue for the RestServerClientFactory.
async ValueTask< ServerInformationResponse > GetServerInformation(Uri host, IEnumerable< IRequestLogger >? requestLoggers=null, TimeSpan? timeout=null, CancellationToken cancellationToken=default)
Gets the ServerInformationResponse for a given host .A ValueTask<TResult> resulting in the ServerInfo...
IRestServerClient CreateFromToken(Uri host, TokenResponse token)
Create a IRestServerClient.A new IRestServerClient.
async ValueTask< IRestServerClient > CreateWithNewToken(Uri host, ApiHeaders loginHeaders, IEnumerable< IRequestLogger >? requestLoggers, TimeSpan? timeout, bool attemptLoginRefresh, CancellationToken cancellationToken)
Creates a IRestServerClient from a login operation.
ValueTask< IRestServerClient > CreateFromLogin(Uri host, string username, string password, IEnumerable< IRequestLogger >? requestLoggers=null, TimeSpan? timeout=null, bool attemptLoginRefresh=true, CancellationToken cancellationToken=default)
Create a IRestServerClient using a password login.A ValueTask<TResult> resulting in a new IRestServer...
ValueTask< IRestServerClient > CreateFromOAuth(Uri host, string oAuthCode, OAuthProvider oAuthProvider, IEnumerable< IRequestLogger >? requestLoggers=null, TimeSpan? timeout=null, CancellationToken cancellationToken=default)
Create a IRestServerClient using an OAuth login.A ValueTask<TResult> resulting in a new IRestServerCl...
void AddRequestLogger(IRequestLogger requestLogger)
Adds a requestLogger to the request pipeline.
For logging HTTP requests and responses.
Main client for communicating with a server.
OAuthProvider
List of OAuth providers supported by TGS.