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));
        }
Esempio n. 2
0
        public void ExtractAuthorizationStateFromUri_Error()
        {
            // ARRANGE
            Mock.Arrange(() => Arg.IsAny <Uri>().MustNotNull(Arg.AnyString)).DoNothing().Occurs(1);
            Mock.Arrange(() => OAuthErrorParser.ParseError(Arg.AnyString)).Throws(new DracoonApiException());
            Uri param = new Uri("https://dracoon.team/oauth/callback?error=someOccuredError");

            // ACT - ASSERT
            Assert.Throws <DracoonApiException>(() => OAuthHelper.ExtractAuthorizationStateFromUri(param));
            Mock.Assert(() => Arg.IsAny <Uri>().MustNotNull(Arg.AnyString));
        }
Esempio n. 3
0
        internal void ParseError_String(string content, int expectedSdkErrorCode)
        {
            // ARRANGE

            try {
                // ACT
                OAuthErrorParser.ParseError(content);
            } catch (DracoonApiException dae) {
                // ASSERT
                Assert.Equal(expectedSdkErrorCode, dae.ErrorCode.Code);
            }
        }
Esempio n. 4
0
        private static string ExtractAuthorizationDataFromUri(Uri uri, string requestedType)
        {
            uri.MustNotNull(nameof(uri));

            NameValueCollection queryElements = HttpUtility.ParseQueryString(uri.Query);

            if (queryElements["error"] != null)
            {
                OAuthErrorParser.ParseError(queryElements["error"]);
            }

            return(queryElements[requestedType]);
        }
Esempio n. 5
0
        internal void ParseError_IRestResponse(RequestType type, string content, HttpStatusCode statusCode, int expectedSdkErrorCode)
        {
            // ARRANGE
            IRestResponse response = Mock.Create <IRestResponse>();

            Mock.Arrange(() => response.Content).Returns(GenerateJsonError(content));
            Mock.Arrange(() => response.StatusCode).Returns(statusCode);

            try {
                // ACT
                OAuthErrorParser.ParseError(response, type);
            } catch (DracoonApiException dae) {
                // ASSERT
                Assert.Equal(expectedSdkErrorCode, dae.ErrorCode.Code);
            }
        }
Esempio n. 6
0
        public void DoSyncApiCall_OAuth_Fail()
        {
            // ARRANGE
            RestResponse response = FactoryRestSharp.RestResponse;
            IRestRequest request  = FactoryRestSharp.PostOAuthTokenMock("id1", "secret1", "grant", "code1");

            Mock.Arrange(() => DracoonClient.HttpConfig).Returns(new DracoonHttpConfig());
            Mock.Arrange(() => new RestClient().Execute(request)).IgnoreInstance().Returns(response);
            Mock.Arrange(() => OAuthErrorParser.ParseError(response, RequestType.PostOAuthToken)).Throws(new DracoonApiException());

            IInternalDracoonClient c    = FactoryClients.InternalDracoonClientMock();
            IRequestExecutor       exec = new DracoonRequestExecutor(FactoryClients.OAuthMock, c);

            // ACT - ASSERT
            Assert.Throws <DracoonApiException>(() => exec.DoSyncApiCall <ApiOAuthToken>(request, RequestType.PostOAuthToken));
        }