Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. private async Task<Stream> GetContent(HttpResponseMessage message, object state = null)
  2. {
  3. if (message == null)
  4. {
  5. throw new ArgumentNullException(nameof(message));
  6. }
  7.  
  8. using (HttpContent content = message.Content)
  9. {
  10. Stream contentStream = await content.ReadAsStreamAsync();
  11. if (contentStream == null)
  12. {
  13. throw new InvalidOperationException("The content stream is null.");
  14. }
  15.  
  16. IEnumerable<string> encoding;
  17. if (message.Headers.TryGetValues("Content-Encoding", out encoding))
  18. {
  19. List<string> encodingList = encoding.ToList();
  20.  
  21. if (encodingList.FirstOrDefault() != null)
  22. {
  23. bool compressed = encodingList.First().Equals("gzip", StringComparison.OrdinalIgnoreCase);
  24. if (compressed)
  25. {
  26. Stream uncompressed = this.gzipInflator.Convert(contentStream, state);
  27. if (uncompressed != null)
  28. {
  29. contentStream = uncompressed;
  30. }
  31. else
  32. {
  33. throw new InvalidOperationException("Could not read stream.");
  34. }
  35. }
  36. }
  37. }
  38.  
  39. await contentStream.FlushAsync();
  40. contentStream.Position = 0;
  41. return contentStream;
  42. }
  43. }
  44.  
  45. public async Task<TOutput> ConvertElementAsync<TInput, TOutput>(HttpResponseMessage responseMessage, IConverter<TInput, TOutput> innerConverter, CancellationToken cancellationToken, object state = null)
  46. {
  47. var contentStream = await this.GetContent(responseMessage, state);
  48.  
  49. TInput response = this.serializerFactory.GetSerializer<TInput>().Deserialize(contentStream);
  50.  
  51. return innerConverter.Convert(response, state);
  52. }
  53.  
  54. public async Task<TOutput> ConvertElementAsync<TInput, TOutput>(HttpResponseMessage responseMessage, IConverter<TInput, TOutput> innerConverter, CancellationToken cancellationToken, object state = null)
  55. {
  56. if (responseMessage == null)
  57. {
  58. throw new ArgumentNullException(nameof(responseMessage));
  59. }
  60.  
  61. using (HttpContent content = responseMessage.Content)
  62. {
  63. Stream contentStream = await content.CopyToAsync();
  64. if (contentStream == null)
  65. {
  66. throw new InvalidOperationException("The content stream is null.");
  67. }
  68.  
  69. IEnumerable<string> encoding;
  70. if (responseMessage.Headers.TryGetValues("Content-Encoding", out encoding))
  71. {
  72. List<string> encodingList = encoding.ToList();
  73.  
  74. if (encodingList.FirstOrDefault() != null)
  75. {
  76. bool compressed = encodingList.First().Equals("gzip", StringComparison.OrdinalIgnoreCase);
  77. if (compressed)
  78. {
  79. Stream uncompressed = this.gzipInflator.Convert(contentStream, state);
  80. if (uncompressed != null)
  81. {
  82. contentStream = uncompressed;
  83. }
  84. else
  85. {
  86. throw new InvalidOperationException("Could not read stream.");
  87. }
  88. }
  89. }
  90. }
  91.  
  92. }
  93.  
  94. TInput response = this.serializerFactory.GetSerializer<TInput>().Deserialize(contentStream);
  95.  
  96. return innerConverter.Convert(response, state);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement