squash a bunch of commits
This commit is contained in:
18
Shogi.UI/Pages/Home/Api/CookieMessageHandler.cs
Normal file
18
Shogi.UI/Pages/Home/Api/CookieMessageHandler.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Shogi.UI/Pages/Home/Api/IShogiApi.cs
Normal file
17
Shogi.UI/Pages/Home/Api/IShogiApi.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
21
Shogi.UI/Pages/Home/Api/MsalMessageHandler.cs
Normal file
21
Shogi.UI/Pages/Home/Api/MsalMessageHandler.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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