From 159f97f190b30605fd0f68e72d9255eff0ac47bd Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Tue, 19 Jul 2022 12:36:20 +0300 Subject: [PATCH] fix: HttpClient HttpClient now sending requests & recieving responses as it should --- Assets/_Scripts/Managers/ScoreManager.cs | 3 --- Assets/_Scripts/{ => Utilities}/HttpClient.cs | 24 +++++++++++++------ .../{ => Utilities}/HttpClient.cs.meta | 0 3 files changed, 17 insertions(+), 10 deletions(-) rename Assets/_Scripts/{ => Utilities}/HttpClient.cs (78%) rename Assets/_Scripts/{ => Utilities}/HttpClient.cs.meta (100%) diff --git a/Assets/_Scripts/Managers/ScoreManager.cs b/Assets/_Scripts/Managers/ScoreManager.cs index 3bb30dc..e369e98 100644 --- a/Assets/_Scripts/Managers/ScoreManager.cs +++ b/Assets/_Scripts/Managers/ScoreManager.cs @@ -1,5 +1,4 @@ using System; -using DatabaseModels.DataTransferObjets; using UnityEngine; public class ScoreManager : MonoBehaviour @@ -33,8 +32,6 @@ public class ScoreManager : MonoBehaviour PlayerEvents.OnWallTouched += ResetMultiplierAndReward; GameStateManager.Instance.OnGameStateChange += OnGameStateChange; - - var sbRecordDto = await HttpClient.Get("https://localhost:7248/api/scoreboard/cuqmbr"); } private void OnDestroy() diff --git a/Assets/_Scripts/HttpClient.cs b/Assets/_Scripts/Utilities/HttpClient.cs similarity index 78% rename from Assets/_Scripts/HttpClient.cs rename to Assets/_Scripts/Utilities/HttpClient.cs index d9111d8..27fe230 100644 --- a/Assets/_Scripts/HttpClient.cs +++ b/Assets/_Scripts/Utilities/HttpClient.cs @@ -1,21 +1,29 @@ +using System; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using UnityEngine.Networking; -public static class HttpClient +public static class HttpClient { public static async Task Get(string endpoint) { var getRequest = CreateRequest(endpoint, RequestType.GET); getRequest.SendWebRequest(); - while (getRequest.isDone) + while (!getRequest.isDone) { await Task.Delay(10); } - return JsonConvert.DeserializeObject(getRequest.downloadHandler.text); + try + { + return JsonConvert.DeserializeObject(getRequest.downloadHandler.text); + } + catch (Exception) + { + return default(T); + } } public static async Task Post(string endpoint, object payload) @@ -23,7 +31,7 @@ public static class HttpClient var postRequest = CreateRequest(endpoint, RequestType.POST, payload); postRequest.SendWebRequest(); - while (postRequest.isDone) + while (!postRequest.isDone) { await Task.Delay(10); } @@ -49,10 +57,12 @@ public static class HttpClient return request; } - public enum RequestType + private enum RequestType { - GET = 0, - POST = 1 + GET, + POST, + PUT, + DELETE } } diff --git a/Assets/_Scripts/HttpClient.cs.meta b/Assets/_Scripts/Utilities/HttpClient.cs.meta similarity index 100% rename from Assets/_Scripts/HttpClient.cs.meta rename to Assets/_Scripts/Utilities/HttpClient.cs.meta