一克泥在线服务
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Best.HTTP;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using IchniOnline.Online.Network.Models;
|
||||
@@ -17,12 +18,12 @@ namespace IchniOnline.Online.Network
|
||||
private static IchniOnlineApiClient _instance;
|
||||
public static IchniOnlineApiClient Instance => _instance ??= new IchniOnlineApiClient();
|
||||
|
||||
public string BaseUrl { get; set; } = "http://localhost:5308";
|
||||
public string BaseUrl { get; set; } = "https://ichni.hoshino.fan";
|
||||
public string JwtToken { get; set; }
|
||||
|
||||
private IchniOnlineApiClient() { }
|
||||
|
||||
public async UniTask<ApiResult<T>> GetAsync<T>(string endpoint)
|
||||
public async UniTask<ApiResult<T>> GetAsync<T>(string endpoint, CancellationToken cancellationToken = default)
|
||||
{
|
||||
string url = BuildUrl(endpoint);
|
||||
var request = new HTTPRequest(new Uri(url), HTTPMethods.Get);
|
||||
@@ -30,16 +31,20 @@ namespace IchniOnline.Online.Network
|
||||
|
||||
try
|
||||
{
|
||||
var resp = await SendAsync(request);
|
||||
var resp = await SendAsync(request, cancellationToken);
|
||||
return ProcessResponse<T>(resp);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return ApiResult<T>.Fail(ResponseCode.InternalServerError, "Request canceled");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ApiResult<T>.Fail(ResponseCode.InternalServerError, "Network error", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask<ApiResult<T>> PostAsync<T>(string endpoint, object body)
|
||||
public async UniTask<ApiResult<T>> PostAsync<T>(string endpoint, object body, CancellationToken cancellationToken = default)
|
||||
{
|
||||
string url = BuildUrl(endpoint);
|
||||
var request = new HTTPRequest(new Uri(url), HTTPMethods.Post);
|
||||
@@ -54,9 +59,13 @@ namespace IchniOnline.Online.Network
|
||||
|
||||
try
|
||||
{
|
||||
var resp = await SendAsync(request);
|
||||
var resp = await SendAsync(request, cancellationToken);
|
||||
return ProcessResponse<T>(resp);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return ApiResult<T>.Fail(ResponseCode.InternalServerError, "Request canceled");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ApiResult<T>.Fail(ResponseCode.InternalServerError, "Network error", ex.Message);
|
||||
@@ -81,12 +90,23 @@ namespace IchniOnline.Online.Network
|
||||
}
|
||||
}
|
||||
|
||||
private UniTask<HTTPResponse> SendAsync(HTTPRequest request)
|
||||
private UniTask<HTTPResponse> SendAsync(HTTPRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var completionSource = new UniTaskCompletionSource<HTTPResponse>();
|
||||
var cancellationRegistration = default(CancellationTokenRegistration);
|
||||
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
cancellationRegistration = cancellationToken.Register(() =>
|
||||
{
|
||||
request.Abort();
|
||||
completionSource.TrySetCanceled();
|
||||
});
|
||||
}
|
||||
|
||||
request.Callback = (req, resp) =>
|
||||
{
|
||||
cancellationRegistration.Dispose();
|
||||
switch (req.State)
|
||||
{
|
||||
case HTTPRequestStates.Finished:
|
||||
|
||||
Reference in New Issue
Block a user