From 3ba8bd4ba8e2d4cbb820530aa6cd4033390db2fe Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Jun 2021 16:17:07 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=20json=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/GiteeController.cs | 72 ++++++------------- .../Controllers/Api/HealthsController.cs | 4 +- .../Extensions/CloudLoggerExtensions.cs | 1 + .../HealthChecks/GiteeHttpHealthCheck.cs | 4 +- src/admin/Bootstrap.Admin/Startup.cs | 2 + .../Bootstrap.DataAccess.csproj | 2 +- .../Bootstrap.Client.DataAccess.csproj | 2 +- .../Helper/TraceHttpClient.cs | 1 + .../Tasks/AppVeyorHttpClient.cs | 2 +- 9 files changed, 31 insertions(+), 59 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs index 311a2f6b..19f3f539 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs @@ -1,9 +1,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; -using System.Collections.Specialized; using System.Linq; -using System.Net.Http; +using System.Net.Http.Json; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; @@ -30,15 +29,13 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public async Task Issues([FromServices] GiteeHttpClient client, [FromQuery] string? userName = "dotnetchina", [FromQuery] string? repoName = "BootstrapAdmin", [FromQuery] string? label = "custom badge", [FromQuery] string? color = "orange") { - var ret = await GetJsonAsync($"https://gitee.com/{userName}/{repoName}/issues", url => client.HttpClient.GetStringAsync(url), content => - { - var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); - var labels = new string[] { "open", "progressing", "closed", "rejected" }; - var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); - return string.Join(" ", result); - }); - color = ret.StartsWith("open 0 progressing 0", StringComparison.OrdinalIgnoreCase) ? "success" : color; - return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); + var content = await client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"); + var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); + var labels = new string[] { "open", "progressing", "closed", "rejected" }; + var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); + var msg = string.Join(" ", result); + color = msg.StartsWith("open 0 progressing 0", StringComparison.OrdinalIgnoreCase) ? "success" : color; + return new JsonResult(new { schemaVersion = 1, label, message = msg, color }); } /// @@ -53,14 +50,12 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public async Task Pulls([FromServices] GiteeHttpClient client, [FromQuery] string? userName = "dotnetchina", [FromQuery] string? repoName = "BootstrapAdmin", [FromQuery] string? label = "custom badge", [FromQuery] string? color = "orange") { - var ret = await GetJsonAsync($"https://gitee.com/{userName}/{repoName}/pulls", url => client.HttpClient.GetStringAsync(url), content => - { - var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); - var labels = new string[] { "open", "merged", "closed" }; - var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); - return string.Join(" ", result); - }); - return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); + var content = await client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls") ?? ""; + var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); + var labels = new string[] { "open", "merged", "closed" }; + var result = string.IsNullOrEmpty(content) ? new string[] { "unknown" } : regex.Select((m, i) => $"{labels[i]} {m.Groups[1].Value}"); + var msg = string.Join(" ", result); + return new JsonResult(new { schemaVersion = 1, label, message = msg, color }); } /// @@ -75,12 +70,10 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public async Task Releases([FromServices] GiteeHttpClient client, [FromQuery] string? userName = "dotnetchina", [FromQuery] string? repoName = "BootstrapAdmin", [FromQuery] string? label = "custom badge", [FromQuery] string? color = "orange") { - var ret = await GetJsonAsync($"https://gitee.com/{userName}/{repoName}/releases", url => client.HttpClient.GetStringAsync(url), content => - { - var regex = Regex.Match(content, $"", RegexOptions.IgnoreCase); - return string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value; - }); - return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); + var content = await client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases") ?? ""; + var regex = Regex.Match(content, $"", RegexOptions.IgnoreCase); + var msg = regex?.Groups[1].Value ?? "unknown"; + return new JsonResult(new { schemaVersion = 1, label, message = msg, color }); } /// @@ -96,33 +89,8 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public async Task 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 ret = await GetJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", url => client.HttpClient.GetAsJsonAsync(url, null, new CancellationTokenSource(10000).Token), content => - { - return content == null ? "unknown" : content.Build.Version; - }); - return new JsonResult(new { schemaVersion = 1, label, message = ret, color }); - } - - private static async Task GetJsonAsync(string url, Func> requestUrl, Func callback) - { - var ret = "unresponsive"; - try - { - var resq = await requestUrl(url); - ret = callback(resq); - } - catch (OperationCanceledException) - { - - } - catch (Exception ex) - { - ex.Log(new NameValueCollection() - { - ["Url"] = url - }); - } - return ret; + var content = await client.HttpClient.GetFromJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", new CancellationTokenSource(10000).Token); + return new JsonResult(new { schemaVersion = 1, label, message = content?.Build.Version ?? "unknown", color }); } private class AppveyorBuildResult diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/HealthsController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/HealthsController.cs index 11521df2..9123d856 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/HealthsController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/HealthsController.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; -using System.Net.Http; +using System.Net.Http.Json; using System.Threading.Tasks; namespace Bootstrap.Admin.Controllers.Api @@ -23,7 +23,7 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPost] - public async Task Healths([FromServices]GiteeHttpClient httpClient, [FromServices]IConfiguration config, [FromBody]string message) + public async Task Healths([FromServices] GiteeHttpClient httpClient, [FromServices] IConfiguration config, [FromBody] string message) { var ret = false; var url = config.GetValue("HealthsCloudUrl", ""); diff --git a/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs b/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs index 2481f4dc..57625727 100644 --- a/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs +++ b/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging.Configuration; using Microsoft.Extensions.Options; using System; using System.Net.Http; +using System.Net.Http.Json; namespace Microsoft.Extensions.DependencyInjection { diff --git a/src/admin/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs b/src/admin/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs index 717bbbe2..7b786312 100644 --- a/src/admin/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs +++ b/src/admin/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Net.Http; +using System.Net.Http.Json; using System.Threading; using System.Threading.Tasks; @@ -42,7 +42,7 @@ namespace Bootstrap.Admin.HealthChecks { var sw = Stopwatch.StartNew(); Exception? error = null; - var result = await _client.HttpClient.GetAsJsonAsync($"/api/Gitee/{url}", ex => error = ex, cancellationToken); + var result = await _client.HttpClient.GetFromJsonAsync($"/api/Gitee/{url}", cancellationToken); sw.Stop(); data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}"); })).ToArray()); diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs index 5af9f538..c604d3ae 100644 --- a/src/admin/Bootstrap.Admin/Startup.cs +++ b/src/admin/Bootstrap.Admin/Startup.cs @@ -14,6 +14,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using System; +using System.Text; namespace Bootstrap.Admin { @@ -50,6 +51,7 @@ namespace Bootstrap.Admin /// public void ConfigureServices(IServiceCollection services) { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); services.AddLogging(logging => logging.AddFileLogger().AddCloudLogger().AddDBLogger(ExceptionsHelper.Log)); services.AddCors(); services.AddResponseCompression(); diff --git a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 43ec22ca..3e7410c4 100644 --- a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index 6d3d16a8..8b1fb570 100644 --- a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs b/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs index 9ede4831..b0e9fc75 100644 --- a/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs +++ b/src/client/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using System; using System.Collections.Specialized; using System.Net.Http; +using System.Net.Http.Json; namespace Bootstrap.Client.DataAccess { diff --git a/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs b/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs index bb649e7e..71366d1f 100644 --- a/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs +++ b/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs @@ -1,10 +1,10 @@ using Microsoft.Extensions.Configuration; using System; -using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Net.Http.Json; using System.Threading.Tasks; namespace Bootstrap.Client.Tasks