Last active
June 20, 2024 20:37
-
-
Save CodeByAidan/2a0f14241504cc3a8a8174d06bec2121 to your computer and use it in GitHub Desktop.
Simple 0 external dependency C# script that uses the GitHub API to fetch information about a specific repository
This file contains hidden or 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
| using System; | |
| using System.Net.Http; | |
| using System.Text.Json; | |
| using System.Threading.Tasks; | |
| #nullable enable | |
| public class GitHubApiClient | |
| { | |
| public async Task<Result<Repository>> GetRepositoryInfoAsync(string owner, string repo) | |
| { | |
| using var client = new HttpClient | |
| { | |
| BaseAddress = new Uri("https://api.github.com/") | |
| }; | |
| client.DefaultRequestHeaders.Add("User-Agent", "CSharpApp"); | |
| string url = $"repos/{owner}/{repo}"; | |
| var response = await client.GetAsync(url); | |
| if (response.StatusCode != System.Net.HttpStatusCode.OK) | |
| { | |
| return new Result<Repository>($"Failed to fetch data: {response.StatusCode}"); | |
| } | |
| var json = await response.Content.ReadAsStringAsync(); | |
| var repository = JsonSerializer.Deserialize<Repository>(json, new JsonSerializerOptions | |
| { | |
| PropertyNameCaseInsensitive = true, | |
| Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, | |
| AllowTrailingCommas = true, | |
| PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | |
| ReadCommentHandling = JsonCommentHandling.Skip | |
| }); | |
| if (repository is null) | |
| { | |
| return new Result<Repository>("Deserialization resulted in null."); | |
| } | |
| return new Result<Repository>(repository); | |
| } | |
| } | |
| public record Repository | |
| { | |
| public string? Name { get; init; } | |
| public string? Description { get; init; } | |
| public int Stargazers_Count { get; init; } | |
| public string? Default_Branch { get; init; } | |
| } | |
| public class Result<T> where T : class | |
| { | |
| public bool IsSuccess { get; } | |
| public T? Value { get; } | |
| public string? Error { get; } | |
| public Result(T value) | |
| { | |
| IsSuccess = true; | |
| Value = value; | |
| } | |
| public Result(string error) | |
| { | |
| IsSuccess = false; | |
| Error = error; | |
| } | |
| } | |
| class Program | |
| { | |
| static async Task Main(string[] args) | |
| { | |
| var client = new GitHubApiClient(); | |
| var result = await client.GetRepositoryInfoAsync("dotnet", "runtime"); | |
| if (result.IsSuccess && result.Value != null) | |
| { | |
| Console.WriteLine($"Repository: {result.Value.Name ?? "Name not available"}"); | |
| Console.WriteLine($"Description: {result.Value.Description ?? "Description not available"}"); | |
| Console.WriteLine($"Stars: {result.Value.Stargazers_Count}"); | |
| Console.WriteLine($"Default Branch: {result.Value.Default_Branch ?? "Default branch not available"}"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"Error: {result.Error}"); | |
| } | |
| } | |
| } | |
| #nullable disable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment