apply hutao api changes

This commit is contained in:
Lightczx
2023-12-06 16:39:48 +08:00
parent 97842559d7
commit e6e6e22b9c
5 changed files with 50 additions and 4 deletions

View File

@@ -2828,6 +2828,9 @@
<data name="WebHoyolabInvalidUid" xml:space="preserve">
<value>无效的 UID</value>
</data>
<data name="WebHutaoServiceUnAvailable" xml:space="preserve">
<value>胡桃服务维护中</value>
</data>
<data name="WebIndexOrSpiralAbyssVerificationFailed" xml:space="preserve">
<value>验证失败,请手动验证或前往「米游社-我的角色」页面查看</value>
</data>

View File

@@ -107,12 +107,19 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel
protected override async ValueTask<bool> InitializeUIAsync()
{
Response<IPInformation> resp = await hutaoInfrastructureClient.GetIPInformationAsync().ConfigureAwait(false);
IPInformation info;
if (resp.IsOk())
{
await taskContext.SwitchToMainThreadAsync();
IPInformation = resp.Data;
info = resp.Data;
}
else
{
info = IPInformation.Default;
}
await taskContext.SwitchToMainThreadAsync();
IPInformation = info;
return true;
}

View File

@@ -5,10 +5,12 @@ namespace Snap.Hutao.Web.Hutao;
internal sealed class IPInformation
{
private const string Unknown = "Unknown";
public static IPInformation Default { get; } = new()
{
Ip = "Unknown",
Division = "Unknown",
Ip = Unknown,
Division = Unknown,
};
[JsonPropertyName("ip")]
@@ -19,6 +21,11 @@ internal sealed class IPInformation
public override string ToString()
{
if (Ip is Unknown && Division is Unknown)
{
return SH.WebHutaoServiceUnAvailable;
}
return SH.FormatViewPageSettingDeviceIpDescription(Ip, Division);
}
}

View File

@@ -1,7 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Web.Hutao;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
@@ -12,6 +14,7 @@ internal static class HttpRequestMessageBuilderExtension
private const string RequestErrorMessage = "请求异常已忽略";
internal static async ValueTask<TResult?> TryCatchSendAsync<TResult>(this HttpRequestMessageBuilder builder, HttpClient httpClient, ILogger logger, CancellationToken token)
where TResult : class
{
try
{
@@ -26,6 +29,22 @@ internal static class HttpRequestMessageBuilderExtension
catch (HttpRequestException ex)
{
logger.LogWarning(ex, RequestErrorMessage);
if (ex.StatusCode is HttpStatusCode.BadGateway)
{
Type resultType = typeof(TResult);
if (resultType == typeof(HutaoResponse))
{
return Activator.CreateInstance(resultType, 502, SH.WebHutaoServiceUnAvailable, default) as TResult;
}
if (resultType.IsConstructedGenericType && resultType.GetGenericTypeDefinition() == typeof(HutaoResponse<>))
{
return Activator.CreateInstance(resultType, 502, SH.WebHutaoServiceUnAvailable, default, default) as TResult;
}
}
return default;
}
catch (IOException ex)

View File

@@ -0,0 +1,10 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Web.Request.Builder;
internal enum HttpTryCatchSendStrategy
{
Default,
HutaoApi,
}