using System.Net.Http.Json; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi; namespace Yi.Framework.AiHub.Domain.AiGateWay; public static class HttpClientExtensions { public static async Task HttpRequestRaw(this HttpClient httpClient, string url, object? postData, string token) { HttpRequestMessage req = new(HttpMethod.Post, url); if (postData != null) { if (postData is HttpContent data) { req.Content = data; } else { string jsonContent = JsonSerializer.Serialize(postData, ThorJsonSerializer.DefaultOptions); var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); req.Content = stringContent; } } if (!string.IsNullOrEmpty(token)) { req.Headers.Add("Authorization", $"Bearer {token}"); } var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead); return response; } public static async Task HttpRequestRaw(this HttpClient httpClient, string url, object? postData, string token, string tokenKey) { HttpRequestMessage req = new(HttpMethod.Post, url); if (postData != null) { if (postData is HttpContent data) { req.Content = data; } else { string jsonContent = JsonSerializer.Serialize(postData, ThorJsonSerializer.DefaultOptions); var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); req.Content = stringContent; } } if (!string.IsNullOrEmpty(token)) { req.Headers.Add(tokenKey, token); } var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead); return response; } public static async Task HttpRequestRaw(this HttpClient httpClient, string url, object? postData, string token, Dictionary headers) { HttpRequestMessage req = new(HttpMethod.Post, url); if (postData != null) { if (postData is HttpContent data) { req.Content = data; } else { string jsonContent = JsonSerializer.Serialize(postData, ThorJsonSerializer.DefaultOptions); var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); req.Content = stringContent; } } if (!string.IsNullOrEmpty(token)) { req.Headers.Add("Authorization", $"Bearer {token}"); } foreach (var header in headers) { req.Headers.Add(header.Key, header.Value); } var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead); return response; } public static async Task HttpRequestRaw(this HttpClient httpClient, HttpRequestMessage req, object? postData) { if (postData != null) { if (postData is HttpContent data) { req.Content = data; } else { string jsonContent = JsonSerializer.Serialize(postData, ThorJsonSerializer.DefaultOptions); var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); req.Content = stringContent; } } var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead); return response; } public static async Task PostJsonAsync(this HttpClient httpClient, string url, object? postData, string token) { HttpRequestMessage req = new(HttpMethod.Post, url); if (postData != null) { if (postData is HttpContent data) { req.Content = data; } else { var stringContent = new StringContent(JsonSerializer.Serialize(postData, ThorJsonSerializer.DefaultOptions), Encoding.UTF8, "application/json"); req.Content = stringContent; } } if (!string.IsNullOrEmpty(token)) { req.Headers.Add("Authorization", $"Bearer {token}"); } return await httpClient.SendAsync(req); } public static async Task PostJsonAsync(this HttpClient httpClient, string url, object? postData, string token, Dictionary headers) { HttpRequestMessage req = new(HttpMethod.Post, url); if (postData != null) { if (postData is HttpContent data) { req.Content = data; } else { string jsonContent = JsonSerializer.Serialize(postData, ThorJsonSerializer.DefaultOptions); var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); req.Content = stringContent; } } if (!string.IsNullOrEmpty(token)) { req.Headers.Add("Authorization", $"Bearer {token}"); } foreach (var header in headers) { req.Headers.Add(header.Key, header.Value); } return await httpClient.SendAsync(req); } public static Task PostJsonAsync(this HttpClient httpClient, string url, object? postData, string token, string tokenKey) { HttpRequestMessage req = new(HttpMethod.Post, url); if (postData != null) { if (postData is HttpContent data) { req.Content = data; } else { string jsonContent = JsonSerializer.Serialize(postData, ThorJsonSerializer.DefaultOptions); var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); req.Content = stringContent; } } if (!string.IsNullOrEmpty(token)) { req.Headers.Add(tokenKey, token); } return httpClient.SendAsync(req); } public static async Task PostAndReadAsAsync(this HttpClient client, string uri, object? requestModel, CancellationToken cancellationToken = default) where TResponse : ThorBaseResponse, new() { var response = await client.PostAsJsonAsync(uri, requestModel, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }, cancellationToken); return await HandleResponseContent(response, cancellationToken); } public static async Task PostFileAndReadAsAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default) where TResponse : ThorBaseResponse, new() { var response = await client.PostAsync(uri, content, cancellationToken); return await HandleResponseContent(response, cancellationToken); } public static async Task PostFileAndReadAsStringAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default) { var response = await client.PostAsync(uri, content, cancellationToken); return await response.Content.ReadAsStringAsync(cancellationToken) ?? throw new InvalidOperationException(); } public static async Task DeleteAndReadAsAsync(this HttpClient client, string uri, CancellationToken cancellationToken = default) where TResponse : ThorBaseResponse, new() { var response = await client.DeleteAsync(uri, cancellationToken); return await HandleResponseContent(response, cancellationToken); } private static async Task HandleResponseContent(this HttpResponseMessage response, CancellationToken cancellationToken) where TResponse : ThorBaseResponse, new() { TResponse result; if (!response.Content.Headers.ContentType?.MediaType?.Equals("application/json", StringComparison.OrdinalIgnoreCase) ?? true) { result = new() { Error = new() { MessageObject = await response.Content.ReadAsStringAsync(cancellationToken) } }; } else { result = await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken) ?? throw new InvalidOperationException(); } return result; } }