diff --git a/ReplayBrowser/Helpers/GitHubApiHelper.cs b/ReplayBrowser/Helpers/GitHubApiHelper.cs new file mode 100644 index 0000000..2fdedfd --- /dev/null +++ b/ReplayBrowser/Helpers/GitHubApiHelper.cs @@ -0,0 +1,116 @@ +using System.Net.Http; +using System.Text.Json; +using Microsoft.Extensions.Caching.Memory; +using Serilog; + +namespace ReplayBrowser.Helpers; + +public class GitHubApiHelper +{ + private readonly IMemoryCache _memoryCache; + private readonly string? _apiToken; + + public GitHubApiHelper(IConfiguration configuration, IMemoryCache memoryCache) + { + _memoryCache = memoryCache; + + try + { + _apiToken = configuration["GitHubAPIToken"]; + } + catch (Exception e) + { + Log.Error("GitHubAPIToken not set, contributors cannot be fetched."); + } + } + + public async Task> GetContributors() + { + if (!_memoryCache.TryGetValue("GitHubContributors", out List contributors)) + { + try + { + using (var httpClient = new HttpClient()) + { + using (var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/Simyon264/ReplayBrowser/contributors")) + { + request.Headers.TryAddWithoutValidation("Accept", "application/vnd.github+json"); + request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {_apiToken}"); + request.Headers.TryAddWithoutValidation("X-GitHub-Api-Version", "2022-11-28"); + request.Headers.TryAddWithoutValidation("User-Agent", "YourAppNameHere"); + + var response = await httpClient.SendAsync(request); + response.EnsureSuccessStatusCode(); + + var responseBody = await response.Content.ReadAsStringAsync(); + + var responseJson = JsonSerializer.Deserialize>(responseBody); + contributors = new List(); + + foreach (var obj in responseJson) + { + string accountName = null; + string accountImageURL = null; + string accountLink = null; + + if (obj.TryGetProperty("login", out var loginProperty)) + { + accountName = loginProperty.GetString(); + } + + if (obj.TryGetProperty("avatar_url", out var avatarUrlProperty)) + { + accountImageURL = avatarUrlProperty.GetString(); + } + + if (obj.TryGetProperty("html_url", out var htmlUrlProperty)) + { + accountLink = htmlUrlProperty.GetString(); + } + + if (accountName != null && accountImageURL != null && accountLink != null) + { + var account = new GitHubAccount + { + AccountName = accountName, + AccountImageUrl = accountImageURL, + AccountLink = accountLink + }; + + contributors.Add(account); + } + else + { + Log.Warning("A required property was missing in the JSON response while attempting to fetch project contributors."); + } + } + + Log.Information("Successfully fetched project contributor list."); + + // Set cache options and cache the contributors list + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetAbsoluteExpiration(TimeSpan.FromMinutes(30)); // Adjust expiration as needed + + _memoryCache.Set("GitHubContributors", contributors, cacheEntryOptions); + } + } + } + catch (Exception e) + { + Log.Error($"Exception when querying GitHub: {e.Message} - {e.StackTrace}"); + + // return empty list + return new List(); + } + } + + return contributors; + } +} + +public struct GitHubAccount +{ + public string AccountName { get; init; } + public string AccountImageUrl { get; init; } + public string AccountLink { get; init; } +} diff --git a/ReplayBrowser/Pages/Contributors.razor b/ReplayBrowser/Pages/Contributors.razor new file mode 100644 index 0000000..ba2d43e --- /dev/null +++ b/ReplayBrowser/Pages/Contributors.razor @@ -0,0 +1,34 @@ +@page "/contributors" + +@inject GitHubApiHelper GitHubApiHelper + +@using Microsoft.AspNetCore.Components.Web +@using ReplayBrowser.Pages.Shared +@using ReplayBrowser.Helpers + +Contributors + +

Contributors

+ +
+ @if (_contributors.Count == 0) + { +

Error fetching contributors from the GitHub API

+ } + else + { + @foreach (var contrib in _contributors) + { + + } + } +
+ +@code { + private List _contributors = new List(); + + protected async override Task OnInitializedAsync() + { + _contributors = await GitHubApiHelper.GetContributors(); + } +} \ No newline at end of file diff --git a/ReplayBrowser/Pages/Shared/ContributorCard.razor b/ReplayBrowser/Pages/Shared/ContributorCard.razor new file mode 100644 index 0000000..8ee85fc --- /dev/null +++ b/ReplayBrowser/Pages/Shared/ContributorCard.razor @@ -0,0 +1,29 @@ +
+
+

@ContributorName

+
+ +
+
+ Profile Picture of the Contributor +
+ +
+ + + GitHub + +
+
+ + +@code { + [Parameter] public required string ContributorName { get; set; } + [Parameter] public required string ContributorImage { get; set; } + [Parameter] public required string ContributorLink { get; set; } +} \ No newline at end of file diff --git a/ReplayBrowser/Pages/Shared/MainLayout.razor b/ReplayBrowser/Pages/Shared/MainLayout.razor index 59526df..6c43c49 100644 --- a/ReplayBrowser/Pages/Shared/MainLayout.razor +++ b/ReplayBrowser/Pages/Shared/MainLayout.razor @@ -109,7 +109,7 @@