/// <summary> /// Sends request to the server and deserializes response to an instance of <typeparamref name="TResult" /> using JSON serializer. /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="uriParts">The URI parts.</param> /// <param name="httpMethod">The HTTP method.</param> /// <param name="requestHeaders">The request headers.</param> /// <param name="streamProvider">The stream provider.</param> /// <param name="streamContentType">The stream content type.</param> /// <param name="cancellationToken">The cancellation token.</param> public Task <TResult> SendWithStreamBodyAsync <TResult>( UriParts uriParts, HttpMethod httpMethod, IEnumerable <KeyValuePair <string, string> > requestHeaders, Func <CancellationToken, Task <Stream> > streamProvider, string streamContentType, CancellationToken cancellationToken) { return(this.SendInternalAsync <TResult>( uriParts, httpMethod, requestHeaders, innerCt => CreateBase64StreamContentAsync(streamProvider, streamContentType, innerCt), cancellationToken)); }
private Task <T> SendInternalAsync <T>( UriParts uriParts, HttpMethod httpMethod, IEnumerable <KeyValuePair <string, string> > requestHeaders, Func <CancellationToken, Task <HttpContent> > requestContentProvider, CancellationToken cancellationToken) { return(this.httpClientWrapper.SendAsync( new HttpRequest <T>( new TransformableUriBuilder(transformersSourceValues => GetCompleteUri(transformersSourceValues, uriParts)), httpMethod, requestHeaders.EmptyIfNull().ToList().AsReadOnly(), requestContentProvider, async(response, innerCt) => { var matchingAttribute = typeof(T).GetTypeInfo().GetCustomAttributes <HttpStatusCodeToExceptionAttribute>().FirstOrDefault(item => item.StatusCode == response.StatusCode); if (matchingAttribute != null) { throw (Exception)Activator.CreateInstance(matchingAttribute.ExceptionType); } if (response.Content == null) { return default(T); } using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) using (var streamReader = new StreamReader(stream)) using (var reader = new JsonTextReader(streamReader)) { var serializer = new JsonSerializer(); return serializer.Deserialize <T>(reader); } }), cancellationToken)); }
private static Uri GetCompleteUri(IList <KeyValuePair <Type, object> > transformersSourceValues, UriParts uriParts) { var relativeUri = string.Join("/", uriParts.RouteValues.EmptyIfNull().Select(item => GetOrTransform(item, transformersSourceValues)).Select(item => item.ToUrlString())); var uriBuilder = new UriBuilder(new Uri(uriParts.BaseUri, relativeUri)); var queryString = SpotifyObjectHelpers.GetPropertyBag(uriParts.QueryStringParameters) .Select(item => new KeyValuePair <string, object>(item.Key, GetOrTransform(item.Value, transformersSourceValues))) .GetQueryString(); if (!string.IsNullOrEmpty(queryString)) { uriBuilder.Query = queryString; } return(uriBuilder.Uri); }