Skip to content

Instantly share code, notes, and snippets.

@cyberpirate92
Created May 19, 2019 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberpirate92/ed8c7893ca241656ce44f64d846a4945 to your computer and use it in GitHub Desktop.
Save cyberpirate92/ed8c7893ca241656ce44f64d846a4945 to your computer and use it in GitHub Desktop.
public class GithubService {
private IHttpClientFactory httpClientFactory;
private static string GITHUB_API_URL = "https://api.github.com";
public GithubService(IHttpClientFactory clientFactory) {
httpClientFactory = clientFactory;
}
public async Task<List<GithubRepository>> FetchUserRepositories(string username) {
var repositories = new List<GithubRepository>();
if (!string.IsNullOrEmpty(username)) {
var httpClient = httpClientFactory.CreateClient();
string reposUrl = GetUserReposUrl(username);
Console.WriteLine(reposUrl);
httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
var apiResponse = await httpClient.GetAsync(reposUrl);
if (apiResponse.IsSuccessStatusCode) {
string json = await apiResponse.Content.ReadAsStringAsync();
repositories.AddRange(JsonConvert.DeserializeObject<List<GithubRepository>>(json));
}
}
return repositories;
}
private static string GetUserReposUrl(string username) {
return $"{GITHUB_API_URL}/users/{username}/repos";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment