com.taptap.sdk.core 4.3.12 → 4.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Mobile/Editor/NativeDependencies.xml +2 -2
- package/Resources/TapMessage.prefab +352 -0
- package/Resources/TapMessage.prefab.meta +10 -0
- package/Resources/TapSDKUIRoot.prefab +1 -1
- package/Resources/tap_toast_background.png +0 -0
- package/Resources/tap_toast_background.png.meta +128 -0
- package/Resources/tap_toast_background1.png +0 -0
- package/Resources/tap_toast_background1.png.meta +128 -0
- package/Runtime/Internal/Log/TapLog.cs +1 -1
- package/Runtime/Internal/Platform/PlatformTypeUtils.cs +5 -5
- package/Runtime/Internal/Utils/TapMessage.cs +69 -0
- package/Runtime/Internal/Utils/TapMessage.cs.meta +13 -0
- package/Runtime/Public/TapTapSDK.cs +1 -1
- package/Standalone/Runtime/Internal/Bean/TapGatekeeper.cs +42 -0
- package/Standalone/Runtime/Internal/{Http/TimeUtil.cs.meta → Bean/TapGatekeeper.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Bean.meta +8 -0
- package/Standalone/Runtime/Internal/Constants.cs +2 -2
- package/Standalone/Runtime/Internal/EventSender.cs +30 -19
- package/Standalone/Runtime/Internal/Http/TapHttp.cs +377 -0
- package/Standalone/Runtime/Internal/Http/{HttpClient.cs.meta → TapHttp.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Http/TapHttpBuilder.cs +92 -0
- package/Standalone/Runtime/Internal/Http/{NetUtils.cs.meta → TapHttpBuilder.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Http/TapHttpErrorConstants.cs +114 -0
- package/Standalone/Runtime/Internal/Http/TapHttpErrorConstants.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpException.cs +71 -0
- package/Standalone/Runtime/Internal/Http/TapHttpException.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpParser.cs +132 -0
- package/Standalone/Runtime/Internal/Http/TapHttpParser.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResponse.cs +37 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResponse.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResult.cs +90 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResult.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpRetryStrategy.cs +237 -0
- package/Standalone/Runtime/Internal/Http/TapHttpRetryStrategy.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpSign.cs +137 -0
- package/Standalone/Runtime/Internal/Http/TapHttpSign.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpUtils.cs +166 -0
- package/Standalone/Runtime/Internal/Http/TapHttpUtils.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogHttpClient.cs +6 -17
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogQueueBase.cs +1 -1
- package/Standalone/Runtime/Internal/service/ITapLoginService.cs +9 -0
- package/Standalone/Runtime/Internal/service/ITapLoginService.cs.meta +11 -0
- package/Standalone/Runtime/Internal/service.meta +8 -0
- package/Standalone/Runtime/Public/TapCoreStandalone.cs +67 -81
- package/link.xml +4 -0
- package/link.xml.meta +7 -0
- package/package.json +1 -1
- package/Standalone/Runtime/Internal/Http/HttpClient.cs +0 -211
- package/Standalone/Runtime/Internal/Http/NetUtils.cs +0 -48
- package/Standalone/Runtime/Internal/Http/TimeUtil.cs +0 -30
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
|
|
3
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
4
|
+
{
|
|
5
|
+
public class TapHttpBuilder
|
|
6
|
+
{
|
|
7
|
+
private readonly string moduleName;
|
|
8
|
+
private readonly string moduleVersion;
|
|
9
|
+
|
|
10
|
+
private string domain = null;
|
|
11
|
+
|
|
12
|
+
private ITapHttpSign sign = null;
|
|
13
|
+
private ITapHttpParser parser = null;
|
|
14
|
+
|
|
15
|
+
private long connectTimeoutMillis = TapHttp.CONNECT_TIMEOUT_MILLIS;
|
|
16
|
+
private long readTimeoutMillis = TapHttp.READ_TIMEOUT_MILLIS;
|
|
17
|
+
private long writeTimeoutMillis = TapHttp.WRITE_TIMEOUT_MILLIS;
|
|
18
|
+
|
|
19
|
+
public TapHttpBuilder(string moduleName, string moduleVersion)
|
|
20
|
+
{
|
|
21
|
+
this.moduleName = moduleName ?? throw new ArgumentNullException(nameof(moduleName));
|
|
22
|
+
this.moduleVersion = moduleVersion ?? throw new ArgumentNullException(nameof(moduleVersion));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public TapHttp Build()
|
|
26
|
+
{
|
|
27
|
+
TapHttpConfig httpConfig = new TapHttpConfig
|
|
28
|
+
{
|
|
29
|
+
ModuleName = moduleName,
|
|
30
|
+
ModuleVersion = moduleVersion,
|
|
31
|
+
Domain = domain,
|
|
32
|
+
Sign = sign ?? TapHttpSign.CreateDefaultSign(),
|
|
33
|
+
Parser = parser ?? TapHttpParser.CreateDefaultParser(),
|
|
34
|
+
ConnectTimeoutMillis = connectTimeoutMillis,
|
|
35
|
+
ReadTimeoutMillis = readTimeoutMillis,
|
|
36
|
+
WriteTimeoutMillis = writeTimeoutMillis,
|
|
37
|
+
};
|
|
38
|
+
return new TapHttp(httpConfig);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public TapHttpBuilder ConnectTimeout(long connectTimeoutMillis)
|
|
42
|
+
{
|
|
43
|
+
this.connectTimeoutMillis = connectTimeoutMillis;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public TapHttpBuilder ReadTimeout(long readTimeoutMillis)
|
|
48
|
+
{
|
|
49
|
+
this.readTimeoutMillis = readTimeoutMillis;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public TapHttpBuilder WriteTimeout(long writeTimeoutMillis)
|
|
54
|
+
{
|
|
55
|
+
this.writeTimeoutMillis = writeTimeoutMillis;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public TapHttpBuilder Domain(string domain)
|
|
60
|
+
{
|
|
61
|
+
this.domain = domain;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public TapHttpBuilder Sign(ITapHttpSign sign)
|
|
66
|
+
{
|
|
67
|
+
this.sign = sign;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public TapHttpBuilder Parser(ITapHttpParser parser)
|
|
72
|
+
{
|
|
73
|
+
this.parser = parser;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
internal class TapHttpConfig
|
|
80
|
+
{
|
|
81
|
+
public string ModuleName { get; set; }
|
|
82
|
+
public string ModuleVersion { get; set; }
|
|
83
|
+
public string Domain { get; set; }
|
|
84
|
+
public ITapHttpSign Sign { get; set; }
|
|
85
|
+
public ITapHttpParser Parser { get; set; }
|
|
86
|
+
public ITapHttpParser RetryStrategy { get; set; }
|
|
87
|
+
public long ConnectTimeoutMillis { get; set; }
|
|
88
|
+
public long ReadTimeoutMillis { get; set; }
|
|
89
|
+
public long WriteTimeoutMillis { get; set; }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
|
|
2
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
3
|
+
{
|
|
4
|
+
/// <summary>
|
|
5
|
+
/// HTTP 错误常量类。
|
|
6
|
+
/// </summary>
|
|
7
|
+
public static class TapHttpErrorConstants
|
|
8
|
+
{
|
|
9
|
+
|
|
10
|
+
// --------------------------------------------------------------------------------------------
|
|
11
|
+
// 下面的错误信息是服务器端 [data.error] 字段的枚举
|
|
12
|
+
// --------------------------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
/// <summary>
|
|
15
|
+
/// 400 请阅读:[通用 API 人机验证协议](https://xindong.atlassian.net/wiki/spaces/TAP/pages/66032081)。
|
|
16
|
+
/// </summary>
|
|
17
|
+
public const string ERROR_CAPTCHA_NEEDS = "captcha.needs";
|
|
18
|
+
|
|
19
|
+
/// <summary>
|
|
20
|
+
/// 400 人机验证未通过。
|
|
21
|
+
/// </summary>
|
|
22
|
+
public const string ERROR_CAPTCHA_FAILED = "captcha.failed";
|
|
23
|
+
|
|
24
|
+
/// <summary>
|
|
25
|
+
/// 403 用户冻结。
|
|
26
|
+
/// </summary>
|
|
27
|
+
public const string ERROR_USER_IS_DEACTIVATED = "user_is_deactivated";
|
|
28
|
+
|
|
29
|
+
/// <summary>
|
|
30
|
+
/// 400 请求缺少某个必需参数,包含一个不支持的参数或参数值,或者格式不正确。
|
|
31
|
+
/// </summary>
|
|
32
|
+
public const string ERROR_INVALID_REQUEST = "invalid_request";
|
|
33
|
+
|
|
34
|
+
/// <summary>
|
|
35
|
+
/// 404 请求失败,请求所希望得到的资源未被在服务器上发现。在参数相同的情况下,不应该重复请求。
|
|
36
|
+
/// </summary>
|
|
37
|
+
public const string ERROR_NOT_FOUND = "not_found";
|
|
38
|
+
|
|
39
|
+
/// <summary>
|
|
40
|
+
/// 403 用户没有对当前动作的权限,引导重新身份验证并不能提供任何帮助,而且这个请求也不应该被重复提交。
|
|
41
|
+
/// </summary>
|
|
42
|
+
public const string ERROR_FORBIDDEN = "forbidden";
|
|
43
|
+
|
|
44
|
+
/// <summary>
|
|
45
|
+
/// 500 服务器出现异常情况,可稍等后重新尝试请求,但需有尝试上限,建议最多 3 次,如一直失败,则中断并告知用户。
|
|
46
|
+
/// </summary>
|
|
47
|
+
public const string ERROR_SERVER_ERROR = "server_error";
|
|
48
|
+
|
|
49
|
+
/// <summary>
|
|
50
|
+
/// 400 客户端时间不正确,应请求服务器时间重新构造。
|
|
51
|
+
/// </summary>
|
|
52
|
+
public const string ERROR_INVALID_TIME = "invalid_time";
|
|
53
|
+
|
|
54
|
+
/// <summary>
|
|
55
|
+
/// 400 请求是重放的。
|
|
56
|
+
/// </summary>
|
|
57
|
+
public const string ERROR_REPLAY_ATTACKS = "replay_attacks";
|
|
58
|
+
|
|
59
|
+
/// <summary>
|
|
60
|
+
/// 401 client_id、client_secret 参数无效。
|
|
61
|
+
/// </summary>
|
|
62
|
+
public const string ERROR_INVALID_CLIENT = "invalid_client";
|
|
63
|
+
|
|
64
|
+
/// <summary>
|
|
65
|
+
/// 400 提供的 Access Grant 是无效的、过期的或已撤销的,例如: Device Code 无效(一个设备授权码只能使用一次)等。
|
|
66
|
+
/// </summary>
|
|
67
|
+
public const string ERROR_INVALID_GRANT = "invalid_grant";
|
|
68
|
+
|
|
69
|
+
/// <summary>
|
|
70
|
+
/// 400 服务器不支持 grant_type 参数的值。
|
|
71
|
+
/// </summary>
|
|
72
|
+
public const string ERROR_UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type";
|
|
73
|
+
|
|
74
|
+
/// <summary>
|
|
75
|
+
/// 400 服务器不支持 response_type 参数的值。
|
|
76
|
+
/// </summary>
|
|
77
|
+
public const string ERROR_UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type";
|
|
78
|
+
|
|
79
|
+
/// <summary>
|
|
80
|
+
/// 400 服务器不支持 secret_type 参数的值。
|
|
81
|
+
/// </summary>
|
|
82
|
+
public const string ERROR_UNSUPPORTED_SECRET_TYPE = "unsupported_secret_type";
|
|
83
|
+
|
|
84
|
+
/// <summary>
|
|
85
|
+
/// 400 Device Flow 中,设备通过 Device Code 换取 Access Token 的接口过于频繁。
|
|
86
|
+
/// </summary>
|
|
87
|
+
public const string ERROR_SLOW_DOWN = "slow_down";
|
|
88
|
+
|
|
89
|
+
/// <summary>
|
|
90
|
+
/// 429 登录尝试次数过多,请稍后重试,用于 password 模式下出错次数过多。
|
|
91
|
+
/// </summary>
|
|
92
|
+
public const string ERROR_TOO_MANY_LOGIN_ATTEMPTS = "too_many_login_attempts";
|
|
93
|
+
|
|
94
|
+
/// <summary>
|
|
95
|
+
/// 401 授权服务器拒绝请求,这个状态出现在拿着 token 请求用户资源时,如出现,客户端应退出本地的用户登录信息,引导用户重新登录。
|
|
96
|
+
/// </summary>
|
|
97
|
+
public const string ERROR_ACCESS_DENIED = "access_denied";
|
|
98
|
+
|
|
99
|
+
/// <summary>
|
|
100
|
+
/// 401 认证内容无效 grant_type 为 password 的模式下,用户名或密码错误。
|
|
101
|
+
/// </summary>
|
|
102
|
+
public const string ERROR_INVALID_CREDENTIALS = "invalid_credentials";
|
|
103
|
+
|
|
104
|
+
/// <summary>
|
|
105
|
+
/// 400 Device Flow 中,用户还没有对 Device Code 完成授权操作,按 interval 要求频率继续轮询,直到 expires_in 过期。
|
|
106
|
+
/// </summary>
|
|
107
|
+
public const string ERROR_AUTHORIZATION_PENDING = "authorization_pending";
|
|
108
|
+
|
|
109
|
+
/// <summary>
|
|
110
|
+
/// 服务端业务异常,如:防沉迷 token 失效(code=20000)。
|
|
111
|
+
/// </summary>
|
|
112
|
+
public const string ERROR_BUSINESS_ERROR = "business_code_error";
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Net;
|
|
3
|
+
|
|
4
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
5
|
+
{
|
|
6
|
+
public abstract class AbsTapHttpException : Exception
|
|
7
|
+
{
|
|
8
|
+
public AbsTapHttpException(string message) : base(message)
|
|
9
|
+
{
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
protected AbsTapHttpException(string message, Exception e) : base(message, e)
|
|
13
|
+
{
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public class TapHttpUnknownException : AbsTapHttpException
|
|
18
|
+
{
|
|
19
|
+
public TapHttpUnknownException(Exception e) : base("Unknown error", e)
|
|
20
|
+
{
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public class TapHttpInvalidResponseException : AbsTapHttpException
|
|
25
|
+
{
|
|
26
|
+
public HttpStatusCode StatusCode { get; }
|
|
27
|
+
|
|
28
|
+
public TapHttpInvalidResponseException(HttpStatusCode statusCode, string message)
|
|
29
|
+
: base(message)
|
|
30
|
+
{
|
|
31
|
+
StatusCode = statusCode;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// <summary>
|
|
36
|
+
/// 表示 TapSDK 中与 Http 相关的服务端返回的错误信息。
|
|
37
|
+
/// </summary>
|
|
38
|
+
public class TapHttpServerException : AbsTapHttpException
|
|
39
|
+
{
|
|
40
|
+
/// <summary>
|
|
41
|
+
/// 获取服务器返回的 HTTP 状态码。
|
|
42
|
+
/// </summary>
|
|
43
|
+
public HttpStatusCode StatusCode { get; }
|
|
44
|
+
|
|
45
|
+
/// <summary>
|
|
46
|
+
/// 获取服务器返回的 Response。
|
|
47
|
+
/// </summary>
|
|
48
|
+
public TapHttpResponse TapHttpResponse { get; }
|
|
49
|
+
|
|
50
|
+
/// <summary>
|
|
51
|
+
/// 获取服务器返回的 Response Error Data。
|
|
52
|
+
/// </summary>
|
|
53
|
+
public TapHttpErrorData ErrorData { get; }
|
|
54
|
+
|
|
55
|
+
/// <summary>
|
|
56
|
+
/// 初始化 <see cref="TapHttpServerException"/> 类的新实例。
|
|
57
|
+
/// </summary>
|
|
58
|
+
/// <param name="statusCode">服务器返回的 HTTP 状态码。</param>
|
|
59
|
+
/// <param name="code">与异常相关的自定义错误代码。</param>
|
|
60
|
+
/// <param name="msg">错误消息。</param>
|
|
61
|
+
/// <param name="error">服务器返回的错误标识符。</param>
|
|
62
|
+
/// <param name="errorDescription">错误的详细描述。</param>
|
|
63
|
+
public TapHttpServerException(HttpStatusCode statusCode, TapHttpResponse tapHttpResponse, TapHttpErrorData tapHttpErrorData)
|
|
64
|
+
: base(tapHttpErrorData.Msg)
|
|
65
|
+
{
|
|
66
|
+
StatusCode = statusCode;
|
|
67
|
+
TapHttpResponse = tapHttpResponse ?? throw new ArgumentNullException(nameof(tapHttpResponse));
|
|
68
|
+
ErrorData = tapHttpErrorData ?? throw new ArgumentNullException(nameof(tapHttpErrorData));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Net;
|
|
3
|
+
using System.Net.Http;
|
|
4
|
+
using System.Threading.Tasks;
|
|
5
|
+
using Newtonsoft.Json;
|
|
6
|
+
|
|
7
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
8
|
+
{
|
|
9
|
+
/// <summary>
|
|
10
|
+
/// 表示一个 HTTP 解析器的接口。
|
|
11
|
+
/// Represents an HTTP parser interface.
|
|
12
|
+
/// </summary>
|
|
13
|
+
public interface ITapHttpParser
|
|
14
|
+
{
|
|
15
|
+
/// <summary>
|
|
16
|
+
/// 解析 HTTP 响应。
|
|
17
|
+
/// Parses the HTTP response.
|
|
18
|
+
/// </summary>
|
|
19
|
+
/// <typeparam name="T">解析后返回的对象类型。The type of the object to return after parsing.</typeparam>
|
|
20
|
+
/// <param name="response">HTTP 响应消息。The HTTP response message.</param>
|
|
21
|
+
/// <returns>解析结果。The parsing result.</returns>
|
|
22
|
+
Task<TapHttpResult<T>> Parse<T>(HttpResponseMessage response);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// <summary>
|
|
26
|
+
/// 提供 HTTP 解析功能的类。
|
|
27
|
+
/// Class that provides HTTP parsing functionality.
|
|
28
|
+
/// </summary>
|
|
29
|
+
public class TapHttpParser
|
|
30
|
+
{
|
|
31
|
+
/// <summary>
|
|
32
|
+
/// 创建默认的 HTTP 解析器。
|
|
33
|
+
/// Creates a default HTTP parser.
|
|
34
|
+
/// </summary>
|
|
35
|
+
/// <returns>返回一个实现了 ITapHttpParser 接口的解析器。Returns a parser that implements the ITapHttpParser interface.</returns>
|
|
36
|
+
public static ITapHttpParser CreateDefaultParser()
|
|
37
|
+
{
|
|
38
|
+
return new Default();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public static ITapHttpParser CreateEventParser()
|
|
42
|
+
{
|
|
43
|
+
return new Event();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private class Event : ITapHttpParser
|
|
47
|
+
{
|
|
48
|
+
public Task<TapHttpResult<T>> Parse<T>(HttpResponseMessage response)
|
|
49
|
+
{
|
|
50
|
+
_ = response ?? throw new ArgumentNullException(nameof(response));
|
|
51
|
+
HttpStatusCode statusCode = response.StatusCode;
|
|
52
|
+
if (statusCode >= HttpStatusCode.OK && statusCode < HttpStatusCode.MultipleChoices)
|
|
53
|
+
{
|
|
54
|
+
return Task.FromResult(TapHttpResult<T>.Success(default));
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
{
|
|
58
|
+
return Task.FromResult(TapHttpResult<T>.UnknownFailure(default));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private class Default : ITapHttpParser
|
|
65
|
+
{
|
|
66
|
+
/// <summary>
|
|
67
|
+
/// 解析 HTTP 响应。
|
|
68
|
+
/// Parses the HTTP response.
|
|
69
|
+
/// </summary>
|
|
70
|
+
/// <typeparam name="T">解析后返回的对象类型。The type of the object to return after parsing.</typeparam>
|
|
71
|
+
/// <param name="response">HTTP 响应消息。The HTTP response message.</param>
|
|
72
|
+
/// <returns>解析结果。The parsing result.</returns>
|
|
73
|
+
public async Task<TapHttpResult<T>> Parse<T>(HttpResponseMessage response)
|
|
74
|
+
{
|
|
75
|
+
_ = response ?? throw new ArgumentNullException(nameof(response));
|
|
76
|
+
HttpStatusCode statusCode = response.StatusCode;
|
|
77
|
+
// 读取响应内容
|
|
78
|
+
// Read the response content
|
|
79
|
+
string content = await response.Content.ReadAsStringAsync();
|
|
80
|
+
|
|
81
|
+
// 处理响应
|
|
82
|
+
// Handle the response
|
|
83
|
+
TapHttpResponse httpResponse;
|
|
84
|
+
try
|
|
85
|
+
{
|
|
86
|
+
httpResponse = JsonConvert.DeserializeObject<TapHttpResponse>(content);
|
|
87
|
+
}
|
|
88
|
+
catch (Exception)
|
|
89
|
+
{
|
|
90
|
+
return TapHttpResult<T>.InvalidResponseFailure(new TapHttpInvalidResponseException(statusCode, "Failed to parse TapHttpResponse"));
|
|
91
|
+
}
|
|
92
|
+
if (httpResponse == null)
|
|
93
|
+
{
|
|
94
|
+
return TapHttpResult<T>.InvalidResponseFailure(new TapHttpInvalidResponseException(statusCode, "TapHttpResponse is null"));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (httpResponse.Success)
|
|
98
|
+
{
|
|
99
|
+
// 修正时间
|
|
100
|
+
// Fix the time
|
|
101
|
+
TapHttpTime.FixTime(httpResponse.Now);
|
|
102
|
+
if (httpResponse.Data == null)
|
|
103
|
+
{
|
|
104
|
+
return TapHttpResult<T>.InvalidResponseFailure(new TapHttpInvalidResponseException(statusCode, "TapHttpResponse.Data is null"));
|
|
105
|
+
}
|
|
106
|
+
try
|
|
107
|
+
{
|
|
108
|
+
T data = httpResponse.Data.ToObject<T>();
|
|
109
|
+
if (data == null)
|
|
110
|
+
{
|
|
111
|
+
return TapHttpResult<T>.InvalidResponseFailure(new TapHttpInvalidResponseException(statusCode, "TapHttpResponse.Data is null"));
|
|
112
|
+
}
|
|
113
|
+
return TapHttpResult<T>.Success(data);
|
|
114
|
+
}
|
|
115
|
+
catch (Exception)
|
|
116
|
+
{
|
|
117
|
+
return TapHttpResult<T>.InvalidResponseFailure(new TapHttpInvalidResponseException(statusCode, "Failed to parse TapHttpResponse.Data"));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else
|
|
121
|
+
{
|
|
122
|
+
TapHttpErrorData httpErrorData = httpResponse.Data.ToObject<TapHttpErrorData>();
|
|
123
|
+
if (httpErrorData == null)
|
|
124
|
+
{
|
|
125
|
+
return TapHttpResult<T>.InvalidResponseFailure(new TapHttpInvalidResponseException(statusCode, "TapHttpErrorData is null"));
|
|
126
|
+
}
|
|
127
|
+
return TapHttpResult<T>.ServerFailure(new TapHttpServerException((HttpStatusCode)500, httpResponse, httpErrorData));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using Newtonsoft.Json;
|
|
3
|
+
using Newtonsoft.Json.Linq;
|
|
4
|
+
|
|
5
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
6
|
+
{
|
|
7
|
+
|
|
8
|
+
[Serializable]
|
|
9
|
+
public class TapHttpResponse
|
|
10
|
+
{
|
|
11
|
+
[JsonProperty("data")]
|
|
12
|
+
public JObject Data { get; private set; }
|
|
13
|
+
|
|
14
|
+
[JsonProperty("success")]
|
|
15
|
+
public bool Success { get; private set; }
|
|
16
|
+
|
|
17
|
+
[JsonProperty("now")]
|
|
18
|
+
public int Now { get; private set; }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
[Serializable]
|
|
22
|
+
public class TapHttpErrorData
|
|
23
|
+
{
|
|
24
|
+
[JsonProperty("code")]
|
|
25
|
+
public int Code { get; private set; }
|
|
26
|
+
|
|
27
|
+
[JsonProperty("msg")]
|
|
28
|
+
public string Msg { get; private set; }
|
|
29
|
+
|
|
30
|
+
[JsonProperty("error")]
|
|
31
|
+
public string Error { get; private set; }
|
|
32
|
+
|
|
33
|
+
[JsonProperty("error_description")]
|
|
34
|
+
public string ErrorDescription { get; private set; }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Net;
|
|
3
|
+
|
|
4
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
5
|
+
{
|
|
6
|
+
/// <summary>
|
|
7
|
+
/// 表示 HTTP 错误类型的枚举。
|
|
8
|
+
/// </summary>
|
|
9
|
+
|
|
10
|
+
/// <summary>
|
|
11
|
+
/// 表示 TapSDK 中 HTTP 请求的结果。
|
|
12
|
+
/// </summary>
|
|
13
|
+
public class TapHttpResult<T>
|
|
14
|
+
{
|
|
15
|
+
/// <summary>
|
|
16
|
+
/// 指示请求是否成功。
|
|
17
|
+
/// </summary>
|
|
18
|
+
public bool IsSuccess { get; private set; }
|
|
19
|
+
|
|
20
|
+
/// <summary>
|
|
21
|
+
/// HTTP 请求的响应内容。
|
|
22
|
+
/// </summary>
|
|
23
|
+
public T Data { get; private set; }
|
|
24
|
+
|
|
25
|
+
/// <summary>
|
|
26
|
+
/// 错误类型,区分网络错误、客户端错误、服务器错误等。
|
|
27
|
+
/// </summary>
|
|
28
|
+
public AbsTapHttpException HttpException { get; private set; }
|
|
29
|
+
|
|
30
|
+
/// <summary>
|
|
31
|
+
/// 私有构造函数,防止直接实例化。
|
|
32
|
+
/// </summary>
|
|
33
|
+
private TapHttpResult()
|
|
34
|
+
{
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// <summary>
|
|
38
|
+
/// 创建一个成功的 HTTP 请求结果。
|
|
39
|
+
/// </summary>
|
|
40
|
+
/// <param name="response">HTTP 响应的内容。</param>
|
|
41
|
+
/// <returns>TapHttpResult 对象,表示成功的请求。</returns>
|
|
42
|
+
public static TapHttpResult<T> Success(T data)
|
|
43
|
+
{
|
|
44
|
+
return new TapHttpResult<T>
|
|
45
|
+
{
|
|
46
|
+
IsSuccess = true,
|
|
47
|
+
Data = data,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// <summary>
|
|
52
|
+
/// 创建一个失败的 HTTP 请求结果,通常是网络错误或客户端错误。
|
|
53
|
+
/// </summary>
|
|
54
|
+
/// <param name="errorType">错误类型,例如网络错误或客户端错误。</param>
|
|
55
|
+
/// <param name="exception">异常对象,用于传递错误详情(可选)。</param>
|
|
56
|
+
/// <returns>TapHttpResult 对象,表示失败的请求。</returns>
|
|
57
|
+
public static TapHttpResult<T> InvalidResponseFailure(TapHttpInvalidResponseException exception)
|
|
58
|
+
{
|
|
59
|
+
return new TapHttpResult<T>
|
|
60
|
+
{
|
|
61
|
+
IsSuccess = false,
|
|
62
|
+
HttpException = exception
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// <summary>
|
|
67
|
+
/// 创建一个服务端返回的错误结果。
|
|
68
|
+
/// </summary>
|
|
69
|
+
/// <param name="httpException">包含详细服务端错误信息的异常对象。</param>
|
|
70
|
+
/// <returns>TapHttpResult 对象,表示服务端错误的请求。</returns>
|
|
71
|
+
public static TapHttpResult<T> ServerFailure(TapHttpServerException httpException)
|
|
72
|
+
{
|
|
73
|
+
return new TapHttpResult<T>
|
|
74
|
+
{
|
|
75
|
+
IsSuccess = false,
|
|
76
|
+
HttpException = httpException,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public static TapHttpResult<T> UnknownFailure(TapHttpUnknownException httpException)
|
|
81
|
+
{
|
|
82
|
+
return new TapHttpResult<T>
|
|
83
|
+
{
|
|
84
|
+
IsSuccess = false,
|
|
85
|
+
HttpException = httpException,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
}
|