started working on player moves.

This commit is contained in:
2022-11-09 18:50:51 -06:00
parent da76917490
commit f7f752b694
13 changed files with 232 additions and 271 deletions

View File

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

View File

@@ -4,11 +4,10 @@ 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 class ShogiApi : IShogiApi
{
public const string GuestClientName = "Guest";
public const string MsalClientName = "Msal";
@@ -20,12 +19,7 @@ namespace Shogi.UI.Pages.Home.Api
public ShogiApi(IHttpClientFactory clientFactory, AccountState accountState)
{
serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};
serializerOptions.Converters.Add(new JsonStringEnumConverter());
serializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
this.clientFactory = clientFactory;
this.accountState = accountState;
}
@@ -37,16 +31,6 @@ namespace Shogi.UI.Pages.Home.Api
_ => 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);
@@ -55,7 +39,7 @@ namespace Shogi.UI.Pages.Home.Api
public async Task<Session?> GetSession(string name)
{
var response = await HttpClient.GetAsync(new Uri($"Session/{name}", UriKind.Relative));
var response = await HttpClient.GetAsync(new Uri($"Sessions/{name}", UriKind.Relative));
if (response.IsSuccessStatusCode)
{
return (await response.Content.ReadFromJsonAsync<ReadSessionResponse>(serializerOptions))?.Session;
@@ -63,9 +47,9 @@ namespace Shogi.UI.Pages.Home.Api
return null;
}
public async Task<ReadSessionsPlayerCountResponse?> GetSessions()
public async Task<ReadSessionsPlayerCountResponse?> GetSessionsPlayerCount()
{
var response = await HttpClient.GetAsync(new Uri("Session", UriKind.Relative));
var response = await HttpClient.GetAsync(new Uri("Sessions/PlayerCount", UriKind.Relative));
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<ReadSessionsPlayerCountResponse>(serializerOptions);
@@ -73,21 +57,20 @@ namespace Shogi.UI.Pages.Home.Api
return null;
}
public async Task<Guid?> GetToken()
public async Task<CreateTokenResponse?> GetToken()
{
var response = await HttpClient.GetAsync(new Uri("User/Token", UriKind.Relative));
var deserialized = await response.Content.ReadFromJsonAsync<CreateTokenResponse>(serializerOptions);
return deserialized?.OneTimeToken;
var response = await HttpClient.GetFromJsonAsync<CreateTokenResponse>(new Uri("User/Token", UriKind.Relative), serializerOptions);
return response;
}
public async Task PostMove(string sessionName, Contracts.Types.Move move)
{
await this.HttpClient.PostAsJsonAsync($"{sessionName}/Move", new MovePieceCommand { Move = move });
await this.HttpClient.PostAsJsonAsync($"Sessions{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
var response = await HttpClient.PostAsJsonAsync(new Uri("Sessions", UriKind.Relative), new CreateSessionCommand
{
Name = name,
});