squash a bunch of commits
This commit is contained in:
98
Shogi.UI/Pages/Home/Api/ShogiApi.cs
Normal file
98
Shogi.UI/Pages/Home/Api/ShogiApi.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using Shogi.Contracts.Api;
|
||||
using Shogi.Contracts.Types;
|
||||
using Shogi.UI.Pages.Home.Account;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Shogi.UI.Pages.Home.Api
|
||||
{
|
||||
public class ShogiApi : IShogiApi
|
||||
{
|
||||
public const string GuestClientName = "Guest";
|
||||
public const string MsalClientName = "Msal";
|
||||
public const string AnonymouseClientName = "Anonymous";
|
||||
|
||||
private readonly JsonSerializerOptions serializerOptions;
|
||||
private readonly IHttpClientFactory clientFactory;
|
||||
private readonly AccountState accountState;
|
||||
|
||||
public ShogiApi(IHttpClientFactory clientFactory, AccountState accountState)
|
||||
{
|
||||
serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true,
|
||||
};
|
||||
serializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
this.clientFactory = clientFactory;
|
||||
this.accountState = accountState;
|
||||
}
|
||||
|
||||
private HttpClient HttpClient => accountState.User?.WhichAccountPlatform switch
|
||||
{
|
||||
WhichAccountPlatform.Guest => clientFactory.CreateClient(GuestClientName),
|
||||
WhichAccountPlatform.Microsoft => clientFactory.CreateClient(MsalClientName),
|
||||
_ => clientFactory.CreateClient(AnonymouseClientName)
|
||||
};
|
||||
|
||||
public async Task<CreateGuestTokenResponse?> GetGuestToken()
|
||||
{
|
||||
var response = await HttpClient.GetAsync(new Uri("User/GuestToken", UriKind.Relative));
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadFromJsonAsync<CreateGuestTokenResponse>(serializerOptions);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task GuestLogout()
|
||||
{
|
||||
var response = await HttpClient.PutAsync(new Uri("User/GuestLogout", UriKind.Relative), null);
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
public async Task<Session?> GetSession(string name)
|
||||
{
|
||||
var response = await HttpClient.GetAsync(new Uri($"Session/{name}", UriKind.Relative));
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return (await response.Content.ReadFromJsonAsync<ReadSessionResponse>(serializerOptions))?.Session;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<ReadAllSessionsResponse?> GetSessions()
|
||||
{
|
||||
var response = await HttpClient.GetAsync(new Uri("Session", UriKind.Relative));
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadFromJsonAsync<ReadAllSessionsResponse>(serializerOptions);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<Guid?> GetToken()
|
||||
{
|
||||
var response = await HttpClient.GetAsync(new Uri("User/Token", UriKind.Relative));
|
||||
var deserialized = await response.Content.ReadFromJsonAsync<CreateTokenResponse>(serializerOptions);
|
||||
return deserialized?.OneTimeToken;
|
||||
}
|
||||
|
||||
public async Task PostMove(string sessionName, Contracts.Types.Move move)
|
||||
{
|
||||
await this.HttpClient.PostAsJsonAsync($"{sessionName}/Move", new MovePieceCommand { Move = move });
|
||||
}
|
||||
|
||||
public async Task<HttpStatusCode> PostSession(string name, bool isPrivate)
|
||||
{
|
||||
var response = await HttpClient.PostAsJsonAsync(new Uri("Session", UriKind.Relative), new CreateSessionCommand
|
||||
{
|
||||
Name = name,
|
||||
IsPrivate = isPrivate
|
||||
});
|
||||
return response.StatusCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user