Update HttpClientHelper.cs

AI-optimized code
This commit is contained in:
2dust 2025-03-06 10:47:57 +08:00
parent 1229c967ba
commit 8d8a887c42

View file

@ -18,12 +18,17 @@ namespace ServiceLib.Common
public static HttpClientHelper Instance => _instance.Value; public static HttpClientHelper Instance => _instance.Value;
private readonly HttpClient httpClient; private readonly HttpClient httpClient;
private HttpClientHelper(HttpClient httpClient) => this.httpClient = httpClient; private HttpClientHelper(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public async Task<string?> TryGetAsync(string url) public async Task<string?> TryGetAsync(string url)
{ {
if (url.IsNullOrEmpty()) if (url.IsNullOrEmpty())
{
return null; return null;
}
try try
{ {
@ -39,14 +44,18 @@ namespace ServiceLib.Common
public async Task<string?> GetAsync(string url) public async Task<string?> GetAsync(string url)
{ {
if (url.IsNullOrEmpty()) if (url.IsNullOrEmpty())
{
return null; return null;
}
return await httpClient.GetStringAsync(url); return await httpClient.GetStringAsync(url);
} }
public async Task<string?> GetAsync(HttpClient client, string url, CancellationToken token = default) public async Task<string?> GetAsync(HttpClient client, string url, CancellationToken token = default)
{ {
if (url.IsNullOrEmpty()) if (url.IsNullOrEmpty())
{
return null; return null;
}
return await client.GetStringAsync(url, token); return await client.GetStringAsync(url, token);
} }
@ -55,13 +64,13 @@ namespace ServiceLib.Common
var jsonContent = JsonUtils.Serialize(headers); var jsonContent = JsonUtils.Serialize(headers);
var content = new StringContent(jsonContent, Encoding.UTF8, MediaTypeNames.Application.Json); var content = new StringContent(jsonContent, Encoding.UTF8, MediaTypeNames.Application.Json);
var result = await httpClient.PutAsync(url, content); await httpClient.PutAsync(url, content);
} }
public async Task PatchAsync(string url, Dictionary<string, string> headers) public async Task PatchAsync(string url, Dictionary<string, string> headers)
{ {
var myContent = JsonUtils.Serialize(headers); var myContent = JsonUtils.Serialize(headers);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent); var buffer = Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer); var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
@ -78,12 +87,16 @@ namespace ServiceLib.Common
ArgumentNullException.ThrowIfNull(url); ArgumentNullException.ThrowIfNull(url);
ArgumentNullException.ThrowIfNull(fileName); ArgumentNullException.ThrowIfNull(fileName);
if (File.Exists(fileName)) if (File.Exists(fileName))
{
File.Delete(fileName); File.Delete(fileName);
}
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token); using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{
throw new Exception(response.StatusCode.ToString()); throw new Exception(response.StatusCode.ToString());
}
var total = response.Content.Headers.ContentLength ?? -1L; var total = response.Content.Headers.ContentLength ?? -1L;
var canReportProgress = total != -1 && progress != null; var canReportProgress = total != -1 && progress != null;
@ -102,7 +115,9 @@ namespace ServiceLib.Common
totalRead += read; totalRead += read;
if (read == 0) if (read == 0)
{
break; break;
}
await file.WriteAsync(buffer.AsMemory(0, read), token); await file.WriteAsync(buffer.AsMemory(0, read), token);
if (canReportProgress) if (canReportProgress)
@ -173,7 +188,7 @@ namespace ServiceLib.Common
totalRead += read; totalRead += read;
var ts = (DateTime.Now - totalDatetime); var ts = DateTime.Now - totalDatetime;
if (progress != null && ts.Seconds > totalSecond) if (progress != null && ts.Seconds > totalSecond)
{ {
totalSecond = ts.Seconds; totalSecond = ts.Seconds;