refactor github client in a separate project

This commit is contained in:
13xforever
2019-01-08 02:12:48 +05:00
parent 2b5f447080
commit 862f1cf5ea
6 changed files with 111 additions and 53 deletions

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using CompatApiClient.Compression;
@@ -17,8 +15,6 @@ namespace CompatApiClient
private readonly HttpClient client;
private readonly MediaTypeFormatterCollection formatters;
private static readonly Dictionary<string, PrInfo> prInfoCache = new Dictionary<string, PrInfo>();
public Client()
{
client = HttpClientFactory.Create(new CompressionMessageHandler());
@@ -92,49 +88,5 @@ namespace CompatApiClient
} while (tries < 3);
return null;
}
public async Task<PrInfo> GetPrInfoAsync(string pr, CancellationToken cancellationToken)
{
if (prInfoCache.TryGetValue(pr, out var result))
return result;
try
{
using (var message = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/RPCS3/rpcs3/pulls/" + pr))
{
message.Headers.UserAgent.Add(new ProductInfoHeaderValue("RPCS3CompatibilityBot", "2.0"));
using (var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false))
{
try
{
await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
result = await response.Content.ReadAsAsync<PrInfo>(formatters, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
ConsoleLogger.PrintError(e, response);
}
}
}
}
catch (Exception e)
{
ApiConfig.Log.Error(e);
}
if (result == null)
{
int.TryParse(pr, out var prnum);
return new PrInfo { Number = prnum };
}
lock (prInfoCache)
{
if (prInfoCache.TryGetValue(pr, out var cachedResult))
return cachedResult;
prInfoCache[pr] = result;
return result;
}
}
}
}

View File

@@ -2,15 +2,16 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CompatApiClient;
using CompatApiClient.POCOs;
using DSharpPlus.Entities;
using GithubClient.POCOs;
namespace CompatBot.Utils.ResultFormatters
{
internal static class UpdateInfoFormatter
{
private static readonly Client client = new Client();
private static readonly CompatApiClient.Client compatApiClient = new CompatApiClient.Client();
private static readonly GithubClient.Client githubClient = new GithubClient.Client();
public static async Task<DiscordEmbedBuilder> AsEmbedAsync(this UpdateInfo info, DiscordEmbedBuilder builder = null)
{
@@ -30,7 +31,7 @@ namespace CompatBot.Utils.ResultFormatters
else
{
url = "https://github.com/RPCS3/rpcs3/pull/" + pr;
prInfo = await client.GetPrInfoAsync(pr, Config.Cts.Token).ConfigureAwait(false);
prInfo = await githubClient.GetPrInfoAsync(pr, Config.Cts.Token).ConfigureAwait(false);
pr = $"PR #{pr} by {prInfo?.User?.Login ?? "???"}";
}
}

79
GithubClient/Client.cs Normal file
View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using CompatApiClient;
using CompatApiClient.Compression;
using CompatApiClient.Utils;
using GithubClient.POCOs;
using Newtonsoft.Json;
using JsonContractResolver = CompatApiClient.JsonContractResolver;
namespace GithubClient
{
public class Client
{
private readonly HttpClient client;
private readonly MediaTypeFormatterCollection formatters;
private static readonly Dictionary<string, PrInfo> prInfoCache = new Dictionary<string, PrInfo>();
public Client()
{
client = HttpClientFactory.Create(new CompressionMessageHandler());
var settings = new JsonSerializerSettings
{
ContractResolver = new JsonContractResolver(NamingStyles.Underscore),
NullValueHandling = NullValueHandling.Ignore
};
formatters = new MediaTypeFormatterCollection(new[] { new JsonMediaTypeFormatter { SerializerSettings = settings } });
}
public async Task<PrInfo> GetPrInfoAsync(string pr, CancellationToken cancellationToken)
{
if (prInfoCache.TryGetValue(pr, out var result))
return result;
try
{
using (var message = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/RPCS3/rpcs3/pulls/" + pr))
{
message.Headers.UserAgent.Add(new ProductInfoHeaderValue("RPCS3CompatibilityBot", "2.0"));
using (var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false))
{
try
{
await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
result = await response.Content.ReadAsAsync<PrInfo>(formatters, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
ConsoleLogger.PrintError(e, response);
}
}
}
}
catch (Exception e)
{
ApiConfig.Log.Error(e);
}
if (result == null)
{
int.TryParse(pr, out var prnum);
return new PrInfo { Number = prnum };
}
lock (prInfoCache)
{
if (prInfoCache.TryGetValue(pr, out var cachedResult))
return cachedResult;
prInfoCache[pr] = result;
return result;
}
}
}
}

View File

@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CompatApiClient\CompatApiClient.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,4 +1,4 @@
namespace CompatApiClient.POCOs
namespace GithubClient.POCOs
{
public class PrInfo
{

View File

@@ -13,7 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HomoglyphConverter", "Homog
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IrdLibraryClient", "IrdLibraryClient\IrdLibraryClient.csproj", "{AA2A333B-CD30-41A5-A680-CC9BCB2D726B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{815D60C3-F84B-4AE2-B4E4-48004515FCFD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{815D60C3-F84B-4AE2-B4E4-48004515FCFD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Clients", "Clients", "{E7FE0ADD-CBA6-4321-8A1C-0A3B5C3F54C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GithubClient", "GithubClient\GithubClient.csproj", "{AF8FDA29-864E-4A1C-9568-99DECB7E4B36}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -44,10 +48,20 @@ Global
{815D60C3-F84B-4AE2-B4E4-48004515FCFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{815D60C3-F84B-4AE2-B4E4-48004515FCFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{815D60C3-F84B-4AE2-B4E4-48004515FCFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF8FDA29-864E-4A1C-9568-99DECB7E4B36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF8FDA29-864E-4A1C-9568-99DECB7E4B36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF8FDA29-864E-4A1C-9568-99DECB7E4B36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF8FDA29-864E-4A1C-9568-99DECB7E4B36}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8AF3C23B-D695-4391-A298-5BA4AAB8E13B} = {E7FE0ADD-CBA6-4321-8A1C-0A3B5C3F54C2}
{AA5FF441-BD1D-4444-9178-7DC7BFF3C139} = {E7FE0ADD-CBA6-4321-8A1C-0A3B5C3F54C2}
{AA2A333B-CD30-41A5-A680-CC9BCB2D726B} = {E7FE0ADD-CBA6-4321-8A1C-0A3B5C3F54C2}
{AF8FDA29-864E-4A1C-9568-99DECB7E4B36} = {E7FE0ADD-CBA6-4321-8A1C-0A3B5C3F54C2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D7696F56-AEAC-4D83-9BD8-BE0C122A5DCE}
EndGlobalSection