change web manager

intergrate reqeust corutine
This commit is contained in:
lwj
2025-06-19 18:11:12 +09:00
parent e5324ab48f
commit c1de962908
2 changed files with 101 additions and 0 deletions

View File

@@ -16,6 +16,8 @@ namespace Octopus.Simulator
public event Action<LogicData> 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<LogicInfo>($"{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());
}
}
}

View File

@@ -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<T>(string requestURL, Action<T> callback, string json = "")
{
StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.GET, callback, json));
}
public void Reqeust_Post<T>(string requestURL, Action<T> callback, string json = "")
{
StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.POST, callback, json));
}
public void Reqeust_Put<T>(string requestURL, Action<T> callback, string json = "")
{
StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.PUT, callback, json));
}
public void Reqeust_Patch<T>(string requestURL, Action<T> callback, string json = "")
{
StartCoroutine(Reqeuest(_webConfigLoader.BaseUrl + requestURL, RequestType.PATCH, callback, json));
}
public void Reqeust_Delete<T>(string requestURL, Action<T> 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<T>(string URL,RequestType type, System.Action<T> 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<T>(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;
}
}
}
}