Beispiel #1
0
        public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action <RequestException, ResponseHelper> callback)
        {
            var retries = 0;

            do
            {
                using (var request = CreateRequest(options))
                {
                    bool IsNetworkError;
#if UNITY_2020_2_OR_NEWER
                    IsNetworkError = (request.result == UnityWebRequest.Result.ConnectionError);
#else
                    IsNetworkError = request.isNetworkError;
#endif
                    var sendRequest = request.SendWebRequestWithOptions(options);
                    if (options.ProgressCallback == null)
                    {
                        yield return(sendRequest);
                    }
                    else
                    {
                        options.ProgressCallback(0);

                        while (!sendRequest.isDone)
                        {
                            options.ProgressCallback(sendRequest.progress);
                            yield return(null);
                        }

                        options.ProgressCallback(1);
                    }
                    var response = request.CreateWebResponse();
                    if (request.IsValidRequest(options))
                    {
                        DebugLog(options.EnableDebug, string.Format("RestClient - Response\nUrl: {0}\nMethod: {1}\nStatus: {2}\nResponse: {3}", options.Uri, options.Method, request.responseCode, options.ParseResponseBody ? response.Text : "body not parsed"), false);
                        callback(null, response);
                        break;
                    }
                    else if (!options.IsAborted && retries < options.Retries && (!options.RetryCallbackOnlyOnNetworkErrors || IsNetworkError))
                    {
                        if (options.RetryCallback != null)
                        {
                            options.RetryCallback(CreateException(options, request), retries);
                        }
                        yield return(new WaitForSeconds(options.RetrySecondsDelay));

                        retries++;
                        DebugLog(options.EnableDebug, string.Format("RestClient - Retry Request\nUrl: {0}\nMethod: {1}", options.Uri, options.Method), false);
                    }
                    else
                    {
                        var err = CreateException(options, request);
                        DebugLog(options.EnableDebug, err, true);
                        callback(err, response);
                        break;
                    }
                }
            }while (retries <= options.Retries);
        }
Beispiel #2
0
        public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action <RequestException, ResponseHelper> callback)
        {
            var retries = 0;

            do
            {
                using (var request = CreateRequest(options))
                {
                    yield return(request.SendWebRequestWithOptions(options));

                    var response = request.CreateWebResponse();
                    if (request.IsValidRequest(options))
                    {
                        if (options.EnableDebug)
                        {
                            Debug.LogFormat("RestClient - Response\nUrl: {0}\nMethod: {1}\nStatus: {2}\nResponse: {3}", options.Uri, options.Method, request.responseCode, options.ParseResponseBody ? response.Text : "body not parsed");
                        }
                        callback(null, response);
                        break;
                    }
                    else if (!options.IsAborted && retries < options.Retries)
                    {
                        yield return(new WaitForSeconds(options.RetrySecondsDelay));

                        retries++;
                        if (options.RetryCallback != null)
                        {
                            options.RetryCallback(CreateException(options, request), retries);
                        }
                        if (options.EnableDebug)
                        {
                            Debug.LogFormat("RestClient - Retry Request\nUrl: {0}\nMethod: {1}", options.Uri, options.Method);
                        }
                    }
                    else
                    {
                        var err = CreateException(options, request);
                        if (options.EnableDebug)
                        {
                            Debug.LogError(err);
                        }
                        callback(err, response);
                        break;
                    }
                }
            }while (retries <= options.Retries);
        }
Beispiel #3
0
        public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action <RequestException, ResponseHelper> callback)
        {
            var retries = 0;

            do
            {
                using (var request = CreateRequest(options))
                {
                    yield return(request.SendWebRequestWithOptions(options));

                    var response = request.CreateWebResponse();
                    //                    Debug.Log("HTTP:" + response.Text);
                    if (request.IsValidRequest(options))
                    {
                        //Debug.Log("1");
                        DebugLog(options.EnableDebug, string.Format("Url: {0}\nMethod: {1}\nStatus: {2}\nResponse: {3}", options.Uri, options.Method, request.responseCode, response.Text), false);
                        callback(null, response);
                        break;
                    }
                    else if (!options.IsAborted && retries < options.Retries)
                    {
                        yield return(new WaitForSeconds(options.RetrySecondsDelay));

                        retries++;
                        //Debug.Log("2");
                        if (options.RetryCallback != null)
                        {
                            options.RetryCallback(CreateException(request), retries);
                        }
                        DebugLog(options.EnableDebug, string.Format("Retry Request\nUrl: {0}\nMethod: {1}", options.Uri, options.Method), false);
                    }
                    else
                    {
                        //Debug.Log("3");
                        var err = CreateException(request);



                        DebugLog(options.EnableDebug, err, true);
                        callback(err, response);
                        break;
                    }
                }
            }while (retries <= options.Retries);
        }
Beispiel #4
0
        public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action <RequestException, ResponseHelper> callback)
        {
            var retries = 0;

            do
            {
                using (var request = CreateRequest(options))
                {
                    yield return(request.SendWebRequestWithOptions(options));

                    if (IsInGame())
                    {
                        break;
                    }
                    var response = request.CreateWebResponse();
                    if (request.IsValidRequest(options))
                    {
                        if (options.EnableDebug)
                        {
                            DebugLog(options.EnableDebug,
                                     string.Format("Url: {0}\nMethod: {1}\nStatus: {2}\nResponse: {3}", options.Uri,
                                                   options.Method, request.responseCode, response.Text), false);
                        }

                        callback(null, response);
                        break;
                    }
                    else if (!options.IsAborted && retries < options.Retries)
                    {
                        yield return(new WaitForSeconds(options.RetrySecondsDelay));

                        if (IsInGame())
                        {
                            break;             // EDIT: Cytoid
                        }
                        retries++;
                        if (options.RetryCallback != null)
                        {
                            options.RetryCallback(CreateException(request), retries);
                        }
                        // EDIT: Cytoid
                        if (options.Retries == 0)
                        {
                            var err = CreateException(request);
                            DebugLog(options.EnableDebug, $"Error: {err}, Url: {options.Uri}, Body: {(request.uploadHandler != null ? Encoding.UTF8.GetString(request.uploadHandler.data) : "null")}, Response: {response.Text}", true);
                            callback(err, response);
                            break;
                        }
                        // End of EDIT
                        DebugLog(options.EnableDebug, string.Format("Retry Request\nUrl: {0}\nMethod: {1}", options.Uri, options.Method), false);
                    }
                    else
                    {
                        var err = CreateException(request);
                        DebugLog(options.EnableDebug, $"Error: {err}, Url: {options.Uri}, Body: {(request.uploadHandler != null ? Encoding.UTF8.GetString(request.uploadHandler.data) : "null")}, Response: {response.Text}", true);
                        callback(err, response);
                        break;
                    }
                }
                options.Request = null;
            }while (retries <= options.Retries);
        }