Browse Source

refactor GithubRepositoryManager #3381

pull/3399/head
Alper Ebicoglu 6 years ago
parent
commit
d72aada3b5
  1. 68
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubRepositoryManager.cs

68
modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubRepositoryManager.cs

@ -23,54 +23,30 @@ namespace Volo.Docs.GitHub.Documents
public async Task<string> GetFileRawStringContentAsync(string rawUrl, string token, string userAgent)
{
var httpClient = _clientFactory.CreateClient(HttpClientName);
if (!token.IsNullOrWhiteSpace())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token);
}
if (!userAgent.IsNullOrWhiteSpace())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
}
using var httpClient = CreateHttpClient(token, userAgent);
return await httpClient.GetStringAsync(new Uri(rawUrl));
}
public async Task<byte[]> GetFileRawByteArrayContentAsync(string rawUrl, string token, string userAgent)
{
var httpClient = _clientFactory.CreateClient(HttpClientName);
if (!token.IsNullOrWhiteSpace())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token);
}
if (!userAgent.IsNullOrWhiteSpace())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
}
using var httpClient = CreateHttpClient(token, userAgent);
return await httpClient.GetByteArrayAsync(new Uri(rawUrl));
}
public async Task<IReadOnlyList<Release>> GetReleasesAsync(string name, string repositoryName, string token)
{
var client = token.IsNullOrWhiteSpace()
? new GitHubClient(new ProductHeaderValue(name))
: new GitHubClient(new ProductHeaderValue(name), new InMemoryCredentialStore(new Credentials(token)));
var client = GetGitHubClient(name, token);
var releases = await client.Repository.Release.GetAll(name, repositoryName);
return (await client
.Repository
.Release
.GetAll(name, repositoryName)).ToList();
return releases.ToList();
}
public async Task<IReadOnlyList<GitHubCommit>> GetFileCommitsAsync(string name, string repositoryName, string version, string filename, string token)
{
var client = token.IsNullOrWhiteSpace()
? new GitHubClient(new ProductHeaderValue(name))
: new GitHubClient(new ProductHeaderValue(name), new InMemoryCredentialStore(new Credentials(token)));
var client = GetGitHubClient(name, token);
var repo = await client.Repository.Get(name, repositoryName);
var request = new CommitRequest { Path = filename, Sha = version };
return await client.Repository.Commit.GetAll(repo.Id, request);
@ -78,12 +54,32 @@ namespace Volo.Docs.GitHub.Documents
public async Task<GitHubCommit> GetSingleCommitsAsync(string name, string repositoryName, string sha, string token)
{
var client = token.IsNullOrWhiteSpace()
? new GitHubClient(new ProductHeaderValue(name))
: new GitHubClient(new ProductHeaderValue(name), new InMemoryCredentialStore(new Credentials(token)));
var client = GetGitHubClient(name, token);
var repo = await client.Repository.Get(name, repositoryName);
return await client.Repository.Commit.Get(repo.Id, sha);
}
private HttpClient CreateHttpClient(string token, string userAgent)
{
var httpClient = _clientFactory.CreateClient(HttpClientName);
if (!token.IsNullOrWhiteSpace())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token);
}
if (!userAgent.IsNullOrWhiteSpace())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
}
return httpClient;
}
private static GitHubClient GetGitHubClient(string name, string token)
{
return token.IsNullOrWhiteSpace()
? new GitHubClient(new ProductHeaderValue(name))
: new GitHubClient(new ProductHeaderValue(name), new InMemoryCredentialStore(new Credentials(token)));
}
}
}

Loading…
Cancel
Save