66using Newtonsoft . Json . Converters ;
77using Newtonsoft . Json . Serialization ;
88using Solana . Unity . Rpc . Converters ;
9+ using System . ComponentModel ;
10+ using System . Net ;
911using System . Net . Http ;
1012using System . Text ;
1113using System . Threading . Tasks ;
12- using JsonException = Newtonsoft . Json . JsonException ;
14+ using UnityEngine ;
15+ using UnityEngine . Networking ;
1316
1417namespace Solana . Unity . Rpc . Core . Http
1518{
@@ -101,14 +104,12 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
101104 } ;
102105
103106 // execute POST
104- using ( var response = await _httpClient . SendAsync ( httpReq ) . ConfigureAwait ( false ) )
107+ using ( var response = await SendAsyncRequest ( _httpClient , httpReq ) )
105108 {
106109 var result = await HandleResult < T > ( req , response ) . ConfigureAwait ( false ) ;
107110 result . RawRpcRequest = requestJson ;
108111 return result ;
109112 }
110-
111-
112113 }
113114 catch ( HttpRequestException e )
114115 {
@@ -130,10 +131,10 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
130131 }
131132 return result ;
132133 }
133-
134-
134+
135135 }
136136
137+
137138 /// <summary>
138139 /// Handles the result after sending a request.
139140 /// </summary>
@@ -143,7 +144,7 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
143144 /// <returns>A task that represents the asynchronous operation that holds the request result.</returns>
144145 private async Task < RequestResult < T > > HandleResult < T > ( JsonRpcRequest req , HttpResponseMessage response )
145146 {
146- RequestResult < T > result = new RequestResult < T > ( response ) ;
147+ RequestResult < T > result = new ( response ) ;
147148 try
148149 {
149150 result . RawRpcResponse = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
@@ -154,9 +155,10 @@ private async Task<RequestResult<T>> HandleResult<T>(JsonRpcRequest req, HttpRes
154155 }
155156 var res = JsonConvert . DeserializeObject < JsonRpcResponse < T > > ( result . RawRpcResponse , _serializerOptions ) ;
156157
158+
157159 if ( res . Result != null )
158160 {
159- result . Result = ( T ) res . Result ;
161+ result . Result = res . Result ;
160162 result . WasRequestSuccessfullyHandled = true ;
161163 }
162164 else
@@ -226,7 +228,7 @@ public async Task<RequestResult<JsonRpcBatchResponse>> SendBatchRequestAsync(Jso
226228 } ;
227229
228230 // execute POST
229- using ( var response = await _httpClient . SendAsync ( httpReq ) . ConfigureAwait ( false ) )
231+ using ( var response = await SendAsyncRequest ( _httpClient , httpReq ) )
230232 {
231233 var result = await HandleBatchResult ( reqs , response ) . ConfigureAwait ( false ) ;
232234 result . RawRpcRequest = requestsJson ;
@@ -315,7 +317,74 @@ private async Task<RequestResult<JsonRpcBatchResponse>> HandleBatchResult(JsonRp
315317
316318 return result ;
317319 }
318-
320+
321+ /// <summary>
322+ /// Return True if running on Unity, False otherwise
323+ /// </summary>
324+ /// <returns>Return True if running on Unity, False otherwise</returns>
325+ private bool IsUnityPlayer ( )
326+ {
327+ #if NETSTANDARD2_0 && ! DEBUG
328+ try
329+ {
330+ if ( Application . platform != null )
331+ {
332+ return true ;
333+ }
334+ }
335+ catch ( Exception )
336+ {
337+ return false ;
338+ }
339+ #endif
340+ return false ;
341+ }
342+
343+ /// <summary>
344+ /// Send an async request using HttpClient or UnityWebRequest if running on Unity
345+ /// </summary>
346+ /// <param name="httpClient"></param>
347+ /// <param name="httpReq"></param>
348+ /// <returns></returns>
349+ private async Task < HttpResponseMessage > SendAsyncRequest ( HttpClient httpClient , HttpRequestMessage httpReq )
350+ {
351+ if ( IsUnityPlayer ( ) )
352+ {
353+ return await SendUnityWebRequest ( httpClient . BaseAddress , httpReq ) ;
354+ }
355+ return await _httpClient . SendAsync ( httpReq ) . ConfigureAwait ( false ) ;
356+ }
357+
358+ /// <summary>
359+ /// Convert a httReq to a Unity Web request
360+ /// </summary>
361+ /// <param name="uri">RPC URI</param>
362+ /// <param name="httpReq">The http request</param>
363+ /// <returns>Http response</returns>
364+ /// <exception cref="HttpRequestException"></exception>
365+ private async Task < HttpResponseMessage > SendUnityWebRequest ( Uri uri , HttpRequestMessage httpReq )
366+ {
367+ Byte [ ] buffer = await httpReq . Content . ReadAsByteArrayAsync ( ) ;
368+ Console . WriteLine ( $ "Send A") ;
369+ using ( var request = new UnityWebRequest ( uri , httpReq . Method . ToString ( ) ) )
370+ {
371+ request . uploadHandler = new UploadHandlerRaw ( buffer ) ;
372+ request . downloadHandler = new DownloadHandlerBuffer ( ) ;
373+ request . SetRequestHeader ( "Content-Type" , "application/json" ) ;
374+ request . SendWebRequest ( ) ;
375+ if ( request . result == UnityWebRequest . Result . ConnectionError )
376+ {
377+ throw new HttpRequestException ( "Error While Sending: " + request . error ) ;
378+ }
379+ while ( ! request . isDone )
380+ {
381+ await Task . Yield ( ) ;
382+ }
383+ var response = new HttpResponseMessage ( HttpStatusCode . OK ) ;
384+ response . Content = new ByteArrayContent ( Encoding . UTF8 . GetBytes ( request . downloadHandler . text ) ) ;
385+ return response ;
386+ }
387+ }
319388 }
320389
321390}
0 commit comments