public async Task <FullTokensResponse> GetNewTokensFromOneTimeRefreshTokenAsync(string refreshToken, CancellationToken cancellationToken)
        {
            var tokenClientOptions = this.tokenClientOptionsProvider.Get();

            var formParams = new Dictionary <string, object>
            {
                ["grant_type"]    = "refresh_token",
                ["refresh_token"] = refreshToken,
                ["client_id"]     = tokenClientOptions.ClientId,
            };

            try
            {
                return(await SpotifyHttpUtils.HandleTimeoutAsync <ISpotifyTokenClient, FullTokensResponse>(
                           async innerCt =>
                {
                    using (var response = await this.httpClientFactory.CreateClient().PostAsync(null as Uri, this.GetFormUrlEncodedContent(formParams), innerCt).ConfigureAwait(false))
                    {
                        return await response.Content.ReadFromJsonAsync <FullTokensResponse>(cancellationToken: innerCt).ConfigureAwait(false);
                    }
                },
                           cancellationToken).ConfigureAwait(false));
            }
            catch (SpotifyAuthenticationErrorException e) when(this.IsInvalidGrantException(e))
            {
                throw new SpotifyInvalidRefreshTokenException(e.ErrorCode, e.Content, e.Error);
            }
        }
        public async Task <ScopedAccessTokenResponse> GetAccessTokenFromRefreshTokenAsync(string refreshToken, CancellationToken cancellationToken)
        {
            var tokenClientOptions = this.tokenClientOptionsProvider.Get();

            var formParams = new Dictionary <string, object>
            {
                ["grant_type"]    = "refresh_token",
                ["refresh_token"] = refreshToken
            };

            using (var request = new HttpRequestMessage())
            {
                request.Method  = HttpMethod.Post;
                request.Content = this.GetFormUrlEncodedContent(formParams);
                request.Headers.Authorization = this.GetAuthorizationHeader(tokenClientOptions);

                try
                {
                    return(await SpotifyHttpUtils.HandleTimeoutAsync <ISpotifyTokenClient, ScopedAccessTokenResponse>(
                               async innerCt =>
                    {
                        using (var response = await this.httpClientFactory.CreateClient().SendAsync(request, innerCt).ConfigureAwait(false))
                        {
                            return await response.Content.ReadFromJsonAsync <ScopedAccessTokenResponse>(cancellationToken: innerCt).ConfigureAwait(false);
                        }
                    },
                               cancellationToken).ConfigureAwait(false));
                }
                catch (SpotifyAuthenticationErrorException e) when(this.IsInvalidGrantException(e))
                {
                    throw new SpotifyInvalidRefreshTokenException(e.ErrorCode, e.Content, e.Error);
                }
            }
        }
        public async Task <AccessTokenResponse> GetAccessTokenFromClientCredentialsAsync(CancellationToken cancellationToken)
        {
            var tokenClientOptions = this.tokenClientOptionsProvider.Get();

            var formParams = new Dictionary <string, object>
            {
                ["grant_type"] = "client_credentials"
            };

            using (var request = new HttpRequestMessage())
            {
                request.Method  = HttpMethod.Post;
                request.Content = this.GetFormUrlEncodedContent(formParams);
                request.Headers.Authorization = this.GetAuthorizationHeader(tokenClientOptions);

                return(await SpotifyHttpUtils.HandleTimeoutAsync <ISpotifyTokenClient, AccessTokenResponse>(
                           async innerCt =>
                {
                    using (var response = await this.httpClientFactory.CreateClient().SendAsync(request, innerCt).ConfigureAwait(false))
                    {
                        return await response.Content.ReadFromJsonAsync <AccessTokenResponse>(cancellationToken: innerCt).ConfigureAwait(false);
                    }
                },
                           cancellationToken).ConfigureAwait(false));
            }
        }
Beispiel #4
0
        protected async Task <TResult> SendBodyAsync <TRequestBody, TResult>(
            HttpMethod httpMethod,
            TRequestBody requestBody,
            CancellationToken cancellationToken,
            IEnumerable <object> additionalRouteValues = null,
            object queryParams = null)
        {
            var relativeUri = await this.GetRelativeUriAsync(additionalRouteValues, queryParams, cancellationToken).ConfigureAwait(false);

            var content = JsonContent.Create(requestBody, options: JsonOptions);

            using (var request = new HttpRequestMessage(httpMethod, relativeUri))
            {
                request.Content = content;

                return(await SpotifyHttpUtils.HandleTimeoutAsync <IFluentSpotifyClient, TResult>(
                           async innerCt =>
                {
                    using (var response = await this.contextData.HttpClientFactory.CreateClient().SendAsync(request, innerCt).ConfigureAwait(false))
                    {
                        return await response.Content.ReadFromJsonAsync <TResult>(JsonOptions, innerCt).ConfigureAwait(false);
                    }
                },
                           cancellationToken).ConfigureAwait(false));
            }
        }
Beispiel #5
0
        protected async Task <TResult> GetAsync <TResult>(
            CancellationToken cancellationToken,
            IEnumerable <object> additionalRouteValues = null,
            object queryParams = null)
        {
            var relativeUri = await this.GetRelativeUriAsync(additionalRouteValues, queryParams, cancellationToken).ConfigureAwait(false);

            return(await SpotifyHttpUtils.HandleTimeoutAsync <IFluentSpotifyClient, TResult>(
                       async innerCt => await this.contextData.HttpClientFactory.CreateClient().GetFromJsonAsync <TResult>(relativeUri, JsonOptions, innerCt).ConfigureAwait(false),
                       cancellationToken).ConfigureAwait(false));
        }
Beispiel #6
0
        protected async Task SendAsync(
            HttpMethod httpMethod,
            CancellationToken cancellationToken,
            IEnumerable <object> additionalRouteValues = null,
            object queryParams = null)
        {
            var relativeUri = await this.GetRelativeUriAsync(additionalRouteValues, queryParams, cancellationToken).ConfigureAwait(false);

            using (var request = new HttpRequestMessage(httpMethod, relativeUri))
            {
                await SpotifyHttpUtils.HandleTimeoutAsync <IFluentSpotifyClient>(
                    async innerCt => (await this.contextData.HttpClientFactory.CreateClient().SendAsync(request, innerCt).ConfigureAwait(false)).Dispose(),
                    cancellationToken).ConfigureAwait(false);
            }
        }
        public async Task <PrivateUser> GetCurrentUserAsync(string accessToken, CancellationToken cancellationToken)
        {
            using (var request = new HttpRequestMessage())
            {
                request.Method = HttpMethod.Get;
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                return(await SpotifyHttpUtils.HandleTimeoutAsync <ISpotifyUserClient, PrivateUser>(
                           async innerCt =>
                {
                    using (var response = await this.httpClientFactory.CreateClient().SendAsync(request, innerCt).ConfigureAwait(false))
                    {
                        return await response.Content.ReadFromJsonAsync <PrivateUser>(cancellationToken: innerCt).ConfigureAwait(false);
                    }
                },
                           cancellationToken).ConfigureAwait(false));
            }
        }
        public async Task <FullTokensResponse> GetTokensFromAuthorizationResultAsync(string authorizationCode, Uri redirectUri, CancellationToken cancellationToken)
        {
            var formParams = new Dictionary <string, object>
            {
                ["grant_type"]   = "authorization_code",
                ["code"]         = authorizationCode,
                ["redirect_uri"] = redirectUri
            };

            return(await SpotifyHttpUtils.HandleTimeoutAsync <ISpotifyTokenClient, FullTokensResponse>(
                       async innerCt =>
            {
                using (var response = await this.httpClientFactory.CreateClient().PostAsync(null as Uri, this.GetFormUrlEncodedContent(formParams), innerCt).ConfigureAwait(false))
                {
                    return await response.Content.ReadFromJsonAsync <FullTokensResponse>(cancellationToken: innerCt).ConfigureAwait(false);
                }
            },
                       cancellationToken).ConfigureAwait(false));
        }
Beispiel #9
0
        protected async Task SendJpegAsync(
            HttpMethod httpMethod,
            byte[] jpeg,
            CancellationToken cancellationToken,
            IEnumerable <object> additionalRouteValues = null,
            object queryParams = null)
        {
            var relativeUri = await this.GetRelativeUriAsync(additionalRouteValues, queryParams, cancellationToken).ConfigureAwait(false);

            var content = new StringContent(Convert.ToBase64String(jpeg));

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");

            using (var request = new HttpRequestMessage(httpMethod, relativeUri))
            {
                request.Content = content;

                await SpotifyHttpUtils.HandleTimeoutAsync <IFluentSpotifyClient>(
                    async innerCt => (await this.contextData.HttpClientFactory.CreateClient().SendAsync(request, innerCt).ConfigureAwait(false)).Dispose(),
                    cancellationToken).ConfigureAwait(false);
            }
        }
 public async Task <T> GetAsync <T>(Uri url, CancellationToken cancellationToken)
 {
     return(await SpotifyHttpUtils.HandleTimeoutAsync <IFluentSpotifyClient, T>(
                async innerCt => await this.contextData.HttpClientFactory.CreateClient().GetFromJsonAsync <T>(url, innerCt).ConfigureAwait(false),
                cancellationToken).ConfigureAwait(false));
 }