|
1 | 1 | using System;
|
2 |
| -using System.Text; |
3 | 2 | using System.Collections;
|
4 | 3 | using UnityEngine;
|
5 | 4 | using UnityEngine.Networking;
|
6 |
| -using Proyecto26.Common.Extensions; |
| 5 | +using Proyecto26.Common; |
7 | 6 |
|
8 | 7 | namespace Proyecto26
|
9 | 8 | {
|
10 | 9 | public static class HttpBase
|
11 | 10 | {
|
12 |
| - private const string CONTENT_TYPE_HEADER = "Content-Type"; |
13 |
| - private const string CONTENT_TYPE_JSON = "application/json"; |
14 |
| - |
15 | 11 | public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action<RequestException, ResponseHelper> callback)
|
16 | 12 | {
|
17 | 13 | var retries = 0;
|
18 | 14 | do
|
19 | 15 | {
|
20 | 16 | using (var request = CreateRequest(options))
|
21 | 17 | {
|
22 |
| - yield return SendWebRequest(request, options); |
| 18 | + yield return request.SendWebRequestWithOptions(options); |
23 | 19 | var response = request.CreateWebResponse();
|
24 | 20 | if (request.IsValidRequest(options))
|
25 | 21 | {
|
@@ -117,103 +113,5 @@ public static IEnumerator DefaultUnityWebRequest<TResponse>(RequestHelper option
|
117 | 113 | });
|
118 | 114 | }
|
119 | 115 |
|
120 |
| - /// <summary> |
121 |
| - /// Send the web request to the server |
122 |
| - /// </summary> |
123 |
| - /// <returns>An UnityWebRequestAsyncOperation object.</returns> |
124 |
| - /// <param name="request">An UnityWebRequest object.</param> |
125 |
| - /// <param name="options">An options object.</param> |
126 |
| - public static IEnumerator SendWebRequest(UnityWebRequest request, RequestHelper options) |
127 |
| - { |
128 |
| - byte[] bodyRaw = options.BodyRaw; |
129 |
| - string contentType = string.Empty; |
130 |
| - if (!options.Headers.TryGetValue(CONTENT_TYPE_HEADER, out contentType)) |
131 |
| - { |
132 |
| - contentType = CONTENT_TYPE_JSON; |
133 |
| - } |
134 |
| - if (options.Body != null || !string.IsNullOrEmpty(options.BodyString)) |
135 |
| - { |
136 |
| - var bodyString = options.BodyString; |
137 |
| - if (options.Body != null) |
138 |
| - { |
139 |
| - bodyString = JsonUtility.ToJson(options.Body); |
140 |
| - } |
141 |
| - bodyRaw = Encoding.UTF8.GetBytes(bodyString.ToCharArray()); |
142 |
| - } |
143 |
| - else if (options.SimpleForm != null && options.SimpleForm.Count > 0) |
144 |
| - { |
145 |
| - bodyRaw = UnityWebRequest.SerializeSimpleForm(options.SimpleForm); |
146 |
| - contentType = "application/x-www-form-urlencoded"; |
147 |
| - } |
148 |
| - else if (options.FormSections != null && options.FormSections.Count > 0) |
149 |
| - { |
150 |
| - byte[] boundary = UnityWebRequest.GenerateBoundary(); |
151 |
| - byte[] formSections = UnityWebRequest.SerializeFormSections(options.FormSections, boundary); |
152 |
| - byte[] terminate = Encoding.UTF8.GetBytes(string.Concat("\r\n--", Encoding.UTF8.GetString(boundary), "--")); |
153 |
| - bodyRaw = new byte[formSections.Length + terminate.Length]; |
154 |
| - System.Buffer.BlockCopy(formSections, 0, bodyRaw, 0, formSections.Length); |
155 |
| - System.Buffer.BlockCopy(terminate, 0, bodyRaw, formSections.Length, terminate.Length); |
156 |
| - contentType = string.Concat("multipart/form-data; boundary=", Encoding.UTF8.GetString(boundary)); |
157 |
| - } |
158 |
| - else if (options.FormData is WWWForm) |
159 |
| - { |
160 |
| - //The Content-Type header will be copied from the formData parameter |
161 |
| - contentType = string.Empty; |
162 |
| - } |
163 |
| - if (!string.IsNullOrEmpty(options.ContentType)) |
164 |
| - { |
165 |
| - contentType = options.ContentType; |
166 |
| - } |
167 |
| -#if UNITY_2018_1_OR_NEWER |
168 |
| - if (options.CertificateHandler is CertificateHandler) |
169 |
| - request.certificateHandler = options.CertificateHandler; |
170 |
| -#endif |
171 |
| - if (options.UploadHandler is UploadHandler) |
172 |
| - request.uploadHandler = options.UploadHandler; |
173 |
| - if (bodyRaw != null) |
174 |
| - { |
175 |
| - request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); |
176 |
| - request.uploadHandler.contentType = contentType; |
177 |
| - } |
178 |
| - if (options.DownloadHandler is DownloadHandler) |
179 |
| - request.downloadHandler = options.DownloadHandler; |
180 |
| - else |
181 |
| - request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); |
182 |
| - if (!string.IsNullOrEmpty(contentType)) |
183 |
| - { |
184 |
| - request.SetRequestHeader(CONTENT_TYPE_HEADER, contentType); |
185 |
| - } |
186 |
| - foreach (var header in RestClient.DefaultRequestHeaders) |
187 |
| - { |
188 |
| - request.SetRequestHeader(header.Key, header.Value); |
189 |
| - } |
190 |
| - foreach (var header in options.Headers) |
191 |
| - { |
192 |
| - request.SetRequestHeader(header.Key, header.Value); |
193 |
| - } |
194 |
| - if (options.Timeout.HasValue) |
195 |
| - { |
196 |
| - request.timeout = options.Timeout.Value; |
197 |
| - } |
198 |
| - if (options.ChunkedTransfer.HasValue) |
199 |
| - { |
200 |
| - request.chunkedTransfer = options.ChunkedTransfer.Value; |
201 |
| - } |
202 |
| - if (options.UseHttpContinue.HasValue) |
203 |
| - { |
204 |
| - request.useHttpContinue = options.UseHttpContinue.Value; |
205 |
| - } |
206 |
| - if (options.RedirectLimit.HasValue) |
207 |
| - { |
208 |
| - request.redirectLimit = options.RedirectLimit.Value; |
209 |
| - } |
210 |
| - options.Request = request; |
211 |
| -#if UNITY_2017_2_OR_NEWER |
212 |
| - yield return request.SendWebRequest(); |
213 |
| -#else |
214 |
| - yield return request.Send(); |
215 |
| -#endif |
216 |
| - } |
217 |
| - |
218 | 116 | }
|
219 | 117 | }
|
0 commit comments