11
11
#if UNITY_WEBGL && ! UNITY_EDITOR
12
12
using System ;
13
13
using UnityEngine . Scripting ;
14
- using MetaMask . Unity . Utils ;
15
14
using Newtonsoft . Json ;
16
15
using System . Runtime . InteropServices ;
17
16
#endif
@@ -29,7 +28,7 @@ public enum RequestType
29
28
POST ,
30
29
DELETE
31
30
}
32
-
31
+
33
32
/// <summary>
34
33
/// A class that represents a single HTTP Request
35
34
/// </summary>
@@ -63,13 +62,16 @@ public UnityHttpServiceProvider(string baseUrl, string authKey, string authValue
63
62
64
63
public Task < string > Get ( string uri )
65
64
{
66
- var fullUrl = string . IsNullOrWhiteSpace ( baseUrl ) ? uri :
67
- baseUrl . EndsWith ( "/" ) || uri . StartsWith ( "/" ) ? $ "{ baseUrl } { uri } " : $ "{ baseUrl } /{ uri } ";
68
-
65
+ var fullUrl = string . IsNullOrWhiteSpace ( baseUrl )
66
+ ? uri
67
+ : baseUrl . EndsWith ( "/" ) || uri . StartsWith ( "/" )
68
+ ? $ "{ baseUrl } { uri } "
69
+ : $ "{ baseUrl } /{ uri } ";
70
+
69
71
// ensure we dont end on a /
70
72
if ( fullUrl . EndsWith ( "/" ) )
71
73
fullUrl = fullUrl . Substring ( 0 , fullUrl . Length - 1 ) ;
72
-
74
+
73
75
var request = new UnityHttpServiceRequest ( )
74
76
{
75
77
url = fullUrl ,
@@ -78,21 +80,24 @@ public Task<string> Get(string uri)
78
80
authValue = authValue ,
79
81
requestType = RequestType . GET
80
82
} ;
81
-
83
+
82
84
service . requests . Enqueue ( request ) ;
83
85
84
86
return request . requestTask . Task ;
85
87
}
86
88
87
89
public Task < string > Post ( string uri , string @params )
88
90
{
89
- var fullUrl = string . IsNullOrWhiteSpace ( baseUrl ) ? uri :
90
- baseUrl . EndsWith ( "/" ) || uri . StartsWith ( "/" ) ? $ "{ baseUrl } { uri } " : $ "{ baseUrl } /{ uri } ";
91
-
91
+ var fullUrl = string . IsNullOrWhiteSpace ( baseUrl )
92
+ ? uri
93
+ : baseUrl . EndsWith ( "/" ) || uri . StartsWith ( "/" )
94
+ ? $ "{ baseUrl } { uri } "
95
+ : $ "{ baseUrl } /{ uri } ";
96
+
92
97
// ensure we dont end on a /
93
98
if ( fullUrl . EndsWith ( "/" ) )
94
99
fullUrl = fullUrl . Substring ( 0 , fullUrl . Length - 1 ) ;
95
-
100
+
96
101
var request = new UnityHttpServiceRequest ( )
97
102
{
98
103
url = fullUrl ,
@@ -102,21 +107,24 @@ public Task<string> Post(string uri, string @params)
102
107
authKey = authKey ,
103
108
authValue = authValue
104
109
} ;
105
-
110
+
106
111
service . requests . Enqueue ( request ) ;
107
112
108
113
return request . requestTask . Task ;
109
114
}
110
-
115
+
111
116
public Task < string > Delete ( string uri , string @params )
112
117
{
113
- var fullUrl = string . IsNullOrWhiteSpace ( baseUrl ) ? uri :
114
- baseUrl . EndsWith ( "/" ) || uri . StartsWith ( "/" ) ? $ "{ baseUrl } { uri } " : $ "{ baseUrl } /{ uri } ";
115
-
118
+ var fullUrl = string . IsNullOrWhiteSpace ( baseUrl )
119
+ ? uri
120
+ : baseUrl . EndsWith ( "/" ) || uri . StartsWith ( "/" )
121
+ ? $ "{ baseUrl } { uri } "
122
+ : $ "{ baseUrl } /{ uri } ";
123
+
116
124
// ensure we dont end on a /
117
125
if ( fullUrl . EndsWith ( "/" ) )
118
126
fullUrl = fullUrl . Substring ( 0 , fullUrl . Length - 1 ) ;
119
-
127
+
120
128
var request = new UnityHttpServiceRequest ( )
121
129
{
122
130
url = fullUrl ,
@@ -126,7 +134,7 @@ public Task<string> Delete(string uri, string @params)
126
134
authKey = authKey ,
127
135
authValue = authValue
128
136
} ;
129
-
137
+
130
138
service . requests . Enqueue ( request ) ;
131
139
132
140
return request . requestTask . Task ;
@@ -135,7 +143,7 @@ public Task<string> Delete(string uri, string @params)
135
143
136
144
private Queue < UnityHttpServiceRequest > requests = new Queue < UnityHttpServiceRequest > ( ) ;
137
145
private bool isCheckingQueue ;
138
-
146
+
139
147
private IMetaMaskSDK _metaMaskSDK => MetaMaskSDK . SDKInstance ;
140
148
141
149
protected override void Awake ( )
@@ -184,7 +192,7 @@ private IEnumerator ProcessRequest(UnityHttpServiceRequest request)
184
192
method = "GET" ;
185
193
break ;
186
194
}
187
-
195
+
188
196
#if UNITY_WEBGL && ! UNITY_EDITOR
189
197
yield return SendRequestWebgl ( method , request ) ;
190
198
#else
@@ -199,10 +207,8 @@ private IEnumerator SendRequestUnity(string method, UnityHttpServiceRequest requ
199
207
bool isGet = request . requestType == RequestType . GET ;
200
208
string authHeaderKey = request . authKey ;
201
209
string authHeaderValue = request . authValue ;
202
-
203
- using ( UnityWebRequest uwr = ! isGet
204
- ? new UnityWebRequest ( url , method )
205
- : UnityWebRequest . Get ( url ) )
210
+
211
+ using ( UnityWebRequest uwr = ! isGet ? new UnityWebRequest ( url , method ) : UnityWebRequest . Get ( url ) )
206
212
{
207
213
if ( ! string . IsNullOrWhiteSpace ( authHeaderValue ) && ! string . IsNullOrWhiteSpace ( authHeaderKey ) )
208
214
{
@@ -212,7 +218,10 @@ private IEnumerator SendRequestUnity(string method, UnityHttpServiceRequest requ
212
218
if ( Infura . IsUrl ( url ) )
213
219
{
214
220
uwr . SetRequestHeader ( "X-Infura-User-Agent" , $ "metamask/sdk-csharp { _metaMaskSDK . SDKVersion } ") ;
215
- uwr . SetRequestHeader ( "Metamask-Sdk-Info" , $ "Sdk/Unity SdkVersion/{ _metaMaskSDK . SDKVersion } Platform/{ SystemInfo . operatingSystem } dApp/{ _metaMaskSDK . Config . AppUrl } dAppTitle/{ _metaMaskSDK . Config . AppName } ") ;
221
+ uwr . SetRequestHeader (
222
+ "Metamask-Sdk-Info" ,
223
+ $ "Sdk/Unity SdkVersion/{ _metaMaskSDK . SDKVersion } Platform/{ SystemInfo . operatingSystem } dApp/{ _metaMaskSDK . Config . AppUrl } dAppTitle/{ _metaMaskSDK . Config . AppName } "
224
+ ) ;
216
225
}
217
226
218
227
if ( ! string . IsNullOrWhiteSpace ( @params ) )
@@ -223,12 +232,12 @@ private IEnumerator SendRequestUnity(string method, UnityHttpServiceRequest requ
223
232
uwr . uploadHandler . contentType = "application/json" ;
224
233
uwr . SetRequestHeader ( "Content-Type" , "application/json" ) ;
225
234
}
226
-
235
+
227
236
yield return uwr . SendWebRequest ( ) ;
228
237
229
238
switch ( uwr . result )
230
239
{
231
- case UnityWebRequest . Result . ConnectionError :
240
+ case UnityWebRequest . Result . ConnectionError :
232
241
case UnityWebRequest . Result . DataProcessingError :
233
242
case UnityWebRequest . Result . ProtocolError :
234
243
request . requestTask . SetException ( new IOException ( uwr . error + " | " + uwr . downloadHandler . text ) ) ;
@@ -306,4 +315,4 @@ public void OnFetchResponseCallback(string resultJson)
306
315
}
307
316
#endif
308
317
}
309
- }
318
+ }
0 commit comments