-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Extensions.cs
98 lines (95 loc) · 4.15 KB
/
Extensions.cs
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
94
95
96
97
98
using System.Text;
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace Proyecto26.Common.Extensions
{
public static class Extensions
{
/// <summary>
/// Send the web request to the server
/// </summary>
/// <returns>An UnityWebRequestAsyncOperation object.</returns>
/// <param name="request">An UnityWebRequest object.</param>
/// <param name="options">An options object.</param>
public static IEnumerator SendWebRequest(this UnityWebRequest request, RequestHelper options)
{
byte[] bodyRaw = null;
string contentType = "application/json";
if (options.Body != null || !string.IsNullOrEmpty(options.BodyString))
{
var bodyString = options.BodyString;
if (options.Body != null) {
bodyString = JsonUtility.ToJson(options.Body);
}
bodyRaw = Encoding.UTF8.GetBytes(bodyString.ToCharArray());
}
else if (options.SimpleForm != null && options.SimpleForm.Count > 0)
{
bodyRaw = UnityWebRequest.SerializeSimpleForm(options.SimpleForm);
contentType = "application/x-www-form-urlencoded";
}
else if (options.FormSections != null && options.FormSections.Count > 0)
{
byte[] boundary = UnityWebRequest.GenerateBoundary();
byte[] formSections = UnityWebRequest.SerializeFormSections(options.FormSections, boundary);
byte[] terminate = Encoding.UTF8.GetBytes(string.Concat("\r\n--", Encoding.UTF8.GetString(boundary), "--"));
bodyRaw = new byte[formSections.Length + terminate.Length];
System.Buffer.BlockCopy(formSections, 0, bodyRaw, 0, formSections.Length);
System.Buffer.BlockCopy(terminate, 0, bodyRaw, formSections.Length, terminate.Length);
contentType = string.Concat("multipart/form-data; boundary=", Encoding.UTF8.GetString(boundary));
}
if (bodyRaw != null) {
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.uploadHandler.contentType = contentType;
}
if (options.DownloadHandler is DownloadHandler)
request.downloadHandler = options.DownloadHandler;
else
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", contentType);
foreach (var header in RestClient.DefaultRequestHeaders)
{
request.SetRequestHeader(header.Key, header.Value);
}
foreach (var header in options.Headers)
{
request.SetRequestHeader(header.Key, header.Value);
}
if (options.Timeout.HasValue)
{
request.timeout = options.Timeout.Value;
}
if (options.ChunkedTransfer.HasValue)
{
request.chunkedTransfer = options.ChunkedTransfer.Value;
}
options.request = request;
yield return request.SendWebRequest();
}
/// <summary>
/// Create an object with the response of the server
/// </summary>
/// <returns>An object with the response.</returns>
/// <param name="request">An UnityWebRequest object.</param>
public static ResponseHelper CreateWebResponse(this UnityWebRequest request)
{
return new ResponseHelper
{
StatusCode = request.responseCode,
Data = request.downloadHandler.data,
Text = request.downloadHandler.text,
Headers = request.GetResponseHeaders(),
Error = request.error
};
}
public static bool IsValidRequest(this UnityWebRequest request, RequestHelper options)
{
return request.isDone &&
!request.isNetworkError &&
(
!request.isHttpError || options.IgnoreHttpException
);
}
}
}