2using System.Net.Http.Headers;
5using System.Threading.Tasks;
7using Microsoft.Extensions.DependencyInjection;
8using Microsoft.Extensions.Logging;
37 var serviceCollection =
new ServiceCollection();
39 var clientBuilder = serviceCollection
43 clientBuilder.ConfigureHttpClient(
46 client.BaseAddress = graphQLEndpoint;
48 if (oAuthProvider.HasValue)
55 if (addAuthorizationHandler)
63 return serviceCollection.BuildServiceProvider();
76 public ValueTask<IAuthenticatedGraphQLServerClient>
CreateFromLogin(Uri host,
string username,
string password,
bool attemptLoginRefresh =
true, CancellationToken cancellationToken =
default)
78 var basicCredentials =
new AuthenticationHeaderValue(
80 Convert.ToBase64String(
81 Encoding.UTF8.GetBytes($
"{username}:{password}")));
92 public ValueTask<IAuthenticatedGraphQLServerClient>
CreateFromOAuth(Uri host,
string oAuthCode,
OAuthProvider oAuthProvider, CancellationToken cancellationToken =
default)
94 var oAuthCredentials =
new AuthenticationHeaderValue(
109 var authenticationHeader =
new AuthenticationHeaderValue(
116 authenticationHeader);
119 serviceProvider.GetRequiredService<IGraphQLClient>(),
121 serviceProvider.GetRequiredService<ILogger<GraphQLServerClient>>(),
131 serviceProvider.GetRequiredService<IGraphQLClient>(),
133 serviceProvider.GetRequiredService<ILogger<GraphQLServerClient>>());
148 AuthenticationHeaderValue initialCredentials,
150 bool attemptLoginRefresh,
151 CancellationToken cancellationToken)
156 oAuthProvider: oAuthProvider);
159 var client = serviceProvider.GetRequiredService<IGraphQLClient>();
161 IOperationResult<ILoginResult> result;
164 AuthorizationMessageHandler.Header.Value = initialCredentials;
167 result = await client.Login.ExecuteAsync(cancellationToken).ConfigureAwait(
false);
171 AuthorizationMessageHandler.Header.Value = previousAuthHeader;
177 serviceProvider.GetRequiredService<ILogger<GraphQLServerClient>>(),
179 attemptLoginRefresh ? initialCredentials :
null,
189 await serviceProvider.
DisposeAsync().ConfigureAwait(
false);
Represents a JWT returned by the API.
Routes to a server actions.
const string GraphQL
The GraphQL route.
override ValueTask DisposeAsync()
DelegatingHandler that applies the AuthenticationHeaderValue.
static AsyncLocal< AuthenticationHeaderValue?> Header
The AsyncLocal<T> AuthenticationHeaderValue to be applied.
readonly IRestServerClientFactory restClientFactory
The IRestServerClientFactory for the GraphQLServerClientFactory.
static ServiceProvider SetupServiceProvider(Uri host, bool addAuthorizationHandler, AuthenticationHeaderValue? headerOverride=null, OAuthProvider? oAuthProvider=null)
Sets up a ServiceProvider for providing the IGraphQLClient.
IAuthenticatedGraphQLServerClient CreateFromToken(Uri host, string token)
Create a IRestServerClient.A new IGraphQLServerClient.
GraphQLServerClientFactory(IRestServerClientFactory restClientFactory)
Initializes a new instance of the GraphQLServerClientFactory class.
IGraphQLServerClient CreateUnauthenticated(Uri host)
Create an unauthenticated IGraphQLServerClient.A new IGraphQLServerClient.
IRestServerClient CreateAuthenticatedTransferClient(Uri host, string bearer)
Create a ITransferClient for a given host and bearer token.
ValueTask< IAuthenticatedGraphQLServerClient > CreateFromOAuth(Uri host, string oAuthCode, OAuthProvider oAuthProvider, CancellationToken cancellationToken=default)
Create a IGraphQLServerClient using an OAuth login.A ValueTask<TResult> resulting in a new IAuthentic...
ValueTask< IAuthenticatedGraphQLServerClient > CreateFromLogin(Uri host, string username, string password, bool attemptLoginRefresh=true, CancellationToken cancellationToken=default)
Create a IGraphQLServerClient using a password login.A ValueTask<TResult> resulting in a new IAuthent...
async ValueTask< IAuthenticatedGraphQLServerClient > CreateWithAuthCall(Uri host, AuthenticationHeaderValue initialCredentials, OAuthProvider? oAuthProvider, bool attemptLoginRefresh, CancellationToken cancellationToken)
Create an IAuthenticatedGraphQLServerClient from a remote login call.
ScalarSerializer<TSerialized, TRuntime> for JsonWebTokens.
ScalarSerializer<TSerialized, TRuntime> for Versions.
ScalarSerializer<TSerialized, TRuntime> for UInt32s.
A IGraphQLServerClient known to be authenticated.
Factory for creating IGraphQLServerClients.
Wrapper for using a TGS IGraphQLClient.
Factory for creating IRestServerClients.
IRestServerClient CreateFromToken(Uri host, TokenResponse token)
Create a IRestServerClient.
Main client for communicating with a server.
OAuthProvider
List of OAuth providers supported by TGS.