diff --git a/Assets/WorkSpace/LH/LogicData/LogicDataManager.cs b/Assets/WorkSpace/LH/LogicData/LogicDataManager.cs index 4c3fddc4..be7904c8 100644 --- a/Assets/WorkSpace/LH/LogicData/LogicDataManager.cs +++ b/Assets/WorkSpace/LH/LogicData/LogicDataManager.cs @@ -16,6 +16,8 @@ namespace Octopus.Simulator public event Action onLogicUpdated; + + // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { @@ -34,6 +36,8 @@ namespace Octopus.Simulator GetDataFromInfo(info.data); } }); + + WebManager.Instance.Reqeust_Get($"{WebManager.Instance.apiConfig.logic}/{WebParameters.config.logicId}",GetDataFromResponse); } void GetDataFromInfo(LogicData Data) @@ -51,5 +55,10 @@ namespace Octopus.Simulator } } } + + void GetDataFromResponse(LogicInfo response) + { + Debug.Log(response.data.ToString()); + } } } \ No newline at end of file diff --git a/Assets/WorkSpace/LH/Web/WebManager.cs b/Assets/WorkSpace/LH/Web/WebManager.cs index f67b60eb..9719da49 100644 --- a/Assets/WorkSpace/LH/Web/WebManager.cs +++ b/Assets/WorkSpace/LH/Web/WebManager.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; +using System; namespace UVC.Networks { @@ -12,6 +13,8 @@ namespace UVC.Networks //Post, Get. Put을 사용하며, Post의 경우엔 Class를 입력으로 받아 Json형식으로 변환하여 업로드합니다. //비동기로 실행하기 위해 Coroutine으로 제작되었으며, callback의 bool값을 통해 서버로부터 //정상적으로 API가 입력, 결과가 출력되었는지를 확인할 수 있습니다. + + public class WebManager : MonoBehaviour { static WebManager webManager; @@ -210,5 +213,94 @@ namespace UVC.Networks } } } + //별도의 Data Content 가 없을 경우 reqeustURL 과 callback 등 만을 parameter로 넘기면 됨 + public void Reqeust_Get(string requestURL, Action callback, string json = "") + { + StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.GET, callback, json)); + } + + public void Reqeust_Post(string requestURL, Action callback, string json = "") + { + StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.POST, callback, json)); + } + + + public void Reqeust_Put(string requestURL, Action callback, string json = "") + { + StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.PUT, callback, json)); + } + + + public void Reqeust_Patch(string requestURL, Action callback, string json = "") + { + StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.PATCH, callback, json)); + } + + + public void Reqeust_Delete(string requestURL, Action callback, string json = "") + { + StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.DELETE, callback, json)); + } + + public enum RequestType + { + //"GET", "POST", "PUT", "PATCH", "DELETE" + GET, + POST, + PUT, + PATCH, + DELETE, + + } + + IEnumerator Reqeuest(string URL,RequestType type, System.Action callback, string json) + { + using (UnityWebRequest request = new UnityWebRequest(URL, type.ToString())) + { + byte[] body = System.Text.Encoding.UTF8.GetBytes(json); + request.uploadHandler = new UploadHandlerRaw(body); + + request.downloadHandler = new DownloadHandlerBuffer(); + + request.SetRequestHeader("Content-Type", "application/json"); + request.SetRequestHeader("Authorization", "Bearer " + _webConfigLoader.Token); + + yield return request.SendWebRequest(); + + if (IsSuccess(request.result)) + { + string responseJson = request.downloadHandler.text; + T result = JsonConvert.DeserializeObject(responseJson); + callback?.Invoke(result); + } + else + { + //통신 실패 시 에러 팝업 추후 추가 + Debug.Log($"Request failed, API Name {URL}, Result :{request.error}"); + } + } + } + + + private bool IsSuccess(UnityWebRequest.Result result) + { + switch(result) + { + case UnityWebRequest.Result.Success: + return true; + + case UnityWebRequest.Result.ConnectionError: + return false; + + case UnityWebRequest.Result.ProtocolError: + return false; + + default: + return false; + + } + + } + } } \ No newline at end of file