コード例 #1
0
ファイル: BaseClient.cs プロジェクト: okproject/Pinch.SDK
        protected async Task <QuickResponse <T> > GetHttp <T>(string url)
        {
            try
            {
                await SetInitialToken();

                var response = await _client.GetAsync(url);

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    await GetToken();

                    response = await _client.GetAsync(url);
                }

                return(await QuickResponse <T> .FromMessage(response));
            }
            catch (Exception ex)
            {
                return(new QuickResponse <T>()
                {
                    Errors = new List <ApiError>()
                    {
                        new ApiError()
                        {
                            ErrorMessage = GetRecursiveErrorMessage(ex)
                        }
                    }
                });
            }
        }
コード例 #2
0
ファイル: AuthClient.cs プロジェクト: crumdev/Pinch.SDK
        public async Task <GetAccessTokenFromSecretKeyResponse> GetAccessTokenFromSecretKey(string secretKey, string clientId)
        {
            var client = new HttpClient()
            {
                BaseAddress = new Uri(_authUri)
            };

            client.DefaultRequestHeaders.Authorization = BasicAuthHeader.GetHeader(clientId, secretKey);

            var parameters = new Dictionary <string, string>()
            {
                { "grant_type", "client_credentials" },
                { "scope", "api1" }
            };

            var clientResponse = await client.PostAsync("connect/token", HttpClientHelpers.GetPostBody(parameters));

            var response = await QuickResponse <GetAccessTokenFromSecretKeyResponse> .FromMessage(clientResponse);

            if (response.Data?.AccessToken == null)
            {
                throw new Exception($"Could not get access token. Error: {response.ResponseBody}");
            }

            return(response.Data);
        }
コード例 #3
0
ファイル: BaseClient.cs プロジェクト: sdaarpad/Pinch.SDK
        private async Task <QuickResponse <T> > SendHttp <T>(Func <HttpRequestMessage> requestFunc)
        {
            try
            {
                var request = requestFunc();
                await SetAuthHeader(request, false);

                var response = await _httpClientFactory().SendAsync(request);

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    request = requestFunc();
                    await SetAuthHeader(request, true);

                    response = await _httpClientFactory().SendAsync(request);
                }

                return(await QuickResponse <T> .FromMessage(response));
            }
            catch (Exception ex)
            {
                return(new QuickResponse <T>()
                {
                    Errors = new List <ApiError>()
                    {
                        new ApiError()
                        {
                            ErrorMessage = GetRecursiveErrorMessage(ex)
                        }
                    }
                });
            }
        }
コード例 #4
0
ファイル: BaseClient.cs プロジェクト: okproject/Pinch.SDK
        protected async Task <QuickResponse <T> > PostHttp <T>(string url, object data)
        {
            await SetInitialToken();

            var response = await _client.PostAsync(url, HttpClientHelpers.GetJsonBody(data));

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                await GetToken();

                response = await _client.PostAsync(url, HttpClientHelpers.GetJsonBody(data));
            }

            return(await QuickResponse <T> .FromMessage(response));
        }
コード例 #5
0
ファイル: BaseClient.cs プロジェクト: okproject/Pinch.SDK
        protected async Task <QuickResponse <T> > PostHttp <T>(string url, Dictionary <string, string> parameters)
        {
            await SetInitialToken();

            var response = await _client.PostAsync(url, HttpClientHelpers.GetPostBody(parameters));

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                await GetToken();

                response = await _client.PostAsync(url, HttpClientHelpers.GetPostBody(parameters));
            }

            return(await QuickResponse <T> .FromMessage(response));
        }
コード例 #6
0
ファイル: BaseClient.cs プロジェクト: okproject/Pinch.SDK
        protected async Task <QuickResponse> DeleteHttp(string url)
        {
            await SetInitialToken();

            var response = await _client.DeleteAsync(url);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                await GetToken();

                response = await _client.DeleteAsync(url);
            }

            return(await QuickResponse.FromMessage(response));
        }
コード例 #7
0
        public async Task <GetAccessTokenFromSecretKeyResponse> GetAccessToken()
        {
            var client = new HttpClient
            {
                BaseAddress = new Uri(_authUri)
            };

            client.DefaultRequestHeaders.Authorization = BasicAuthHeader.GetHeader(_consumerKey, _consumerSecret);

            var clientResponse = await client.GetAsync("/oauth/v1/generate?grant_type=client_credentials");

            var response = await QuickResponse <GetAccessTokenFromSecretKeyResponse> .FromMessage(clientResponse);

            if (response.Data?.AccessToken == null)
            {
                throw new Exception($"Could not get access token. Error: {response.ResponseBody}");
            }

            return(response.Data);
        }