BaseHttpInvoker.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using DataSharing.Share.Dtos.Province;
  2. using IdentityModel.Client;
  3. using Microsoft.AspNetCore.Http;
  4. using System.Net.Http.Headers;
  5. using System.Net.Http.Json;
  6. using System.Text;
  7. using XF.Domain.Dependency;
  8. using XF.Domain.Exceptions;
  9. namespace DataSharing;
  10. public class BaseHttpInvoker : IHttpInvoker, IScopeDependency
  11. {
  12. private readonly IHttpClientFactory _httpClientFactory;
  13. public BaseHttpInvoker(IHttpClientFactory httpClientFactory)
  14. {
  15. _httpClientFactory = httpClientFactory;
  16. }
  17. public async Task<TResponse?> RequestAsync<TRequest, TResponse>(
  18. TRequest request, Action<HttpClient>? setHttpClient = null, CancellationToken cancellationToken = default)
  19. where TRequest : ISharingRequest, new()
  20. {
  21. var httpClient = _httpClientFactory.CreateClient();
  22. if (setHttpClient != null)
  23. setHttpClient.Invoke(httpClient);
  24. var url = request.GetRequestUrl();
  25. var method = request.GetHttpMethod();
  26. var rsp = string.Empty;
  27. if (HttpMethods.IsGet(method))
  28. {
  29. rsp = await httpClient.GetStringAsync(url, cancellationToken);
  30. }
  31. else if (HttpMethods.IsPost(method))
  32. {
  33. using var responseMessage = await httpClient.PostAsJsonAsync(url, request, cancellationToken);
  34. responseMessage.EnsureSuccessStatusCode();
  35. using var responseContent = responseMessage.Content;
  36. rsp = await responseContent.ReadAsStringAsync(cancellationToken);
  37. }
  38. else
  39. {
  40. throw new UserFriendlyException("暂不支持该请求方式");
  41. }
  42. return System.Text.Json.JsonSerializer.Deserialize<TResponse>(rsp);
  43. }
  44. public async Task<TResponse?> RequestStringContentAsync<TResponse>(string url, string httpMethod, string? stringContent = null,
  45. Action<HttpClient>? setHttpClient = null, CancellationToken cancellationToken = default)
  46. {
  47. var httpClient = _httpClientFactory.CreateClient();
  48. if (setHttpClient != null)
  49. setHttpClient.Invoke(httpClient);
  50. var rsp = string.Empty;
  51. if (HttpMethods.IsGet(httpMethod))
  52. {
  53. rsp = await httpClient.GetStringAsync(url, cancellationToken);
  54. }
  55. else if (HttpMethods.IsPost(httpMethod))
  56. {
  57. using var responseMessage = await httpClient.PostAsync(url, new StringContent(stringContent, Encoding.UTF8, new MediaTypeWithQualityHeaderValue("application/json")),
  58. cancellationToken);
  59. responseMessage.EnsureSuccessStatusCode();
  60. using var responseContent = responseMessage.Content;
  61. rsp = await responseContent.ReadAsStringAsync(cancellationToken);
  62. }
  63. else
  64. {
  65. throw new UserFriendlyException("暂不支持该请求方式");
  66. }
  67. return System.Text.Json.JsonSerializer.Deserialize<TResponse>(rsp);
  68. }
  69. public async Task<TokenResponse> GetTokenAsync(ClientCredentialsTokenRequest request, CancellationToken cancellationToken)
  70. {
  71. var httpClient = _httpClientFactory.CreateClient();
  72. return await httpClient.RequestClientCredentialsTokenAsync(request, cancellationToken);
  73. }
  74. }