Пример #1
0
 private static RequestException CreateException(RequestHelper options, UnityWebRequest request)
 {
     return(new RequestException(request.error, request.isHttpError, request.isNetworkError, request.responseCode, options.ParseResponseBody ? request.downloadHandler.text : "body not parsed"));
 }
Пример #2
0
 public static IEnumerator DefaultUnityWebRequest(RequestHelper options, Action <RequestException, ResponseHelper> callback)
 {
     return(CreateRequestAndRetry(options, callback));
 }
Пример #3
0
 /// <summary>
 /// Load data from the server using a HTTP GET request
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 /// <typeparam name="T">The element type of the response.</typeparam>
 public static void Get <T>(RequestHelper options, Action <Exception, ResponseHelper, T> callback)
 {
     options.Method = UnityWebRequest.kHttpVerbGET;
     Request(options, callback);
 }
Пример #4
0
        /// <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(UnityWebRequest request, RequestHelper options)
        {
            byte[] bodyRaw = options.BodyRaw;
            string contentType = string.Empty;
            if (!options.Headers.TryGetValue(CONTENT_TYPE_HEADER, out contentType))
            {
                contentType = CONTENT_TYPE_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));
            }
            else if (options.FormData is WWWForm)
            {
                //The Content-Type header will be copied from the formData parameter
                contentType = string.Empty;
            }
            if (!string.IsNullOrEmpty(options.ContentType))
            {
                contentType = options.ContentType;
            }
#if UNITY_2018_1_OR_NEWER
            if (options.CertificateHandler is CertificateHandler)
                request.certificateHandler = options.CertificateHandler;
#endif
            if (options.UploadHandler is UploadHandler)
                request.uploadHandler = options.UploadHandler;
            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();
            if (!string.IsNullOrEmpty(contentType))
            {
                request.SetRequestHeader(CONTENT_TYPE_HEADER, 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;
            }
            if (options.UseHttpContinue.HasValue)
            {
                request.useHttpContinue = options.UseHttpContinue.Value;
            }
            if (options.RedirectLimit.HasValue)
            {
                request.redirectLimit = options.RedirectLimit.Value;
            }
            options.Request = request;
#if UNITY_2017_2_OR_NEWER
            yield return request.SendWebRequest();
#else
            yield return request.Send();
#endif
        }
Пример #5
0
 /// <summary>
 /// Delete the specified resource identified by the URI.
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 public static void Delete(RequestHelper options, Action <Exception, ResponseHelper> callback)
 {
     StaticCoroutine.StartCoroutine(HttpDelete.DeleteUnityWebRequest(options, callback));
 }
Пример #6
0
 /// <summary>
 /// Request the specified options and callback.
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 /// <typeparam name="T">The element type of the response.</typeparam>
 public static void Request <T>(RequestHelper options, Action <Exception, ResponseHelper, T> callback)
 {
     StaticCoroutine.StartCoroutine(HttpBase.DefaultUnityWebRequest(options, callback));
 }
Пример #7
0
 /// <summary>
 /// Load data from the server using a HTTP PUT request.
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="bodyJson">A plain object that is sent to the server with the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 /// <typeparam name="T">The element type of the response.</typeparam>
 public static void Put <T>(RequestHelper options, object bodyJson, Action <Exception, ResponseHelper, T> callback)
 {
     options.Method = UnityWebRequest.kHttpVerbPUT;
     options.Body   = bodyJson;
     Request(options, callback);
 }
Пример #8
0
 /// <summary>
 /// Load a JSON array from the server using a HTTP GET request
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 /// <typeparam name="T">The element type of the array.</typeparam>
 public static void GetArray <T>(RequestHelper options, Action <Exception, T[]> callback)
 {
     StaticCoroutine.StartCoroutine(HttpGet.GetArrayUnityWebRequest <T>(options, callback));
 }
Пример #9
0
 /// <summary>
 /// Request the headers that are returned from the server
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 public static void Head(RequestHelper options, Action <RequestException, ResponseHelper> callback)
 {
     options.Method = UnityWebRequest.kHttpVerbHEAD;
     Request(options, callback);
 }
Пример #10
0
 /// <summary>
 /// Delete the specified resource identified by the URI.
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 public static void Delete(RequestHelper options, Action <RequestException, ResponseHelper> callback)
 {
     options.Method = UnityWebRequest.kHttpVerbDELETE;
     Request(options, callback);
 }
Пример #11
0
 /// <summary>
 /// Load a JSON array from the server using a HTTP GET request
 /// </summary>
 /// <param name="options">The options of the request.</param>
 /// <param name="callback">A callback function that is executed when the request is finished.</param>
 /// <typeparam name="T">The element type of the array.</typeparam>
 public static void GetArray <T>(RequestHelper options, Action <RequestException, ResponseHelper, T[]> callback)
 {
     options.Method = UnityWebRequest.kHttpVerbGET;
     StaticCoroutine.StartCoroutine(HttpBase.DefaultUnityWebRequest <T>(options, callback));
 }