Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async Task<Stream> GetContent(HttpResponseMessage message, object state = null)
- {
- if (message == null)
- {
- throw new ArgumentNullException(nameof(message));
- }
- using (HttpContent content = message.Content)
- {
- Stream contentStream = await content.ReadAsStreamAsync();
- if (contentStream == null)
- {
- throw new InvalidOperationException("The content stream is null.");
- }
- IEnumerable<string> encoding;
- if (message.Headers.TryGetValues("Content-Encoding", out encoding))
- {
- List<string> encodingList = encoding.ToList();
- if (encodingList.FirstOrDefault() != null)
- {
- bool compressed = encodingList.First().Equals("gzip", StringComparison.OrdinalIgnoreCase);
- if (compressed)
- {
- Stream uncompressed = this.gzipInflator.Convert(contentStream, state);
- if (uncompressed != null)
- {
- contentStream = uncompressed;
- }
- else
- {
- throw new InvalidOperationException("Could not read stream.");
- }
- }
- }
- }
- await contentStream.FlushAsync();
- contentStream.Position = 0;
- return contentStream;
- }
- }
- public async Task<TOutput> ConvertElementAsync<TInput, TOutput>(HttpResponseMessage responseMessage, IConverter<TInput, TOutput> innerConverter, CancellationToken cancellationToken, object state = null)
- {
- var contentStream = await this.GetContent(responseMessage, state);
- TInput response = this.serializerFactory.GetSerializer<TInput>().Deserialize(contentStream);
- return innerConverter.Convert(response, state);
- }
- public async Task<TOutput> ConvertElementAsync<TInput, TOutput>(HttpResponseMessage responseMessage, IConverter<TInput, TOutput> innerConverter, CancellationToken cancellationToken, object state = null)
- {
- if (responseMessage == null)
- {
- throw new ArgumentNullException(nameof(responseMessage));
- }
- using (HttpContent content = responseMessage.Content)
- {
- Stream contentStream = await content.CopyToAsync();
- if (contentStream == null)
- {
- throw new InvalidOperationException("The content stream is null.");
- }
- IEnumerable<string> encoding;
- if (responseMessage.Headers.TryGetValues("Content-Encoding", out encoding))
- {
- List<string> encodingList = encoding.ToList();
- if (encodingList.FirstOrDefault() != null)
- {
- bool compressed = encodingList.First().Equals("gzip", StringComparison.OrdinalIgnoreCase);
- if (compressed)
- {
- Stream uncompressed = this.gzipInflator.Convert(contentStream, state);
- if (uncompressed != null)
- {
- contentStream = uncompressed;
- }
- else
- {
- throw new InvalidOperationException("Could not read stream.");
- }
- }
- }
- }
- }
- TInput response = this.serializerFactory.GetSerializer<TInput>().Deserialize(contentStream);
- return innerConverter.Convert(response, state);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement