update
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Best.HTTP;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using IchniOnline.Online.Network.Models;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -17,12 +17,12 @@ namespace IchniOnline.Online.Network
|
||||
private static IchniOnlineApiClient _instance;
|
||||
public static IchniOnlineApiClient Instance => _instance ??= new IchniOnlineApiClient();
|
||||
|
||||
public string BaseUrl { get; set; } = "http://localhost:53734";
|
||||
public string BaseUrl { get; set; } = "http://localhost:5308";
|
||||
public string JwtToken { get; set; }
|
||||
|
||||
private IchniOnlineApiClient() { }
|
||||
|
||||
public async Task<ApiResult<T>> GetAsync<T>(string endpoint)
|
||||
public async UniTask<ApiResult<T>> GetAsync<T>(string endpoint)
|
||||
{
|
||||
string url = BuildUrl(endpoint);
|
||||
var request = new HTTPRequest(new Uri(url), HTTPMethods.Get);
|
||||
@@ -30,7 +30,7 @@ namespace IchniOnline.Online.Network
|
||||
|
||||
try
|
||||
{
|
||||
var resp = await request.GetHTTPResponseAsync();
|
||||
var resp = await SendAsync(request);
|
||||
return ProcessResponse<T>(resp);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -39,7 +39,7 @@ namespace IchniOnline.Online.Network
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ApiResult<T>> PostAsync<T>(string endpoint, object body)
|
||||
public async UniTask<ApiResult<T>> PostAsync<T>(string endpoint, object body)
|
||||
{
|
||||
string url = BuildUrl(endpoint);
|
||||
var request = new HTTPRequest(new Uri(url), HTTPMethods.Post);
|
||||
@@ -54,7 +54,7 @@ namespace IchniOnline.Online.Network
|
||||
|
||||
try
|
||||
{
|
||||
var resp = await request.GetHTTPResponseAsync();
|
||||
var resp = await SendAsync(request);
|
||||
return ProcessResponse<T>(resp);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -81,6 +81,30 @@ namespace IchniOnline.Online.Network
|
||||
}
|
||||
}
|
||||
|
||||
private UniTask<HTTPResponse> SendAsync(HTTPRequest request)
|
||||
{
|
||||
var completionSource = new UniTaskCompletionSource<HTTPResponse>();
|
||||
|
||||
request.Callback = (req, resp) =>
|
||||
{
|
||||
switch (req.State)
|
||||
{
|
||||
case HTTPRequestStates.Finished:
|
||||
completionSource.TrySetResult(resp);
|
||||
break;
|
||||
case HTTPRequestStates.Aborted:
|
||||
completionSource.TrySetCanceled();
|
||||
break;
|
||||
default:
|
||||
completionSource.TrySetException(req.Exception ?? new Exception($"HTTP request failed: {req.State}"));
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
request.Send();
|
||||
return completionSource.Task;
|
||||
}
|
||||
|
||||
private ApiResult<T> ProcessResponse<T>(HTTPResponse resp)
|
||||
{
|
||||
string json = resp.DataAsText;
|
||||
@@ -98,12 +122,12 @@ namespace IchniOnline.Online.Network
|
||||
return ApiResult<T>.Fail(ResponseCode.InternalServerError, "Failed to parse response JSON");
|
||||
}
|
||||
|
||||
if (response.Code == ResponseCode.Ok)
|
||||
if (response.code == ResponseCode.Ok)
|
||||
{
|
||||
return ApiResult<T>.Ok(response.Data);
|
||||
return ApiResult<T>.Ok(response.data);
|
||||
}
|
||||
|
||||
return ApiResult<T>.Fail(response.Code, response.Message);
|
||||
return ApiResult<T>.Fail(response.code, response.message);
|
||||
}
|
||||
|
||||
// Non-2xx: try to parse server error body
|
||||
@@ -112,7 +136,7 @@ namespace IchniOnline.Online.Network
|
||||
var errorResponse = JsonUtility.FromJson(json, typeof(GlobalResponseBase)) as GlobalResponseBase;
|
||||
if (errorResponse != null)
|
||||
{
|
||||
return ApiResult<T>.Fail(errorResponse.Code, errorResponse.Message);
|
||||
return ApiResult<T>.Fail(errorResponse.code, errorResponse.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user