HttpClientResponseExtensions.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using EasyDevCore.Common;
  2. using System.Net;
  3. using System.Net.Http.Json;
  4. namespace EasyDevCore.Remote.HttpAccess
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public static class HttpClientResponseExtensions
  10. {
  11. /// <summary>
  12. /// Gets the json asynchronous.
  13. /// </summary>
  14. /// <typeparam name="TResult">The type of the result.</typeparam>
  15. /// <param name="response">The response.</param>
  16. /// <param name="defaultValue">The default value.</param>
  17. /// <returns></returns>
  18. public static async Task<TResult> GetJsonAsync<TResult>(this Task<HttpResponseMessage> response, TResult defaultValue = default)
  19. {
  20. try
  21. {
  22. var value = await response;
  23. if (value.IsSuccessStatusCode)
  24. {
  25. return await value.Content.ReadFromJsonAsync<TResult>();
  26. }
  27. else
  28. {
  29. return defaultValue;
  30. }
  31. }
  32. catch
  33. {
  34. if (defaultValue != null)
  35. {
  36. return defaultValue;
  37. }
  38. throw;
  39. }
  40. }
  41. private static async Task<T> GetValueFromJsonContent<T>(HttpContent content)
  42. {
  43. using (var stream = content.ReadAsStreamAsync())
  44. {
  45. var result = HttpAccessSetup.JsonSerializerResponse.Deserialize<T>(await stream);
  46. return result;
  47. }
  48. }
  49. private static TResult DefaultFailureHandler<TResult>(Exception e)
  50. {
  51. return (TResult)Activator.CreateInstance(typeof(TResult), e);
  52. }
  53. /// <summary>
  54. /// Makes the result.
  55. /// </summary>
  56. /// <typeparam name="TResult">The type of the result.</typeparam>
  57. /// <param name="response">The response.</param>
  58. /// <param name="failure">The failure.</param>
  59. /// <returns></returns>
  60. public static async Task<TResult> MakeResultAsync<TResult>(this Task<HttpResponseMessage> response, Func<Exception, TResult> failure = null)
  61. where TResult : new()
  62. {
  63. try
  64. {
  65. if (failure == null)
  66. {
  67. failure = DefaultFailureHandler<TResult>;
  68. }
  69. var value = await response;
  70. if (value.IsSuccessStatusCode)
  71. {
  72. return await GetValueFromJsonContent<TResult>(value.Content);
  73. }
  74. else
  75. {
  76. TResult result = default;
  77. string errorMessage = string.Empty;
  78. int errorNum = 0;
  79. int statusNum = (int)value.StatusCode;
  80. if (value.Content.Headers.ContentLength > 0)
  81. {
  82. try
  83. {
  84. result = await GetValueFromJsonContent<TResult>(value.Content);
  85. }
  86. catch
  87. {
  88. errorMessage = await value.Content.ReadAsStringAsync();
  89. }
  90. }
  91. if (result == null)
  92. {
  93. errorNum = statusNum;
  94. }
  95. else if (result.GetType().HasInterface(typeof(IResult<>)))
  96. {
  97. bool isError = result.GetPropertyValue<bool>("IsError");
  98. if (isError)
  99. {
  100. string resultCode = result.GetPropertyValue<string>("ResultCode");
  101. errorMessage = result.GetPropertyValue<string>("Message");
  102. errorNum = int.TryParse(resultCode, out var resultNum) ? resultNum : statusNum;
  103. }
  104. }
  105. Exception exception = new Exception(string.IsNullOrWhiteSpace(errorMessage) ? value.StatusCode.ToString() : errorMessage);
  106. exception.HResult = errorNum == 0 ? statusNum : errorNum;
  107. return failure(exception);
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. return failure(ex);
  113. }
  114. }
  115. }
  116. }