refactor: 增加 GiteeHttpClient 复用 HttpClient 提高速度
This commit is contained in:
parent
f21e26d26a
commit
96df7bfe76
|
@ -20,18 +20,16 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 Gitee 网站 Issues 信息
|
/// 获取 Gitee 网站 Issues 信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="httpClientFactory"></param>
|
/// <param name="client"></param>
|
||||||
/// <param name="userName"></param>
|
/// <param name="userName"></param>
|
||||||
/// <param name="repoName"></param>
|
/// <param name="repoName"></param>
|
||||||
/// <param name="label"></param>
|
/// <param name="label"></param>
|
||||||
/// <param name="color"></param>
|
/// <param name="color"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult> Issues([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
public async Task<ActionResult> Issues([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
||||||
{
|
{
|
||||||
var client = httpClientFactory.CreateClient();
|
var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content =>
|
||||||
client.Timeout = TimeSpan.FromMilliseconds(2000);
|
|
||||||
var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content =>
|
|
||||||
{
|
{
|
||||||
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
|
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
|
||||||
var labels = new string[] { "open", "progressing", "closed", "rejected" };
|
var labels = new string[] { "open", "progressing", "closed", "rejected" };
|
||||||
|
@ -45,18 +43,16 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 Gitee 网站 Pulls 信息
|
/// 获取 Gitee 网站 Pulls 信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="httpClientFactory"></param>
|
/// <param name="client"></param>
|
||||||
/// <param name="userName"></param>
|
/// <param name="userName"></param>
|
||||||
/// <param name="repoName"></param>
|
/// <param name="repoName"></param>
|
||||||
/// <param name="label"></param>
|
/// <param name="label"></param>
|
||||||
/// <param name="color"></param>
|
/// <param name="color"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult> Pulls([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
public async Task<ActionResult> Pulls([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
||||||
{
|
{
|
||||||
var client = httpClientFactory.CreateClient();
|
var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content =>
|
||||||
client.Timeout = TimeSpan.FromMilliseconds(2000);
|
|
||||||
var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content =>
|
|
||||||
{
|
{
|
||||||
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
|
var regex = Regex.Matches(content, "<div class='ui mini circular label'>([\\d]+)</div>", RegexOptions.IgnoreCase);
|
||||||
var labels = new string[] { "open", "merged", "closed" };
|
var labels = new string[] { "open", "merged", "closed" };
|
||||||
|
@ -69,18 +65,16 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 Gitee 网站 Releases 信息
|
/// 获取 Gitee 网站 Releases 信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="httpClientFactory"></param>
|
/// <param name="client"></param>
|
||||||
/// <param name="userName"></param>
|
/// <param name="userName"></param>
|
||||||
/// <param name="repoName"></param>
|
/// <param name="repoName"></param>
|
||||||
/// <param name="label"></param>
|
/// <param name="label"></param>
|
||||||
/// <param name="color"></param>
|
/// <param name="color"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult> Releases([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
public async Task<ActionResult> Releases([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
||||||
{
|
{
|
||||||
var client = httpClientFactory.CreateClient();
|
var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content =>
|
||||||
client.Timeout = TimeSpan.FromMilliseconds(2000);
|
|
||||||
var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content =>
|
|
||||||
{
|
{
|
||||||
var regex = Regex.Match(content, $"<a href=\"/{userName}/{repoName}/releases/([^\\s]+)\" target=\"_blank\">", RegexOptions.IgnoreCase);
|
var regex = Regex.Match(content, $"<a href=\"/{userName}/{repoName}/releases/([^\\s]+)\" target=\"_blank\">", RegexOptions.IgnoreCase);
|
||||||
return string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value;
|
return string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value;
|
||||||
|
@ -91,7 +85,7 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 Gitee 网站 Builds 信息
|
/// 获取 Gitee 网站 Builds 信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="httpClientFactory"></param>
|
/// <param name="client"></param>
|
||||||
/// <param name="userName"></param>
|
/// <param name="userName"></param>
|
||||||
/// <param name="projName"></param>
|
/// <param name="projName"></param>
|
||||||
/// <param name="branchName"></param>
|
/// <param name="branchName"></param>
|
||||||
|
@ -99,10 +93,9 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
/// <param name="color"></param>
|
/// <param name="color"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult> Builds([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
public async Task<ActionResult> Builds([FromServices]GiteeHttpClient client, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange")
|
||||||
{
|
{
|
||||||
var client = httpClientFactory.CreateClient();
|
var ret = await GetJsonAsync(() => client.HttpClient.GetAsJsonAsync<AppveyorBuildResult>($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", null, new CancellationTokenSource(2000).Token), content =>
|
||||||
var ret = await GetJsonAsync(() => client.GetAsJsonAsync<AppveyorBuildResult>($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", null, new CancellationTokenSource(2000).Token), content =>
|
|
||||||
{
|
{
|
||||||
return content == null ? "unknown" : content.Build.Version;
|
return content == null ? "unknown" : content.Build.Version;
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
|
namespace Bootstrap.Admin
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gitee HttpClient 操作类
|
||||||
|
/// </summary>
|
||||||
|
public class GiteeHttpClient
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// HttpClient 实例
|
||||||
|
/// </summary>
|
||||||
|
public HttpClient HttpClient { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client"></param>
|
||||||
|
public GiteeHttpClient(HttpClient client)
|
||||||
|
{
|
||||||
|
client.Timeout = TimeSpan.FromSeconds(10);
|
||||||
|
client.DefaultRequestHeaders.Connection.Add("keep-alive");
|
||||||
|
HttpClient = client;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,16 +15,16 @@ namespace Bootstrap.Admin.HealthChecks
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GiteeHttpHealthCheck : IHealthCheck
|
public class GiteeHttpHealthCheck : IHealthCheck
|
||||||
{
|
{
|
||||||
private readonly HttpClient _client;
|
private readonly GiteeHttpClient _client;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="factory"></param>
|
/// <param name="client"></param>
|
||||||
/// <param name="accessor"></param>
|
/// <param name="accessor"></param>
|
||||||
public GiteeHttpHealthCheck(IHttpClientFactory factory, IHttpContextAccessor accessor)
|
public GiteeHttpHealthCheck(GiteeHttpClient client, IHttpContextAccessor accessor)
|
||||||
{
|
{
|
||||||
_client = factory.CreateClient();
|
_client = client;
|
||||||
_client.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}{accessor.HttpContext.Request.PathBase}");
|
_client.HttpClient.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}{accessor.HttpContext.Request.PathBase}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -42,7 +42,7 @@ namespace Bootstrap.Admin.HealthChecks
|
||||||
{
|
{
|
||||||
var sw = Stopwatch.StartNew();
|
var sw = Stopwatch.StartNew();
|
||||||
Exception error = null;
|
Exception error = null;
|
||||||
var result = await _client.GetAsJsonAsync<object>($"/api/Gitee/{url}", ex => error = ex, cancellationToken);
|
var result = await _client.HttpClient.GetAsJsonAsync<object>($"/api/Gitee/{url}", ex => error = ex, cancellationToken);
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}");
|
data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}");
|
||||||
})).ToArray());
|
})).ToArray());
|
||||||
|
|
|
@ -64,6 +64,7 @@ namespace Bootstrap.Admin
|
||||||
services.AddSwagger();
|
services.AddSwagger();
|
||||||
services.AddButtonAuthorization();
|
services.AddButtonAuthorization();
|
||||||
services.AddDemoTask();
|
services.AddDemoTask();
|
||||||
|
services.AddHttpClient<GiteeHttpClient>();
|
||||||
services.AddHealthChecks().AddBootstrapAdminHealthChecks();
|
services.AddHealthChecks().AddBootstrapAdminHealthChecks();
|
||||||
services.AddMvc(options =>
|
services.AddMvc(options =>
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue