public async Task <HttpResponseMessage> SendHttpRequestAsync(PartyGoer user, SpotifyEndpoint spotifyEndpoint, ApiParameters queryStringParameters, object requestBodyParameters)
        {
            await RefreshTokenForUserAsync(user.GetId());

            string spotifyEndpointUrl = spotifyEndpoint.EndpointUrl;

            // look to see if spotifyendpoint has keys
            if (spotifyEndpoint?.Keys != null)
            {
                // we need to verify these keys exist in api parameters
                foreach (string key in spotifyEndpoint.Keys)
                {
                    if (!queryStringParameters.Keys.ContainsKey(key))
                    {
                        throw new Exception($"Endpoint: {spotifyEndpoint} says it contains a key: {key} but it is not found in parameters");
                    }

                    spotifyEndpointUrl = spotifyEndpointUrl.Replace(key, queryStringParameters.Keys[key]);
                }
            }

            using (var requestMessage = new HttpRequestMessage(spotifyEndpoint.HttpMethod, spotifyEndpointUrl + AddQueryStrings(queryStringParameters)))
            {
                requestMessage.Headers.Authorization = await _spotifyAuthentication.GetAuthenticationHeaderForPartyGoerAsync(user.GetId());

                if (requestBodyParameters != null)
                {
                    requestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestBodyParameters));
                }

                return(await _httpClient.SendAsync(requestMessage));
            }
        }
        private async Task <HttpResponseMessage> SendHttpRequestAsync(PartyGoer user, SpotifyEndpoint spotifyEndpoint, ApiParameters queryStringParameters, object requestBodyParameters)
        {
            await RefreshTokenForUserAsync(user.Id);

            using (var requestMessage = new HttpRequestMessage(spotifyEndpoint.HttpMethod, spotifyEndpoint.EndpointUrl + AddQueryStrings(queryStringParameters)))
            {
                requestMessage.Headers.Authorization = await _spotifyAuthentication.GetAuthenticationHeaderForPartyGoerAsync(user.Id);

                if (requestBodyParameters != null)
                {
                    requestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestBodyParameters));
                }

                return(await _httpClient.SendAsync(requestMessage));
            }
        }
Beispiel #3
0
        private async Task <HttpResponseMessage> SendHttpRequestAsync(string spotifyId, SpotifyEndpoint spotifyEndpoint, object content = null, bool useQueryString = false)
        {
            await RefreshTokenForUserAsync(spotifyId);

            using (var requestMessage = new HttpRequestMessage(spotifyEndpoint.HttpMethod, spotifyEndpoint.EndpointUrl + (useQueryString ? $"?{content}" : string.Empty)))
            {
                requestMessage.Headers.Authorization = await _spotifyAuthentication.GetAuthenticationHeaderForPartyGoerAsync(spotifyId);

                if (content != null)
                {
                    if (!useQueryString)
                    {
                        requestMessage.Content = new StringContent(JsonConvert.SerializeObject(content));
                    }
                }


                return(await _httpClient.SendAsync(requestMessage));
            }
        }