-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration and support for GitHub
- Loading branch information
Showing
7 changed files
with
257 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace GitMan.Clients | ||
{ | ||
internal class GitHubClient | ||
{ | ||
private readonly GitHubClientConfig _config; | ||
|
||
public GitHubClient(GitHubClientConfig config) | ||
{ | ||
_config = config; | ||
} | ||
|
||
private HttpClient GetClient() | ||
{ | ||
var client = new HttpClient(); | ||
|
||
var acceptHeader = new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"); | ||
client.DefaultRequestHeaders.Accept.Add(acceptHeader); | ||
|
||
var assemblyName = Assembly.GetExecutingAssembly().GetName(); | ||
var userAgentHeader = new ProductInfoHeaderValue(assemblyName.Name, assemblyName.Version.ToString()); | ||
client.DefaultRequestHeaders.UserAgent.Add(userAgentHeader); | ||
|
||
var patBytes = Encoding.ASCII.GetBytes(_config.Username + ":" + _config.PersonalAccessToken); | ||
var authParameter = Convert.ToBase64String(patBytes); | ||
var authHeader = new AuthenticationHeaderValue("Basic", authParameter); | ||
client.DefaultRequestHeaders.Authorization = authHeader; | ||
|
||
client.Timeout = TimeSpan.FromSeconds(30); | ||
|
||
return client; | ||
} | ||
|
||
private Uri BuildUri(string path) | ||
{ | ||
var builder = new UriBuilder | ||
{ | ||
Host = "api.github.com", | ||
Scheme = "https", | ||
Path = path | ||
}; | ||
|
||
var uriString = builder.ToString(); | ||
var uri = new Uri(uriString); | ||
return uri; | ||
} | ||
|
||
private JsonDocument GetResponse(string path) | ||
{ | ||
var uri = BuildUri(path); | ||
|
||
using (var client = GetClient()) | ||
{ | ||
using (var response = client.GetAsync(uri).Result) | ||
{ | ||
var json = response.Content.ReadAsStringAsync().Result; | ||
var document = JsonDocument.Parse(json); | ||
return document; | ||
} | ||
} | ||
} | ||
|
||
public GitHubRepository[] GetRepositories() | ||
{ | ||
var document = GetResponse("user/repos"); | ||
var repositories = document.RootElement; | ||
|
||
var count = repositories.GetArrayLength(); | ||
var GitHubRepos = new GitHubRepository[count]; | ||
var index = 0; | ||
|
||
foreach (var repository in repositories.EnumerateArray()) | ||
{ | ||
var name = repository.GetProperty("name").GetString(); | ||
var fullName = repository.GetProperty("full_name").GetString(); | ||
var remoteUrl = repository.GetProperty("clone_url").GetString(); | ||
var GitHubRepo = new GitHubRepository(name, fullName, remoteUrl); | ||
GitHubRepos[index] = GitHubRepo; | ||
index++; | ||
} | ||
|
||
return GitHubRepos; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace GitMan.Clients | ||
{ | ||
internal class GitHubClientConfig | ||
{ | ||
public string Username { get; set; } | ||
public string PersonalAccessToken { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace GitMan.Clients | ||
{ | ||
public class GitHubRepository | ||
{ | ||
public string Name { get; } | ||
public string FullName { get; } | ||
public string RemoteUrl { get; } | ||
|
||
public GitHubRepository(string name, string fullName, string remoteUrl) | ||
{ | ||
Name = name; | ||
FullName = fullName; | ||
RemoteUrl = remoteUrl; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace GitMan.Config | ||
{ | ||
internal class GitHubProvider | ||
{ | ||
public string Username { get; set; } | ||
public string PersonalAccessToken { get; set; } | ||
public Dictionary<string, string> DefaultConfig { get; set; } = new Dictionary<string, string>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters