Created
May 19, 2019 14:53
-
-
Save cyberpirate92/ed8c7893ca241656ce44f64d846a4945 to your computer and use it in GitHub Desktop.
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
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