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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ namespace IchniOnline.Online.Network.Models
|
||||
[System.Serializable]
|
||||
public abstract class GlobalResponseBase
|
||||
{
|
||||
public ResponseCode Code;
|
||||
public string Message;
|
||||
public ResponseCode code;
|
||||
public string message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -32,7 +32,7 @@ namespace IchniOnline.Online.Network.Models
|
||||
[System.Serializable]
|
||||
public class GlobalResponse<T> : GlobalResponseBase
|
||||
{
|
||||
public T Data;
|
||||
public T data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -5,42 +5,42 @@ namespace IchniOnline.Online.Network.Models
|
||||
[Serializable]
|
||||
public class ThirdPartyLoginRequestDto
|
||||
{
|
||||
public string Token;
|
||||
public string TokenType;
|
||||
public string MacKey;
|
||||
public string MacAlgorithm;
|
||||
public string token;
|
||||
public string tokenType;
|
||||
public string macKey;
|
||||
public string macAlgorithm;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class LoginRequestDto
|
||||
{
|
||||
public string Username;
|
||||
public string EncryptedPassword;
|
||||
public string SessionKey;
|
||||
public string username;
|
||||
public string encryptedPassword;
|
||||
public string sessionKey;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class RegisterRequestDto
|
||||
{
|
||||
public string Username;
|
||||
public string Password;
|
||||
public string DisplayName;
|
||||
public string username;
|
||||
public string password;
|
||||
public string displayName;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class LoginResponseDto
|
||||
{
|
||||
public string Token;
|
||||
public UserResponseDto User;
|
||||
public string token;
|
||||
public UserResponseDto user;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class UserResponseDto
|
||||
{
|
||||
public string UserId;
|
||||
public string Username;
|
||||
public string DisplayName;
|
||||
public string AvatarUrl;
|
||||
public int Permission;
|
||||
public string userId;
|
||||
public string username;
|
||||
public string displayName;
|
||||
public string avatarUrl;
|
||||
public int permission;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user