コード例 #1
0
        private void SetGeneralRestValues(IRestRequest request, bool requiresAuth, object optionalJsonBody = null)
        {
            if (requiresAuth)
            {
                request.AddHeader(ApiConfig.AuthorizationHeader, _auth.BuildAuthString());
            }

            if (optionalJsonBody != null)
            {
                request.AddParameter("application/json", JsonConvert.SerializeObject(optionalJsonBody), ParameterType.RequestBody);
            }

            request.ReadWriteTimeout = DracoonClient.HttpConfig.ReadWriteTimeout;
            request.Timeout          = DracoonClient.HttpConfig.ConnectionTimeout;
        }
コード例 #2
0
        T IRequestExecutor.DoSyncApiCall <T>(IRestRequest request, RequestType requestType, int authTry = 0)
        {
            IRestClient client = new RestClient(_client.ServerUri)
            {
                UserAgent = DracoonClient.HttpConfig.UserAgent,
            };

            if (DracoonClient.HttpConfig.WebProxy != null)
            {
                client.Proxy = DracoonClient.HttpConfig.WebProxy;
            }

            IRestResponse response = client.Execute(request);

            if (response.ErrorException is WebException we)
            {
                // It's an HTTP exception
                DracoonErrorParser.ParseError(we, requestType);
            }

            if (!response.IsSuccessful)
            {
                // It's an API exception
                if (RequestIsOAuthRequest(requestType))
                {
                    OAuthErrorParser.ParseError(response, requestType);
                }

                try {
                    DracoonErrorParser.ParseError(response, requestType);
                } catch (DracoonApiException apiError) {
                    if (apiError.ErrorCode == DracoonApiCode.AUTH_UNAUTHORIZED && authTry < 3)
                    {
                        DracoonClient.Log.Debug(Logtag, "Retry the refresh of the access token in " + authTry * 1000 + " millis again.");
                        Thread.Sleep(1000 * authTry);
                        _auth.RefreshAccessToken();
                        foreach (Parameter cur in request.Parameters)
                        {
                            if (cur.Name == ApiConfig.AuthorizationHeader)
                            {
                                cur.Value = _auth.BuildAuthString();
                            }
                        }

                        return(((IRequestExecutor)this).DoSyncApiCall <T>(request, requestType, authTry + 1));
                    }

                    throw apiError;
                }
            }

            if (typeof(T) == typeof(VoidResponse))
            {
                return(new VoidResponse() as T);
            }

            return(JsonConvert.DeserializeObject <T>(response.Content));
        }