mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-17 10:00:44 +08:00
refactor: WpfGui拆解GetAsync, 暴露Exception, 增加PostAsync
This commit is contained in:
@@ -98,8 +98,16 @@ namespace MaaWpfGui.Helper
|
||||
headers["If-None-Match"] = etag;
|
||||
}
|
||||
|
||||
var response = await Instances.HttpService.GetAsync(new Uri(url), headers);
|
||||
return response;
|
||||
try
|
||||
{
|
||||
var response = await Instances.HttpService.GetAsync(new Uri(url), headers);
|
||||
return response;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", url);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using MaaWpfGui.Constants;
|
||||
using MaaWpfGui.Extensions;
|
||||
@@ -121,8 +122,16 @@ namespace MaaWpfGui.Models
|
||||
|
||||
var url = $"{BaseUrl}?current_version={currentVersion}&cdk={cdk}&user_agent=MaaWpfGui&sp_id={spid}";
|
||||
|
||||
var response = await Instances.HttpService.GetAsync(new(url), logQuery: false);
|
||||
_logger.Information($"current_version: {currentVersion}, cdk: {cdk.Mask()}");
|
||||
HttpResponseMessage? response = null;
|
||||
try
|
||||
{
|
||||
response = await Instances.HttpService.GetAsync(new(url), logQuery: false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", new Uri(url).GetLeftPart(UriPartial.Path));
|
||||
_logger.Information($"current_version: {currentVersion}, cdk: {cdk.Mask()}");
|
||||
}
|
||||
|
||||
if (response is null)
|
||||
{
|
||||
@@ -339,14 +348,21 @@ namespace MaaWpfGui.Models
|
||||
|
||||
private static async Task<bool> DownloadFullPackageAsync(string url, string saveTo)
|
||||
{
|
||||
using var response = await Instances.HttpService.GetAsync(new Uri(url));
|
||||
|
||||
if (response is not { StatusCode: System.Net.HttpStatusCode.OK })
|
||||
try
|
||||
{
|
||||
using var response = await Instances.HttpService.GetAsync(new Uri(url));
|
||||
if (response is not { StatusCode: System.Net.HttpStatusCode.OK })
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return await HttpResponseHelper.SaveResponseToFileAsync(response, saveTo);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", url);
|
||||
return false;
|
||||
}
|
||||
|
||||
return await HttpResponseHelper.SaveResponseToFileAsync(response, saveTo);
|
||||
}
|
||||
|
||||
private static void DirectoryMerge(string sourceDirName, string destDirName)
|
||||
|
||||
@@ -107,75 +107,65 @@ namespace MaaWpfGui.Services.Web
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string?> GetStringAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead, bool logQuery = true)
|
||||
{
|
||||
var response = await GetAsync(uri, extraHeader, httpCompletionOption, logQuery);
|
||||
|
||||
if (response?.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
public async Task<Stream?> GetStreamAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead, bool logQuery = true)
|
||||
{
|
||||
var response = await GetAsync(uri, extraHeader, httpCompletionOption, logQuery);
|
||||
|
||||
if (response?.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.Content.ReadAsStreamAsync();
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage?> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true)
|
||||
public async Task<string?> GetStringAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage { RequestUri = uri, Method = HttpMethod.Get, Version = HttpVersion.Version20, };
|
||||
|
||||
if (extraHeader != null)
|
||||
var response = await GetAsync(uri, extraHeader, httpCompletionOption);
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
foreach (var kvp in extraHeader)
|
||||
{
|
||||
request.Headers.Add(kvp.Key, kvp.Value);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
var response = await _client.SendAsync(request, httpCompletionOption);
|
||||
response.Log(logQuery);
|
||||
|
||||
return response;
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", uri.GetLeftPart(logQuery ? UriPartial.Query : UriPartial.Path));
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", uri);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Stream?> GetStreamAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await GetAsync(uri, extraHeader, httpCompletionOption);
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.Content.ReadAsStreamAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", uri);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true)
|
||||
{
|
||||
var request = new HttpRequestMessage { RequestUri = uri, Method = HttpMethod.Get, Version = HttpVersion.Version20, };
|
||||
if (extraHeader != null)
|
||||
{
|
||||
foreach (var kvp in extraHeader)
|
||||
{
|
||||
request.Headers.Add(kvp.Key, kvp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
var response = await _client.SendAsync(request, httpCompletionOption);
|
||||
response.Log(logQuery);
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<string?> PostAsJsonAsync<T>(Uri uri, T content, Dictionary<string, string>? extraHeader = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var body = JsonSerializer.Serialize(content);
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, uri) { Version = HttpVersion.Version20 };
|
||||
|
||||
if (extraHeader is not null)
|
||||
{
|
||||
foreach (var header in extraHeader)
|
||||
{
|
||||
message.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
message.Headers.Accept.ParseAdd("application/json");
|
||||
message.Content = new StringContent(body, Encoding.UTF8, "application/json");
|
||||
var response = await _client.SendAsync(message);
|
||||
response.Log();
|
||||
var response = await PostAsync(uri, new StringContent(JsonSerializer.Serialize(content), Encoding.UTF8, "application/json"), extraHeader);
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -189,20 +179,7 @@ namespace MaaWpfGui.Services.Web
|
||||
{
|
||||
try
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, uri) { Version = HttpVersion.Version20 };
|
||||
message.Headers.Accept.ParseAdd("application/json");
|
||||
|
||||
if (extraHeader is not null)
|
||||
{
|
||||
foreach (var header in extraHeader)
|
||||
{
|
||||
message.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
message.Content = new FormUrlEncodedContent(content);
|
||||
var response = await _client.SendAsync(message);
|
||||
response.Log();
|
||||
var response = await PostAsync(uri, new FormUrlEncodedContent(content), extraHeader);
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -212,6 +189,24 @@ namespace MaaWpfGui.Services.Web
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> PostAsync(Uri uri, HttpContent content, Dictionary<string, string>? extraHeader = null)
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, uri) { Version = HttpVersion.Version20 };
|
||||
if (extraHeader is not null)
|
||||
{
|
||||
foreach (var header in extraHeader)
|
||||
{
|
||||
message.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
message.Headers.Accept.ParseAdd("application/json");
|
||||
message.Content = content;
|
||||
var response = await _client.SendAsync(message);
|
||||
response.Log();
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<bool> DownloadFileAsync(Uri uri, string fileName, string? contentType = "application/octet-stream")
|
||||
{
|
||||
string fileDir = Directory.GetCurrentDirectory();
|
||||
@@ -220,10 +215,18 @@ namespace MaaWpfGui.Services.Web
|
||||
string fullFilePathWithTemp = Path.Combine(fileDir, fileNameWithTemp);
|
||||
_logger.Information("Start to download file from {Uri} and save to {TempPath}", uri, fullFilePathWithTemp);
|
||||
|
||||
var response = await GetAsync(uri, extraHeader: new Dictionary<string, string> { { "Accept", contentType ?? "application/octet-stream" } }, httpCompletionOption: HttpCompletionOption.ResponseHeadersRead);
|
||||
|
||||
if (response?.StatusCode != HttpStatusCode.OK)
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await GetAsync(uri, extraHeader: new Dictionary<string, string> { { "Accept", contentType ?? "application/octet-stream" } }, httpCompletionOption: HttpCompletionOption.ResponseHeadersRead);
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", uri);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,9 +36,8 @@ namespace MaaWpfGui.Services.Web
|
||||
/// <param name="uri">Target Uri</param>
|
||||
/// <param name="extraHeader">Extra HTTP Request Headers</param>
|
||||
/// <param name="httpCompletionOption">The HTTP completion option</param>
|
||||
/// <param name="logQuery">Whether to log uri</param>
|
||||
/// <returns>Response string, null when failed</returns>
|
||||
Task<string?> GetStringAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead, bool logQuery = true);
|
||||
Task<string?> GetStringAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead);
|
||||
|
||||
/// <summary>
|
||||
/// Send HTTP GET request and get a body stream response
|
||||
@@ -46,10 +45,9 @@ namespace MaaWpfGui.Services.Web
|
||||
/// <param name="uri">Target Uri</param>
|
||||
/// <param name="extraHeader">Extra HTTP Request Headers</param>
|
||||
/// <param name="httpCompletionOption">The HTTP completion option</param>
|
||||
/// <param name="logQuery">Whether to log uri</param>
|
||||
/// <returns>Response stream, null when failed</returns>
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
Task<Stream?> GetStreamAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead, bool logQuery = true);
|
||||
Task<Stream?> GetStreamAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead);
|
||||
|
||||
/// <summary>
|
||||
/// Send HTTP GET request and get the original <see cref="HttpRequestMessage"/>
|
||||
@@ -59,7 +57,7 @@ namespace MaaWpfGui.Services.Web
|
||||
/// <param name="httpCompletionOption">The HTTP completion option</param>
|
||||
/// <param name="logQuery">Whether to log uri</param>
|
||||
/// <returns><see cref="HttpRequestMessage"/> object</returns>
|
||||
Task<HttpResponseMessage?> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true);
|
||||
Task<HttpResponseMessage> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true);
|
||||
|
||||
/// <summary>
|
||||
/// Send HTTP POST request and a string response
|
||||
@@ -80,6 +78,15 @@ namespace MaaWpfGui.Services.Web
|
||||
/// <returns>Response string, null when failed</returns>
|
||||
Task<string?> PostAsFormUrlEncodedAsync(Uri uri, Dictionary<string, string?> content, Dictionary<string, string>? extraHeader = null);
|
||||
|
||||
/// <summary>
|
||||
/// Send HTTP POST request with raw HttpContent and get the response
|
||||
/// </summary>
|
||||
/// <param name="uri">Target Uri</param>
|
||||
/// <param name="content">The POST body content</param>
|
||||
/// <param name="extraHeader">Extra HTTP Request Headers</param>
|
||||
/// <returns>HttpResponseMessage, null when failed</returns>
|
||||
Task<HttpResponseMessage> PostAsync(Uri uri, HttpContent content, Dictionary<string, string>? extraHeader = null);
|
||||
|
||||
/// <summary>
|
||||
/// Download a file from the Web
|
||||
/// </summary>
|
||||
|
||||
@@ -18,6 +18,7 @@ using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
@@ -963,8 +964,16 @@ public class VersionUpdateViewModel : Screen
|
||||
|
||||
var url = $"{MaaUrls.MirrorChyanAppUpdate}?current_version={_curVersion}&cdk={cdk}&user_agent=MaaWpfGui&os=win&arch={arch}&channel={channel}&sp_id={spid}";
|
||||
|
||||
var response = await Instances.HttpService.GetAsync(new(url), logQuery: false);
|
||||
_logger.Information($"current_version: {_curVersion}, cdk: {cdk.Mask()}, arch: {arch}, channel: {channel}");
|
||||
HttpResponseMessage? response = null;
|
||||
try
|
||||
{
|
||||
response = await Instances.HttpService.GetAsync(new(url), logQuery: false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to send GET request to {Uri}", new Uri(url).GetLeftPart(UriPartial.Path));
|
||||
_logger.Information($"current_version: {_curVersion}, cdk: {cdk.Mask()}, arch: {arch}, channel: {channel}");
|
||||
}
|
||||
|
||||
if (response is null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user