Skip to content

Commit 71f8079

Browse files
committed
refactor(RESTConnector): Removed WWW dependency, refactored to UnityWebRequest
1 parent 298db1d commit 71f8079

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

Scripts/Connection/RESTConnector.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System.Text;
2727
using UnityEngine;
2828
using UnityEngine.Networking;
29+
using FullSerializer;
2930

3031
#if !NETFX_CORE
3132
using System.Net;
@@ -233,6 +234,10 @@ public Request()
233234
/// The data to send through the connection. Do not use Forms if set.
234235
/// </summary>
235236
public byte[] Send { get; set; }
237+
///// <summary>
238+
///// The data to send through the connection. Do not use Forms if set.
239+
///// </summary>
240+
//public string Body { get; set; }
236241
/// <summary>
237242
/// Multi-part form data that needs to be sent. Do not use Send if set.
238243
/// </summary>
@@ -414,9 +419,9 @@ private IEnumerator ProcessRequestQueue()
414419
Response resp = new Response();
415420

416421
DateTime startTime = DateTime.Now;
417-
if (string.IsNullOrEmpty(req.HttpMethod))
422+
if (string.IsNullOrEmpty(req.HttpMethod) || req.Delete)
418423
{
419-
WWW www = null;
424+
UnityWebRequest www = null;
420425
if (req.Forms != null)
421426
{
422427
if (req.Send != null)
@@ -443,13 +448,30 @@ private IEnumerator ProcessRequestQueue()
443448
{
444449
Log.Error("RESTConnector.ProcessRequestQueue()", "Exception when initializing WWWForm: {0}", e.ToString());
445450
}
446-
www = new WWW(url, form.data, req.Headers);
451+
www = UnityWebRequest.Post(url, form);
447452
}
448453
else if (req.Send == null)
449-
www = new WWW(url, null, req.Headers);
454+
{
455+
www = UnityWebRequest.Get(url);
456+
}
450457
else
451-
www = new WWW(url, req.Send, req.Headers);
458+
{
459+
www = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
460+
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(req.Send);
461+
www.SetRequestHeader("Content-Type", "application/json");
462+
}
463+
464+
foreach (KeyValuePair<string, string> kvp in req.Headers)
465+
{
466+
www.SetRequestHeader(kvp.Key, kvp.Value);
467+
}
468+
www.downloadHandler = new DownloadHandlerBuffer();
452469

470+
#if UNITY_2017_2_OR_NEWER
471+
www.SendWebRequest();
472+
#else
473+
www.Send();
474+
#endif
453475
#if ENABLE_DEBUGGING
454476
Log.Debug("RESTConnector", "URL: {0}", url);
455477
#endif
@@ -465,7 +487,7 @@ private IEnumerator ProcessRequestQueue()
465487
if (req.OnUploadProgress != null)
466488
req.OnUploadProgress(www.uploadProgress);
467489
if (req.OnDownloadProgress != null)
468-
req.OnDownloadProgress(www.progress);
490+
req.OnDownloadProgress(www.downloadProgress);
469491

470492
#if UNITY_EDITOR
471493
if (!UnityEditorInternal.InternalEditorUtility.inBatchMode)
@@ -508,16 +530,16 @@ private IEnumerator ProcessRequestQueue()
508530
URL = url,
509531
ErrorCode = resp.HttpResponseCode = nErrorCode,
510532
ErrorMessage = www.error,
511-
Response = www.text,
512-
ResponseHeaders = www.responseHeaders
533+
Response = www.downloadHandler.text,
534+
ResponseHeaders = www.GetResponseHeaders()
513535
};
514536

515537
if (bError)
516538
Log.Error("RESTConnector.ProcessRequestQueue()", "URL: {0}, ErrorCode: {1}, Error: {2}, Response: {3}", url, nErrorCode, www.error,
517-
string.IsNullOrEmpty(www.text) ? "" : www.text);
539+
string.IsNullOrEmpty(www.downloadHandler.text) ? "" : www.downloadHandler.text);
518540
else
519541
Log.Warning("RESTConnector.ProcessRequestQueue()", "URL: {0}, ErrorCode: {1}, Error: {2}, Response: {3}", url, nErrorCode, www.error,
520-
string.IsNullOrEmpty(www.text) ? "" : www.text);
542+
string.IsNullOrEmpty(www.downloadHandler.text) ? "" : www.downloadHandler.text);
521543
}
522544
if (!www.isDone)
523545
{
@@ -535,16 +557,16 @@ private IEnumerator ProcessRequestQueue()
535557
if (!bError)
536558
{
537559
resp.Success = true;
538-
resp.Data = www.bytes;
539-
resp.HttpResponseCode = GetResponseCode(www);
560+
resp.Data = www.downloadHandler.data;
561+
resp.HttpResponseCode = www.responseCode;
540562
}
541563
else
542564
{
543565
resp.Success = false;
544566
resp.Error = error;
545567
}
546568

547-
resp.Headers = www.responseHeaders;
569+
resp.Headers = www.GetResponseHeaders();
548570

549571
resp.ElapsedTime = (float)(DateTime.Now - startTime).TotalSeconds;
550572

0 commit comments

Comments
 (0)