Skip to content

Commit

Permalink
optimize GitHub Client
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 22, 2024
1 parent 815d3a9 commit f950827
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 12 deletions.
41 changes: 41 additions & 0 deletions src/Nager.PublicSuffix.WebApi/GitHub/GitHubClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Nager.PublicSuffix.WebApi.GitHub.Models;

namespace Nager.PublicSuffix.WebApi.GitHub
{
/// <summary>
/// GitHub Client
/// </summary>
public class GitHubClient
{
private readonly HttpClient _httpClient;

/// <summary>
/// GitHub Client
/// </summary>
/// <param name="httpClient"></param>
public GitHubClient(HttpClient httpClient)
{
this._httpClient = httpClient;
this._httpClient.DefaultRequestHeaders.Add("User-Agent", "Nager.PublicSuffix");
}

/// <summary>
/// GetCommitAsync
/// </summary>
/// <param name="owner"></param>
/// <param name="repository"></param>
/// <param name="branch"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<GitHubCommit?> GetCommitAsync(
string owner,
string repository,
string branch,
CancellationToken cancellationToken = default)
{
var requestUri = $"https://api.github.com/repos/{owner}/{repository}/commits/{branch}";

return await this._httpClient.GetFromJsonAsync<GitHubCommit>(requestUri, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nager.PublicSuffix.WebApi.Models
namespace Nager.PublicSuffix.WebApi.GitHub.Models
{
public class Author
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nager.PublicSuffix.WebApi.Models
namespace Nager.PublicSuffix.WebApi.GitHub.Models
{
public class CommitInfo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nager.PublicSuffix.WebApi.Models
namespace Nager.PublicSuffix.WebApi.GitHub.Models
{
public class Committer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nager.PublicSuffix.WebApi.Models
namespace Nager.PublicSuffix.WebApi.GitHub.Models
{
public class File
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nager.PublicSuffix.WebApi.Models
namespace Nager.PublicSuffix.WebApi.GitHub.Models
{

public class GitHubCommit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nager.PublicSuffix.WebApi.Models
namespace Nager.PublicSuffix.WebApi.GitHub.Models
{
public class Tree
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nager.PublicSuffix.WebApi.Models
namespace Nager.PublicSuffix.WebApi.GitHub.Models
{
public class Verification
{
Expand Down
9 changes: 4 additions & 5 deletions src/Nager.PublicSuffix.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Nager.PublicSuffix;
using Nager.PublicSuffix.RuleProviders;
using Nager.PublicSuffix.RuleProviders.CacheProviders;
using Nager.PublicSuffix.WebApi.Models;
using System.Text.Encodings.Web;
using Nager.PublicSuffix.WebApi.GitHub;
using System.Text.Json.Serialization;
using System.Web;

Expand All @@ -14,6 +13,7 @@
builder.Services.AddSingleton<ICacheProvider, LocalFileSystemCacheProvider>();
builder.Services.AddSingleton<IRuleProvider, CachedHttpRuleProvider>();
builder.Services.AddSingleton<IDomainParser, DomainParser>();
builder.Services.AddScoped<GitHubClient>();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
Expand Down Expand Up @@ -49,10 +49,9 @@
.WithName("DomainInfo")
.WithOpenApi();

app.MapPost("/CheckLastCommit", async (HttpClient httpClient) =>
app.MapPost("/CheckLastCommit", async (GitHubClient gitHubClient, CancellationToken cancellationToken) =>
{
httpClient.DefaultRequestHeaders.Add("User-Agent", "Nager.PublicSuffix");
var lastGitHubCommit = await httpClient.GetFromJsonAsync<GitHubCommit>("https://api.github.com/repos/publicsuffix/list/commits/master");
var lastGitHubCommit = await gitHubClient.GetCommitAsync("publicsuffix", "list", "master", cancellationToken);
return lastGitHubCommit?.Commit?.Committer?.Date;
})
Expand Down

0 comments on commit f950827

Please sign in to comment.