From fc869caa935bd39615e7581db134cf1800cea888 Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Fri, 12 Feb 2021 20:14:25 -0600 Subject: [PATCH 1/2] Fix calling a PUT route with POST. --- .../Repositories/GameboardRepository.cs | 4 ++-- .../Utility/AuthenticatedHttpClient.cs | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs b/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs index 96c3e44..54f7143 100644 --- a/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs +++ b/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs @@ -1,10 +1,10 @@ using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.ShogiUI.Sockets.Repositories.Utility; using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; -using Gameboard.ShogiUI.Sockets.Repositories.Utility; namespace Gameboard.ShogiUI.Sockets.Repositories { @@ -73,7 +73,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories { var uri = $"Session/{gameName}/Join"; var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); - var response = await client.PostAsync(Uri.EscapeUriString(uri), content); + var response = await client.PutAsync(Uri.EscapeUriString(uri), content); var json = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(json); } diff --git a/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs b/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs index a25c7d7..07a3df4 100644 --- a/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs +++ b/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs @@ -13,6 +13,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.Utility Task DeleteAsync(string requestUri); Task GetAsync(string requestUri); Task PostAsync(string requestUri, HttpContent content); + Task PutAsync(string requestUri, HttpContent content); } public class AuthenticatedHttpClient : HttpClient, IAuthenticatedHttpClient @@ -31,7 +32,6 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.Utility clientSecret = configuration["AppSettings:ClientSecret"]; BaseAddress = new Uri(configuration["AppSettings:GameboardShogiApi"]); } - private async Task RefreshBearerToken() { var disco = await this.GetDiscoveryDocumentAsync(identityServerUrl); @@ -56,7 +56,23 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.Utility logger.LogInformation("Refreshing Bearer Token to {BaseAddress}", BaseAddress); this.SetBearerToken(tokenResponse.AccessToken); } - + public async new Task PutAsync(string requestUri, HttpContent content) + { + var response = await base.PutAsync(requestUri, content); + if (response.StatusCode == HttpStatusCode.Unauthorized) + { + await RefreshBearerToken(); + response = await base.PostAsync(requestUri, content); + } + logger.LogInformation( + "Repository PUT to {BaseUrl}{RequestUrl} \n\tRespCode: {RespCode} \n\tRequest: {Request}\n\tResponse: {Response}\n", + BaseAddress, + requestUri, + response.StatusCode, + await content.ReadAsStringAsync(), + await response.Content.ReadAsStringAsync()); + return response; + } public async new Task GetAsync(string requestUri) { var response = await base.GetAsync(requestUri); From 6fc29100bd55e54720a223a55d45ce2e5a351fab Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Fri, 12 Feb 2021 20:48:52 -0600 Subject: [PATCH 2/2] Fix not sending session name when joining a public session. --- Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj | 4 ---- .../Managers/ClientActionHandlers/JoinGameHandler.cs | 3 ++- Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs | 3 ++- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj b/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj index aa056ee..3f3669b 100644 --- a/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj +++ b/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj @@ -18,9 +18,5 @@ - - - - diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs index 0a45cca..c435d63 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs @@ -35,7 +35,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers var joinGameResponse = await gameboardRepository.PutJoinPublicSession(request.GameName, new PutJoinPublicSession { - PlayerName = userName + PlayerName = userName, + SessionName = request.GameName }); if (joinGameResponse.JoinSucceeded) diff --git a/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs b/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs index 54f7143..07504fc 100644 --- a/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs +++ b/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs @@ -71,7 +71,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories public async Task PutJoinPublicSession(string gameName, PutJoinPublicSession request) { - var uri = $"Session/{gameName}/Join"; + var uri = $"Session/Join"; var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var response = await client.PutAsync(Uri.EscapeUriString(uri), content); var json = await response.Content.ReadAsStringAsync(); @@ -114,6 +114,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories { var uri = $"Player/{playerName}"; var response = await client.GetAsync(Uri.EscapeUriString(uri)); + Console.WriteLine(response); var json = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(json); }