diff --git a/src/Monambike.Core/Data/Config.cs b/src/Monambike.Core/Data/Config.cs new file mode 100644 index 0000000..519067d --- /dev/null +++ b/src/Monambike.Core/Data/Config.cs @@ -0,0 +1,16 @@ +namespace Monambike.Core.Data +{ + /// + /// Provides access to configuration settings. + /// + internal static class Config + { + /// + /// Gets the GitHub token from the environment variables. + /// + /// + /// The GitHub token is retrieved from the environment variable "GITHUB_REPOSITORIES_TOKEN". + /// + internal static string GitHubToken => Environment.GetEnvironmentVariable("GITHUB_REPOSITORIES_TOKEN")!; + } +} diff --git a/src/Monambike.Core/Entities/Certificate.cs b/src/Monambike.Core/Entities/Certificate.cs new file mode 100644 index 0000000..1af344b --- /dev/null +++ b/src/Monambike.Core/Entities/Certificate.cs @@ -0,0 +1,17 @@ +namespace Monambike.Core.Entities +{ + internal class Certificate + { + internal required string Name { get; init; } + + internal required string Organization { get; init; } + + internal DateOnly IssueDate { get; init; } + + internal DateOnly ExpirationDate { get; init; } + + internal ulong CredentialId { get; init; } + + internal string CredentialUrl { get; init; } = null!; + } +} diff --git a/src/Monambike.Core/Entities/GithubRepository.cs b/src/Monambike.Core/Entities/GithubRepository.cs new file mode 100644 index 0000000..8a8c179 --- /dev/null +++ b/src/Monambike.Core/Entities/GithubRepository.cs @@ -0,0 +1,45 @@ +using System.Drawing; + +namespace Monambike.Core.Entities +{ + /// + /// Represents a GitHub repository. + /// + public class GithubRepository + { + /// + /// Gets or sets the name of the GitHub repository. + /// + public required string Name { get; set; } + + /// + /// Gets or sets a value indicating whether the GitHub repository is private. + /// + public required bool Private { get; set; } + + /// + /// Gets or sets the URL of the GitHub repository. + /// + public required string Url { get; set; } + + /// + /// Gets or sets the programming language used in the GitHub repository. Can be null. + /// + public string? Language { get; set; } = null; + + /// + /// Gets or sets the color associated with the programming language of the GitHub repository. Can be null. + /// + public Color? LanguageColor { get; set; } = null; + + /// + /// Gets or sets the date and time when the GitHub repository was created. + /// + public required DateTimeOffset CreateDate { get; set; } + + /// + /// Gets or sets the date and time of the last push to the GitHub repository. Can be null. + /// + public required DateTimeOffset? PushDate { get; set; } + } +} diff --git a/src/Monambike.Core/Models/Github.cs b/src/Monambike.Core/Models/Github.cs new file mode 100644 index 0000000..2831288 --- /dev/null +++ b/src/Monambike.Core/Models/Github.cs @@ -0,0 +1,65 @@ +using Monambike.Core.Data; +using Monambike.Core.Entities; +using Octokit; + +namespace Monambike.Core.Models +{ + /// + /// Provides methods for interacting with GitHub repositories. + /// + public static class Github + { + /// + /// Gets the product header value for API requests. + /// + private static ProductHeaderValue ProductHeaderValue => new("monambike-core"); + + /// + /// Gets the GitHub client instance for making API requests. + /// + private static GitHubClient GitHub => new(ProductHeaderValue) { Credentials = new Credentials(Config.GitHubToken) }; + + /// + /// Asynchronously retrieves a list of repositories for the current user. + /// + /// A task representing the asynchronous operation, containing a list of repositories. + private static async Task> GetRepositories() => await GitHub.Repository.GetAllForCurrent(); + + public static List GetGithubRepositories() + { + // Fetch repositories from GitHub. + var repositories = GetRepositories().Result; + + // Fetch language colors from GitHub by a third-party API. + var languageColors = GithubLanguage.GetLanguageColors().Result; + + // Initialize a list to store GitHub repositories. + var githubRepositories = new List(); + + // Iterate through each repository to map language colors. + foreach (var repository in repositories) + { + // Retrieve the language color corresponding to the repository's language. + var languageColor = languageColors.SingleOrDefault(language => language.Name == repository.Language); + + // Create a GithubRepository object and populate its properties. + var test = new GithubRepository + { + Name = repository.Name, + CreateDate = repository.CreatedAt, + PushDate = repository.PushedAt, + Private = repository.Private, + Url = repository.Url, + Language = languageColor?.Name, + LanguageColor = languageColor?.RgbColor + }; + + // Add the GithubRepository object to the list. + githubRepositories.Add(test); + } + + // Return the GithubRepositories object list. + return githubRepositories; + } + } +} \ No newline at end of file diff --git a/src/Monambike.Core/Models/GithubLanguage.cs b/src/Monambike.Core/Models/GithubLanguage.cs new file mode 100644 index 0000000..2f35516 --- /dev/null +++ b/src/Monambike.Core/Models/GithubLanguage.cs @@ -0,0 +1,78 @@ +using Monambike.Core.Services; +using Newtonsoft.Json; +using System.Drawing; + +namespace Monambike.Core.Models +{ + /// + /// Provides functionality to retrieve language colors from GitHub using a third-party. + /// + internal static class GithubLanguage + { + /// + /// Gets the HTTP service instance for making requests to third-party API. + /// + private static HttpService TestHttpService => new("https://raw.githubusercontent.com/ozh/github-colors/master/"); + + /// + /// Asynchronously retrieves language colors like from GitHub. + /// + /// A list of LanguageColor objects representing language names and their corresponding colors. + internal static async Task> GetLanguageColors() + { + // Retrieve JSON data containing language colors from GitHub. + var jsonColors = await TestHttpService.GetAsync>("colors.json"); + + // Throw ArgumentNullException if the retrieved JSON colors are null. + ArgumentNullException.ThrowIfNull(jsonColors); + + // Create the list for holding the LanguageColor objects. + var languageColors = new List(); + + // Convert JSON colors to LanguageColor objects. + foreach (var jsonColor in jsonColors) + languageColors.Add(new LanguageColor + { + Name = jsonColor.Key, + HexColor = jsonColor.Value.Color + }); + + // Return the list of LanguageColor objects. + return languageColors; + } + + /// + /// Represents the JSON structure for language colors retrieved from GitHub. + /// + public class JsonColor + { + /// + /// Gets or initializes the hexadecimal color code. + /// + + [JsonProperty("color")] + public required string Color { get; init; } + } + + /// + /// Represents a programming language with its corresponding color. + /// + public class LanguageColor + { + /// + /// Gets or initializes the name of the programming language. + /// + public required string Name { get; init; } + + /// + /// Gets or initializes the hexadecimal color code. + /// + public required string HexColor { get; init; } + + /// + /// Gets the RGB color equivalent of the hexadecimal color code. + /// + public Color RgbColor => ColorTranslator.FromHtml(HexColor); + } + } +} diff --git a/src/Monambike.Core/Models/GithubProjects.cs b/src/Monambike.Core/Models/GithubProjects.cs deleted file mode 100644 index 8d9ddd3..0000000 --- a/src/Monambike.Core/Models/GithubProjects.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Monambike.Core.Data; -using Newtonsoft.Json; - -namespace Monambike.Core.Models -{ - public class GithubProjects - { - /// - /// HttpClient for making api requests. - /// - readonly static HttpClient HttpClient = new() { BaseAddress = new Uri("https://api.github.com/repos/") }; - - //internal static async Task Get() - //{ - // var response = await HttpService.GetAsync(HttpClient, $"monambike"); - // return JsonConvert.DeserializeObject(response); - //} - - internal class GithubResponse - { - [JsonProperty("name")] - internal string Name { get; set; } - - [JsonProperty("private")] - internal bool Private { get; set; } - - [JsonProperty("html_url")] - internal string Url { get; set; } - - [JsonProperty("language")] - internal string Language { get; set; } - - [JsonProperty("created_at")] - internal string CreateDate { get; set; } - - [JsonProperty("pushed_at")] - internal string PushDate { get; set; } - } - } -} diff --git a/src/Monambike.Core/Monambike.Core.csproj b/src/Monambike.Core/Monambike.Core.csproj index 0244cf6..9e7b8d1 100644 --- a/src/Monambike.Core/Monambike.Core.csproj +++ b/src/Monambike.Core/Monambike.Core.csproj @@ -6,10 +6,12 @@ enable True https://github.com/monambike/monambike-core + + diff --git a/src/Monambike.Core/Services/HttpService.cs b/src/Monambike.Core/Services/HttpService.cs new file mode 100644 index 0000000..f63c840 --- /dev/null +++ b/src/Monambike.Core/Services/HttpService.cs @@ -0,0 +1,45 @@ +using Newtonsoft.Json; + +namespace Monambike.Core.Services +{ + /// + /// A simple HTTP service for making GET requests and retrieving the response content. + /// + public class HttpService(string httpClientBaseAddress) + { + /// + /// Base address for making api requests. + /// + public Uri Address => new(httpClientBaseAddress); + + /// + /// HttpClient for making api requests. + /// + public HttpClient HttpClient => new() { BaseAddress = Address }; + + /// + /// Sends an asynchronous GET request to the specified URI and returns the response content as a string. + /// + /// The HttpClient instance to use for the request. + /// The URI of the resource to request. + /// A Task representing the asynchronous operation. The result contains the response content as a string. + public async Task GetAsync(string strRequest) + { + // Tries to make the request with the base address and the request + // for URI validation. + Uri.TryCreate($"{HttpClient.BaseAddress}{strRequest}", UriKind.Absolute, out Uri? request); + + // If current URI is valid, makes the requisition and returns the response. + using HttpResponseMessage response = await HttpClient.GetAsync(request); + + // Assigning assynchronously the content response as string. + var content = await response.Content.ReadAsStringAsync(); + + // Deserializing object into the provided class. + var deserializedObject = JsonConvert.DeserializeObject(content); + + // Returning deserialized object content response into the provided class. + return deserializedObject; + } + } +} \ No newline at end of file diff --git a/src/Monambike.Test/MainUnitTest.cs b/src/Monambike.Test/MainUnitTest.cs index 0a896a1..98cde26 100644 --- a/src/Monambike.Test/MainUnitTest.cs +++ b/src/Monambike.Test/MainUnitTest.cs @@ -1,6 +1,3 @@ -using Monambike.Core.Data; -using System.Diagnostics; - namespace Monambike.Test { [TestClass] @@ -9,7 +6,7 @@ public class MainUnitTest [TestMethod] public void Main() { - Debug.WriteLine(Links.WebsiteLearn); + var test = Core.Models.Github.GetGithubRepositories(); } } } \ No newline at end of file diff --git a/src/Monambike.Test/Monambike.Test.csproj b/src/Monambike.Test/Monambike.Test.csproj index 431a177..0c945f5 100644 --- a/src/Monambike.Test/Monambike.Test.csproj +++ b/src/Monambike.Test/Monambike.Test.csproj @@ -10,9 +10,9 @@ - - - + + +