HttpClientResponseExtensions.cs 4.0 KB

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