chore: 更新 json 请求相关方法

This commit is contained in:
Argo-Tianyi 2021-06-01 16:17:07 +08:00
parent 45a70fe238
commit 3ba8bd4ba8
9 changed files with 31 additions and 59 deletions

View File

@ -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<ActionResult> 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, "<div class='ui mini circular label'>([\\d]+)</div>", 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, "<div class='ui mini circular label'>([\\d]+)</div>", 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 });
}
/// <summary>
@ -53,14 +50,12 @@ namespace Bootstrap.Admin.Controllers.Api
[HttpGet]
public async Task<ActionResult> 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, "<div class='ui mini circular label'>([\\d]+)</div>", 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, "<div class='ui mini circular label'>([\\d]+)</div>", 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 });
}
/// <summary>
@ -75,12 +70,10 @@ namespace Bootstrap.Admin.Controllers.Api
[HttpGet]
public async Task<ActionResult> 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, $"<a href=\"/{userName}/{repoName}/releases/([^\\s]+)\" target=\"_blank\">", 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, $"<a href=\"/{userName}/{repoName}/releases/([^\\s]+)\" target=\"_blank\">", RegexOptions.IgnoreCase);
var msg = regex?.Groups[1].Value ?? "unknown";
return new JsonResult(new { schemaVersion = 1, label, message = msg, color });
}
/// <summary>
@ -96,33 +89,8 @@ namespace Bootstrap.Admin.Controllers.Api
[HttpGet]
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 ret = await GetJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", url => client.HttpClient.GetAsJsonAsync<AppveyorBuildResult>(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<string> GetJsonAsync<T>(string url, Func<string, Task<T>> requestUrl, Func<T, string> 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<AppveyorBuildResult>($"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

View File

@ -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
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
public async Task<bool> Healths([FromServices]GiteeHttpClient httpClient, [FromServices]IConfiguration config, [FromBody]string message)
public async Task<bool> Healths([FromServices] GiteeHttpClient httpClient, [FromServices] IConfiguration config, [FromBody] string message)
{
var ret = false;
var url = config.GetValue("HealthsCloudUrl", "");

View File

@ -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
{

View File

@ -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<object>($"/api/Gitee/{url}", ex => error = ex, cancellationToken);
var result = await _client.HttpClient.GetFromJsonAsync<object>($"/api/Gitee/{url}", cancellationToken);
sw.Stop();
data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}");
})).ToArray());

View File

@ -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
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
services.AddLogging(logging => logging.AddFileLogger().AddCloudLogger().AddDBLogger(ExceptionsHelper.Log));
services.AddCors();
services.AddResponseCompression();

View File

@ -14,7 +14,7 @@
<PackageReference Include="Longbow.Security.Cryptography" Version="5.0.0" />
<PackageReference Include="Longbow.Tasks" Version="5.0.1" />
<PackageReference Include="Longbow.TencentAuth" Version="5.0.0" />
<PackageReference Include="Longbow.Web" Version="5.0.0" />
<PackageReference Include="Longbow.Web" Version="5.0.1" />
<PackageReference Include="Longbow.WeChatAuth" Version="5.0.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.6" />
<PackageReference Include="PetaPoco.Extensions" Version="5.0.1" />

View File

@ -6,7 +6,7 @@
<PackageReference Include="Longbow" Version="5.0.0" />
<PackageReference Include="Longbow.Cache" Version="5.0.0" />
<PackageReference Include="Longbow.Data" Version="5.0.1" />
<PackageReference Include="Longbow.Web" Version="5.0.0" />
<PackageReference Include="Longbow.Web" Version="5.0.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
</ItemGroup>

View File

@ -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
{

View File

@ -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