From ac3eaa58b6efbd7ece7d4ef1a57db142fbd27dc7 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 17 Feb 2020 12:08:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20GiteeControlle?= =?UTF-8?q?r=20=E6=8E=A5=E5=8F=A3=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/GiteeController.cs | 18 +++++++++++------- test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs | 6 ++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs index 4bf97095..bdf5617d 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/GiteeController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; +using System.Collections.Specialized; using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; @@ -29,7 +30,7 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public async Task Issues([FromServices]GiteeHttpClient client, [FromQuery]string? userName = "LongbowEnterprise", [FromQuery]string? repoName = "BootstrapAdmin", [FromQuery]string? label = "custom badge", [FromQuery]string? color = "orange") { - var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content => + 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" }; @@ -52,7 +53,7 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public async Task Pulls([FromServices]GiteeHttpClient client, [FromQuery]string? userName = "LongbowEnterprise", [FromQuery]string? repoName = "BootstrapAdmin", [FromQuery]string? label = "custom badge", [FromQuery]string? color = "orange") { - var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content => + 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" }; @@ -74,7 +75,7 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public async Task Releases([FromServices]GiteeHttpClient client, [FromQuery]string? userName = "LongbowEnterprise", [FromQuery]string? repoName = "BootstrapAdmin", [FromQuery]string? label = "custom badge", [FromQuery]string? color = "orange") { - var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content => + 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; @@ -95,19 +96,19 @@ 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(() => client.HttpClient.GetAsJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", null, new CancellationTokenSource(10000).Token), content => + 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 async static Task GetJsonAsync(Func> requestUrl, Func callback) + private async static Task GetJsonAsync(string url, Func> requestUrl, Func callback) { var ret = "unresponsive"; try { - var resq = await requestUrl(); + var resq = await requestUrl(url); ret = callback(resq); } catch (OperationCanceledException) @@ -116,7 +117,10 @@ namespace Bootstrap.Admin.Controllers.Api } catch (Exception ex) { - ex.Log(); + ex.Log(new NameValueCollection() + { + ["Url"] = url + }); } return ret; } diff --git a/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs b/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs index 27267bdb..c7cdc3e1 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/GiteeTest.cs @@ -46,7 +46,8 @@ namespace Bootstrap.Admin.Api t = t.MakeGenericMethod(new Type[] { typeof(string) }); t.Invoke(null, new object[] { - new Func>(() => + "", + new Func>(url => { throw new TaskCanceledException(); }), @@ -56,7 +57,8 @@ namespace Bootstrap.Admin.Api }); t.Invoke(null, new object[] { - new Func>(()=> { + "", + new Func>(url => { throw new Exception(); }), new Func(content => {