private static void DisplayRepoInfo(IGitHubRepo repo) { Console.WriteLine("I found this random GitHub just for you!"); Console.WriteLine($"Id: {repo.Id}"); Console.WriteLine($"Owner: {repo.Owner.Login}"); Console.WriteLine($"Owner's GitHub: {repo.Owner.Html_url}"); Console.WriteLine($"Name: {repo.Name}"); Console.WriteLine($"Description: {repo.Description}"); Console.WriteLine($"URL: {repo.Html_url}"); Console.WriteLine($"Programming Language: {repo.Language}"); }
private async void btnFind_Click(object sender, EventArgs e) { IGitHubRepo repo = await GetRandomGitHub(); UpdateLabels(repo); if (repo != null && repo.Id == 342959905) { MessageBox.Show("No way! I found myself!"); } }
private async Task <IGitHubRepo> GetRandomGitHub() { int tryMax; if (int.TryParse(configuration.GetSection("MaxId").Value, out int res)) { tryMax = res; } else { tryMax = 350000000; } IGitHubRepo repo = null; while (repo?.Name == null) { int id = rand.Next(1, tryMax); try { repo = await githubAPI.GetRepo(id); } catch (RateLimitedException) { MessageBox.Show("You cannnot make any more API calls, please try again later", "Out Of API Calls", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } catch (Exception ex) { MessageBox.Show($"Error Encountered during API Call:\n{ex.Message}", "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Try to waste less API calls if (repo.Name == null) { tryMax = id - 1; } } return(repo); }
private void UpdateLabels(IGitHubRepo repo) { lblIdValue.Text = repo?.Id.ToString() ?? ""; lblOwnerValue.Text = repo?.Owner.Login ?? ""; linkLabelOwner.Text = repo?.Owner.Html_url ?? ""; lblNameValue.Text = repo?.Name ?? ""; textBoxDesc.Text = repo?.Description ?? ""; linkLabelUrl.Text = repo?.Html_url ?? ""; lblLanguageValue.Text = repo?.Language ?? ""; lblForksValue.Text = repo?.Forks_count.ToString() ?? ""; lblStarredValue.Text = repo?.Stargazers_count.ToString() ?? ""; lblWatchersValue.Text = repo?.Watchers_count.ToString() ?? ""; if (githubAPI.RateLimitRemaining < 0) { lblRemainingValue.Text = "Unknown"; } else { lblRemainingValue.Text = githubAPI.RateLimitRemaining.ToString(); } }
public GitHubService(IGitHubRepo getHubRepo) { _getHubRepo = getHubRepo; }
static async Task Main(string[] args) { var serviceProvider = Bootstrap.Initialize(args); var config = serviceProvider.GetRequiredService <IConfiguration>(); var api = serviceProvider.GetRequiredService <IGithubAPI>(); IGitHubRepo repo = null; bool again = true; while (again) { if (int.TryParse(config.GetSection("MaxId").Value, out int res)) { tryMax = res; } else { tryMax = 300000000; } while (repo?.Name == null) { int id = rand.Next(1, tryMax); try { repo = await api.GetRepo(id); } catch (RateLimitedException) { Console.WriteLine("You cannot make any more API calls due to rate limiting. Try again later."); Environment.Exit(-1); } catch (Exception ex) { Console.WriteLine("Something went wrong with the API call!"); Console.WriteLine(ex.Message); } // Try to waste less API calls if (repo.Name == null) { tryMax = id - 1; } else { DisplayRepoInfo(repo); Console.WriteLine(); DisplayRateLimitingInfo(api); } } Console.WriteLine(); Console.Write("Would you like to find another (Y/n): "); var input = Console.ReadLine(); if (!string.IsNullOrEmpty(input) && Char.ToUpperInvariant(input[0]) == 'N') { again = false; } repo = null; Console.WriteLine(); } }