Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PCL.Core/App/Essentials/TelemetryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static async Task _StartAsync()
NatFilterBehaviour = natTest?.State.FilteringBehavior.ToString(),
Ipv6Status = NetworkInterfaceUtils.GetIPv6Status().ToString()
};
using var response = await HttpRequestBuilder
using var response = await HttpRequestCreator
.Create("https://pcl2ce.pysio.online/post", HttpMethod.Post)
.WithAuthentication(telemetryKey).WithJsonContent(telemetry)
.SendAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace PCL.Core.IO.Net.Http.Client;

public class HttpRequestBuilder
public class HttpRequestCreator
{
private readonly HttpRequestMessage _request;
private readonly Dictionary<string, string> _cookies = [];
Expand All @@ -25,46 +25,46 @@ public class HttpRequestBuilder
private TimeSpan _timeOutMillisec = TimeSpan.FromMilliseconds(30 * 1000);
private bool _isEndOfLife = false;

private HttpRequestBuilder(Uri uri, HttpMethod? method = null)
private HttpRequestCreator(Uri uri, HttpMethod? method = null)
{
_request = new HttpRequestMessage(method ?? HttpMethod.Get, uri);
}

/// <summary>
/// 创建一个 HttpRequestBuilder 对象
/// 创建一个 HttpRequestCreator 对象
/// </summary>
public static HttpRequestBuilder Create(string url, HttpMethod? method = null)
public static HttpRequestCreator Create(string url, HttpMethod? method = null)
{
return new HttpRequestBuilder(new Uri(url), method);
return new HttpRequestCreator(new Uri(url), method);
}

/// <summary>
/// 创建一个 HttpRequestBuilder 对象
/// 创建一个 HttpRequestCreator 对象
/// </summary>
public static HttpRequestBuilder Create(Uri uri, HttpMethod? method = null)
public static HttpRequestCreator Create(Uri uri, HttpMethod? method = null)
{
return new HttpRequestBuilder(uri, method);
return new HttpRequestCreator(uri, method);
}

/// <summary>
/// 设置请求载荷
/// </summary>
public HttpRequestBuilder WithContent(HttpContent content, string? contentType = null)
public HttpRequestCreator WithContent(HttpContent content, string? contentType = null)
{
_request.Content = content;
if (contentType is not null) WithHeader("Content-Type", contentType);
return this;
}

public HttpRequestBuilder WithContent(string content, string? contentType = null)
public HttpRequestCreator WithContent(string content, string? contentType = null)
{
_request.Content = contentType is null
? new StringContent(content, Encoding.UTF8)
: new StringContent(content, Encoding.UTF8, contentType);
return this;
}

public HttpRequestBuilder WithJsonContent(dynamic content)
public HttpRequestCreator WithJsonContent(dynamic content)
{
_request.Content = new StringContent(JsonSerializer.Serialize(content), Encoding.UTF8, "application/json");
return this;
Expand All @@ -73,7 +73,7 @@ public HttpRequestBuilder WithJsonContent(dynamic content)
/// <summary>
/// 设置一个请求所用的 Cookie,如果已设置过对应的键,旧的则会被覆盖
/// </summary>
public HttpRequestBuilder WithCookie(string key, string value)
public HttpRequestCreator WithCookie(string key, string value)
{
_cookies[key] = value;
return this;
Expand All @@ -82,7 +82,7 @@ public HttpRequestBuilder WithCookie(string key, string value)
/// <summary>
/// 设置多个请求所用的 Cookie,如果已设置过对应的键,旧的则会被覆盖
/// </summary>
public HttpRequestBuilder WithCookie(IDictionary<string, string> cookies)
public HttpRequestCreator WithCookie(IDictionary<string, string> cookies)
{
foreach (var cookie in cookies) _cookies[cookie.Key] = cookie.Value;
return this;
Expand All @@ -91,7 +91,7 @@ public HttpRequestBuilder WithCookie(IDictionary<string, string> cookies)
/// <summary>
/// 设置多个 Header
/// </summary>
public HttpRequestBuilder WithHeader(IDictionary<string, string> headers)
public HttpRequestCreator WithHeader(IDictionary<string, string> headers)
{
foreach (var header in headers) WithHeader(header.Key, header.Value);
return this;
Expand All @@ -100,7 +100,7 @@ public HttpRequestBuilder WithHeader(IDictionary<string, string> headers)
/// <summary>
/// 设置一个 Header
/// </summary>
public HttpRequestBuilder WithHeader(string key, string value)
public HttpRequestCreator WithHeader(string key, string value)
{
if (key.StartsWith("Content-", StringComparison.OrdinalIgnoreCase) && _request.Content is not null)
_request.Content.Headers.TryAddWithoutValidation(key, value);
Expand All @@ -109,56 +109,56 @@ public HttpRequestBuilder WithHeader(string key, string value)
return this;
}

public HttpRequestBuilder WithHeader(KeyValuePair<string, string> header) => WithHeader(header.Key, header.Value);
public HttpRequestCreator WithHeader(KeyValuePair<string, string> header) => WithHeader(header.Key, header.Value);

public HttpRequestBuilder WithAuthentication(string scheme, string token)
public HttpRequestCreator WithAuthentication(string scheme, string token)
{
ArgumentException.ThrowIfNullOrEmpty(scheme);
ArgumentException.ThrowIfNullOrEmpty(token);
_request.Headers.Authorization = new AuthenticationHeaderValue(scheme, token);
return this;
}

public HttpRequestBuilder WithAuthentication(string token)
public HttpRequestCreator WithAuthentication(string token)
{
ArgumentException.ThrowIfNullOrEmpty(token);
_request.Headers.Authorization = new AuthenticationHeaderValue(token);
return this;
}

public HttpRequestBuilder WithBearerToken(string token) => WithAuthentication("Bearer", token);
public HttpRequestCreator WithBearerToken(string token) => WithAuthentication("Bearer", token);

public HttpRequestBuilder WithDefaultHeaderOption(bool hasDefaultHeader = true)
public HttpRequestCreator WithDefaultHeaderOption(bool hasDefaultHeader = true)
{
_addLauncherHeader = hasDefaultHeader;
return this;
}

public HttpRequestBuilder WithHttpVersionOption(Version httpVersion)
public HttpRequestCreator WithHttpVersionOption(Version httpVersion)
{
_requestVersion = httpVersion;
return this;
}

public HttpRequestBuilder WithLoggingOptions(bool doLog)
public HttpRequestCreator WithLoggingOptions(bool doLog)
{
_doLog = doLog;
return this;
}

public HttpRequestBuilder WithCompletionOption(HttpCompletionOption option)
public HttpRequestCreator WithCompletionOption(HttpCompletionOption option)
{
_completionOption = option;
return this;
}

public HttpRequestBuilder WithTimeOut(uint millisec)
public HttpRequestCreator WithTimeOut(uint millisec)
{
_timeOutMillisec = TimeSpan.FromMilliseconds(millisec);
return this;
}

public HttpRequestBuilder WithTimeOut(TimeSpan timeSpan)
public HttpRequestCreator WithTimeOut(TimeSpan timeSpan)
{
_timeOutMillisec = timeSpan;
return this;
Expand All @@ -177,7 +177,7 @@ public async Task<HttpResponseHandler> SendAsync(
CancellationToken ct = default,
Func<int, TimeSpan>? retryPolicy = null)
{
if (_isEndOfLife) throw new ObjectDisposedException(nameof(HttpRequestBuilder));
if (_isEndOfLife) throw new ObjectDisposedException(nameof(HttpRequestCreator));

_PrepareRequestParameters();

Expand Down
2 changes: 1 addition & 1 deletion PCL.Core/Link/Lobby/LobbyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ select relay
}
else
{
using var response = await HttpRequestBuilder
using var response = await HttpRequestCreator
.Create("https://pcl2ce.pysio.online/post", HttpMethod.Post)
.WithContent(httpContent)
.WithAuthentication(key)
Expand Down
4 changes: 2 additions & 2 deletions PCL.Core/Link/Natayark/NatayarkProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static async Task GetNaidDataAsync(string token, bool isRefresh = false,

var httpContent = new StringContent(requestData, Encoding.UTF8, "application/x-www-form-urlencoded");

using var oauthResponse = await HttpRequestBuilder
using var oauthResponse = await HttpRequestCreator
.Create("https://account.naids.com/api/oauth2/token", HttpMethod.Post)
.WithContent(httpContent)
.SendAsync(true).ConfigureAwait(false);
Expand All @@ -78,7 +78,7 @@ public static async Task GetNaidDataAsync(string token, bool isRefresh = false,
var expiresAt = data["refresh_token_expires_at"]!.ToString();

// 获取用户信息
using var userDataResponse = await HttpRequestBuilder
using var userDataResponse = await HttpRequestCreator
.Create("https://account.naids.com/api/api/user/data", HttpMethod.Get)
.WithBearerToken(NaidProfile.AccessToken)
.SendAsync(true).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion PCL.Core/Link/Scaffolding/EasyTier/EasyTierEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private async Task<IReadOnlyList<string>> _GetPublicNodeAsync()
}

private Task<HttpResponseHandler> _SendPublicNodeGetReqAsync() =>
HttpRequestBuilder
HttpRequestCreator
.Create("https://uptime.easytier.cn/api/nodes?page=1&per_page=50&is_active=true", HttpMethod.Get)
.SendAsync();

Expand Down
2 changes: 1 addition & 1 deletion PCL.Core/Minecraft/Yggdrasil/ApiLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static async Task<string> TryRequestAsync(string address)
{
var originAddr = address.StartsWith("http") ? address : $"https://{address}";
var originUri = new Uri(originAddr);
using var response = await HttpRequestBuilder.Create(originAddr, HttpMethod.Head).SendAsync();
using var response = await HttpRequestCreator.Create(originAddr, HttpMethod.Head).SendAsync();
response.TryGetHeader("X-Authlib-Injector-Api-Location", out var responses);
if (responses.Length == 0) return originAddr;
var resultAddr = responses.First();
Expand Down
2 changes: 1 addition & 1 deletion PCL.Core/ViewModel/Homepage/NewsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private async Task LoadDataAsync()
try
{
var url = $"{BaseApiUrl}?pageSize={PageSize}&sortType=Recent&category=News&newsOnly=true&page={_currentPage}";
using var resp = await HttpRequestBuilder
using var resp = await HttpRequestCreator
.Create(url)
.SendAsync(true);
var json = await resp.AsJsonAsync<ApiResponse>();
Expand Down