squash a bunch of commits

This commit is contained in:
2022-10-30 12:03:16 -05:00
parent 09b72c1858
commit 93027e8c57
222 changed files with 6157 additions and 3201 deletions

View File

@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Components.WebAssembly.Http;
namespace Shogi.UI.Pages.Home.Api
{
public class CookieCredentialsMessageHandler : DelegatingHandler
{
public CookieCredentialsMessageHandler()
{
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
return base.SendAsync(request, cancellationToken);
}
}
}

View File

@@ -0,0 +1,17 @@
using Shogi.Contracts.Api;
using Shogi.Contracts.Types;
using System.Net;
namespace Shogi.UI.Pages.Home.Api
{
public interface IShogiApi
{
Task<CreateGuestTokenResponse?> GetGuestToken();
Task<Session?> GetSession(string name);
Task<ReadAllSessionsResponse?> GetSessions();
Task<Guid?> GetToken();
Task GuestLogout();
Task PostMove(string sessionName, Move move);
Task<HttpStatusCode> PostSession(string name, bool isPrivate);
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
namespace Shogi.UI.Pages.Home.Api
{
public class MsalMessageHandler : AuthorizationMessageHandler
{
public MsalMessageHandler(IAccessTokenProvider provider, NavigationManager navigation) : base(provider, navigation)
{
ConfigureHandler(
authorizedUrls: new[] { "https://api.lucaserver.space", "https://localhost:5001" },
scopes: new[] { "api://c1e94676-cab0-42ba-8b6c-9532b8486fff/DefaultScope" },
returnUrl: "https://localhost:3000");
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken);
}
}
}

View 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;
}
}
}