示例#1
0
 private void OnTooManyRequests(ResponseErrorEventArgs e) => TooManyRequests?.Invoke(this, e);
示例#2
0
 private void OnAuthorizationFailed(ResponseErrorEventArgs e) => AuthorizationFailed?.Invoke(this, e);
示例#3
0
 private void OnCaptchaNeeded(ResponseErrorEventArgs e) => CaptchaNeeded?.Invoke(this, e);
示例#4
0
        private async Task <Response <T> > RequestInternal <T>(string methodName, Dictionary <string, string> methodParams,
                                                               bool isOpenMethod, string path, int retryCount)
        {
            if (!isOpenMethod)
            {
                if (Session == null)
                {
                    throw new NullReferenceException(
                              "The session is not set. You need to authorize to get the new session.");
                }

                if (Session.IsExpired)
                {
                    throw new InTouchException(
                              "The session is dead. You need to obtain new access token to perform API calls.");
                }
            }

            if (_apiClient == null)
            {
                InitApiClient();
            }

            var normalizedParams = NormalizeRequestParams(methodParams, isOpenMethod);

            CacheReqData(methodName, normalizedParams, isOpenMethod, path);

            var json = await Post($"method/{methodName}", normalizedParams);

            try
            {
                return(await Task.Run(() => ParseJsonReponse <T>(json, path)));
            }
            catch (InTouchResponseErrorException ex)
            {
                var args = new ResponseErrorEventArgs(ex.ResponseError, retryCount);

                switch (ex.ResponseError.Code)
                {
                case 5:
                    OnAuthorizationFailed(args);
                    break;

                case 6:
                    OnTooManyRequests(args);
                    break;

                case 14:
                    OnCaptchaNeeded(args);
                    break;
                }

                if (args.Handled)
                {
                    if (retryCount > 5)
                    {
                        throw new InTouchException("Too many retries for request, cancelling");
                    }
                    // Retry request
                    return(await RequestInternal <T>(methodName, methodParams, isOpenMethod, path, retryCount + 1));
                }
                else
                {
                    throw;
                }
            }
        }