using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; using System.Collections.ObjectModel; namespace Octokit { /// /// A client for GitHub's miscellaneous APIs. /// /// /// See the Miscellaneous API documentation for more details. /// public class MiscellaneousClient : IMiscellaneousClient { readonly IConnection _connection; /// /// Initializes a new GitHub miscellaneous API client. /// /// An API connection public MiscellaneousClient(IConnection connection) { Ensure.ArgumentNotNull(connection, "connection"); _connection = connection; } /// /// Gets all the emojis available to use on GitHub. /// /// Thrown when a general API error occurs. /// An of emoji and their URI. public async Task> GetAllEmojis() { var endpoint = new Uri("emojis", UriKind.Relative); var response = await _connection.Get>(endpoint, null, null).ConfigureAwait(false); return new ReadOnlyCollection( response.Body.Select(kvp => new Emoji(kvp.Key, kvp.Value)).ToArray()); } /// /// Gets the rendered Markdown for the specified plain-text Markdown document. /// /// A plain-text Markdown document /// Thrown when a general API error occurs. /// The rendered Markdown. public async Task RenderRawMarkdown(string markdown) { var endpoint = new Uri("markdown/raw", UriKind.Relative); var response = await _connection.Post(endpoint, markdown, "text/html", "text/plain").ConfigureAwait(false); return response.Body; } /// /// Gets the rendered Markdown for an arbitrary markdown document. /// /// An arbitrary Markdown document /// Thrown when a general API error occurs. /// The rendered Markdown. public async Task RenderArbitraryMarkdown(NewArbitraryMarkdown markdown) { var endpoint = new Uri("markdown", UriKind.Relative); var response = await _connection.Post(endpoint, markdown, "text/html", "text/plain").ConfigureAwait(false); return response.Body; } /// /// List all templates available to pass as an option when creating a repository. /// /// A list of template names public async Task> GetAllGitIgnoreTemplates() { var endpoint = new Uri("gitignore/templates", UriKind.Relative); var response = await _connection.Get(endpoint, null, null).ConfigureAwait(false); return new ReadOnlyCollection(response.Body); } /// /// Retrieves the source for a single GitIgnore template /// /// /// A template and its source public async Task GetGitIgnoreTemplate(string templateName) { Ensure.ArgumentNotNullOrEmptyString(templateName, "templateName"); var endpoint = new Uri("gitignore/templates/" + Uri.EscapeUriString(templateName), UriKind.Relative); var response = await _connection.Get(endpoint, null, null).ConfigureAwait(false); return response.Body; } /// /// Returns a list of the licenses shown in the license picker on GitHub.com. This is not a comprehensive /// list of all possible OSS licenses. /// /// This is a PREVIEW API! Use it at your own risk. /// A list of licenses available on the site public async Task> GetAllLicenses() { var endpoint = new Uri("licenses", UriKind.Relative); var response = await _connection.Get(endpoint, null, AcceptHeaders.LicensesApiPreview).ConfigureAwait(false); return new ReadOnlyCollection(response.Body); } /// /// Retrieves a license based on the license key such as "mit" /// /// /// A that includes the license key, text, and attributes of the license. public async Task GetLicense(string key) { var endpoint = new Uri("licenses/" + Uri.EscapeUriString(key), UriKind.Relative); var response = await _connection.Get(endpoint, null, AcceptHeaders.LicensesApiPreview).ConfigureAwait(false); return response.Body; } /// /// Gets API Rate Limits (API service rather than header info). /// /// Thrown when a general API error occurs. /// An of Rate Limits. [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public async Task GetRateLimits() { var endpoint = new Uri("rate_limit", UriKind.Relative); var response = await _connection.Get(endpoint, null, null).ConfigureAwait(false); return response.Body; } /// /// Retrieves information about GitHub.com, the service or a GitHub Enterprise installation. /// /// Thrown when a general API error occurs. /// An containing metadata about the GitHub instance. [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public async Task GetMetadata() { var endpoint = new Uri("meta", UriKind.Relative); var response = await _connection.Get(endpoint, null, null).ConfigureAwait(false); return response.Body; } } }