|
@@ -0,0 +1,57 @@
|
|
|
+using Fw.Utility.UnifyResponse;
|
|
|
+using MapsterMapper;
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Http.Json;
|
|
|
+using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace DataSharing.LuZhou.LZ110
|
|
|
+{
|
|
|
+ public class LZ110Invoker
|
|
|
+ {
|
|
|
+ private readonly IHttpClientFactory _httpClientFactory;
|
|
|
+ private readonly IMapper _mapper;
|
|
|
+ private readonly ILogger<LZ110Invoker> _logger;
|
|
|
+
|
|
|
+ public LZ110Invoker(
|
|
|
+ IHttpClientFactory httpClientFactory,
|
|
|
+ IMapper mapper,
|
|
|
+ ILogger<LZ110Invoker> logger)
|
|
|
+ {
|
|
|
+ _httpClientFactory = httpClientFactory;
|
|
|
+ _mapper = mapper;
|
|
|
+ _logger = logger;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<ApiResponse<TResponse>> InvokeAsync<TRequest, TResponse>(TRequest request,
|
|
|
+ CancellationToken cancellationToken)
|
|
|
+ where TRequest : ILZ110RequestBase<TResponse>
|
|
|
+ where TResponse : class
|
|
|
+ {
|
|
|
+ using var client = _httpClientFactory.CreateClient();
|
|
|
+ client.BaseAddress = new Uri(_address.Value.LuZhou110);
|
|
|
+ client.DefaultRequestHeaders.Add("User-Identify", "12345rexian");
|
|
|
+ var serializeOptions = new JsonSerializerOptions
|
|
|
+ {
|
|
|
+ PropertyNamingPolicy = new JsonPascalNamingPolicy(),
|
|
|
+ };
|
|
|
+
|
|
|
+ //_logger.LogInformation($"发起请求LZ110:{request.GetRequestUrl()}");
|
|
|
+ var result = await client.PostAsJsonAsync(request.GetRequestUrl(), request, serializeOptions, cancellationToken);
|
|
|
+
|
|
|
+ if (result is null || !result.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ _logger.LogError($"泸州110接口请求失败,request: {request}");
|
|
|
+ return new ApiResponse<TResponse> { Code = 1, Message = $"泸州110接口请求失败,request: {request}" };
|
|
|
+ }
|
|
|
+ var response = await result.Content.ReadFromJsonAsync<TResponse>();
|
|
|
+ return new ApiResponse<TResponse> { Code = 0, Data = response };
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|