public virtual async Task <long> ContentLengthRequestAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null) { var requestUri = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters); var clientHandler = GetClientHandler(); var client = GetClient(clientHandler); long length = -1; cancelToken?.ThrowIfCancellationRequested(); try { using (var message = new HttpRequestMessage(System.Net.Http.HttpMethod.Head, requestUri)) using (var response = cancelToken.HasValue ? await client.SendAsync(message, cancelToken.Value) : await client.SendAsync(message)) { if (!response.IsSuccessStatusCode) { throw new SubsonicApiException(string.Format(CultureInfo.CurrentCulture, "Unexpected response status code: {0}", Enum.GetName(typeof(ItemChoiceType), response.StatusCode))); } if (response.Content.Headers.ContentLength != null) { length = response.Content.Headers.ContentLength.GetValueOrDefault(); } } } catch (Exception ex) { throw new SubsonicApiException(ex.Message, ex); } cancelToken?.ThrowIfCancellationRequested(); return(length); }
public virtual async Task <IImageFormat <T> > ImageRequestAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null) { var requestUri = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters); var clientHandler = GetClientHandler(); var client = GetClient(clientHandler); cancelToken?.ThrowIfCancellationRequested(); var content = new MemoryStream(); try { using (var response = cancelToken.HasValue ? await client.GetAsync(requestUri, cancelToken.Value) : await client.GetAsync(requestUri)) { if (response.Content.Headers.ContentType != null && response.Content.Headers.ContentType.MediaType.Contains(HttpContentTypes.TextXml)) { var stringResponse = await response.Content.ReadAsStringAsync(); if (stringResponse == null) { throw new SubsonicErrorException("HTTP response contains no content"); } var result = await DeserializeResponseAsync(stringResponse); if (result.ItemElementName == ItemChoiceType.Error) { throw new SubsonicErrorException("Error occurred during request.", result.Item as Error); } throw new SubsonicApiException(string.Format(CultureInfo.CurrentCulture, "Unexpected response type: {0}", Enum.GetName(typeof(ItemChoiceType), result.ItemElementName))); } await response.Content.CopyToAsync(content); } } catch (Exception ex) { throw new SubsonicApiException(ex.Message, ex); } cancelToken?.ThrowIfCancellationRequested(); content.Position = 0; var image = ImageFormatFactory.Create(); await image.SetImageFromStreamAsync(content); cancelToken?.ThrowIfCancellationRequested(); return(image); }
public virtual async Task RequestWithoutResponseAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null) { var requestUri = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters); var clientHandler = GetClientHandler(); var client = GetClient(clientHandler); cancelToken?.ThrowIfCancellationRequested(); try { using (var message = new HttpRequestMessage(System.Net.Http.HttpMethod.Head, requestUri)) using (var ignored = cancelToken.HasValue ? await client.SendAsync(message, cancelToken.Value) : await client.SendAsync(message)) { } } catch (Exception ex) { throw new SubsonicApiException(ex.Message, ex); } cancelToken?.ThrowIfCancellationRequested(); }
public virtual async Task <Response> RequestAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null) { var requestUri = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters); var clientHandler = GetClientHandler(); var client = GetClient(clientHandler); cancelToken?.ThrowIfCancellationRequested(); Response result; try { using (var response = cancelToken.HasValue ? await client.PostAsync(requestUri, null, cancelToken.Value) : await client.PostAsync(requestUri, null)) { var stringResponse = await response.Content.ReadAsStringAsync(); if (stringResponse == null) { throw new SubsonicErrorException("HTTP response contains no content"); } if (response.Content.Headers.ContentType.MediaType.Contains(HttpContentTypes.TextXml)) { result = await DeserializeResponseAsync(stringResponse); } else { throw new SubsonicApiException(string.Format(CultureInfo.CurrentCulture, "HTTP response does not contain XML, content type is: {0}", response.Content.Headers.ContentType.MediaType)); } } } catch (Exception ex) { throw new SubsonicApiException(ex.Message, ex); } cancelToken?.ThrowIfCancellationRequested(); return(result); }