-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathUnityHttpClient.cs
More file actions
93 lines (81 loc) 路 3.64 KB
/
Copy pathUnityHttpClient.cs
File metadata and controls
93 lines (81 loc) 路 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Text;
using System.Threading.Tasks;
using ChainSafe.Gaming.Web3.Environment;
using ChainSafe.Gaming.Web3.Environment.Http;
using UnityEngine.Assertions;
using UnityEngine.Networking;
namespace ChainSafe.Gaming.Web3.Core.Unity
{
/// <summary>
/// A Unity-specific implementation of an HTTP client, allowing for making web requests within the Unity environment.
/// This class handles both GET and POST requests and ensures operations occur on the main Unity thread.
/// </summary>
public class UnityHttpClient : IHttpClient
{
private readonly IMainThreadRunner mainThreadRunner;
/// <summary>
/// Initializes a new instance of the <see cref="UnityHttpClient"/> class.
/// </summary>
/// <param name="mainThreadRunner">Provides capabilities to run tasks on the Unity main thread.</param>
public UnityHttpClient(IMainThreadRunner mainThreadRunner)
{
this.mainThreadRunner = mainThreadRunner;
}
public ValueTask<NetworkResponse<string>> GetRaw(string url, params HttpHeader[] headers)
{
return new ValueTask<NetworkResponse<string>>(mainThreadRunner.EnqueueTask(async () =>
{
using var request = UnityWebRequest.Get(url);
foreach (var header in headers)
{
request.SetRequestHeader(header.Name, header.Value);
}
request.downloadHandler = new DownloadHandlerBuffer();
await request.SendWebRequest();
return UnityWebRequestToNetworkResponse(request);
}));
}
public ValueTask<NetworkResponse<string>> PostRaw(
string url,
string data,
string contentType,
params HttpHeader[] headers)
{
return new ValueTask<NetworkResponse<string>>(mainThreadRunner.EnqueueTask(async () =>
{
using var request = new UnityWebRequest(url, "POST");
foreach (var header in headers)
{
request.SetRequestHeader(header.Name, header.Value);
}
request.uploadHandler = new UploadHandlerRaw(new UTF8Encoding().GetBytes(data));
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", contentType);
await request.SendWebRequest();
return UnityWebRequestToNetworkResponse(request);
}));
}
/// <summary>
/// Converts the UnityWebRequest response to a structured NetworkResponse.
/// </summary>
/// <param name="request">The Unity web request instance.</param>
/// <returns>Converted Network Response.</returns>
private static NetworkResponse<string> UnityWebRequestToNetworkResponse(UnityWebRequest request)
{
// Assert response is successful
{
Assert.AreNotEqual(UnityWebRequest.Result.InProgress, request.result);
if (request.result == UnityWebRequest.Result.ConnectionError)
{
throw new Web3Exception($"HTTP.{request.method} to '{request.url}' - connection error : {request.error}.");
}
if (request.result != UnityWebRequest.Result.Success)
{
throw new Web3Exception(
$"HTTP.{request.method} to '{request.url}' responded with error: {request.downloadHandler.text}");
}
}
return NetworkResponse<string>.Success(request.downloadHandler.text);
}
}
}