using System; using System.Net; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; namespace Octokit { /// /// Extensions for working with the /// public static class ApiExtensions { /// /// Gets all API resources in the list at the specified URI. /// /// Type of the API resource in the list. /// The connection to use /// URI of the API resource to get /// of the API resources in the list. /// Thrown when an API error occurs. public static Task> GetAll(this IApiConnection connection, Uri uri) { Ensure.ArgumentNotNull(connection, nameof(connection)); Ensure.ArgumentNotNull(uri, nameof(uri)); return connection.GetAll(uri, ApiOptions.None); } /// /// Gets the HTML content of the API resource at the specified URI. /// /// The connection to use /// URI of the API resource to get /// The API resource's HTML content. /// Thrown when an API error occurs. public static Task GetHtml(this IApiConnection connection, Uri uri) { Ensure.ArgumentNotNull(connection, nameof(connection)); Ensure.ArgumentNotNull(uri, nameof(uri)); return connection.GetHtml(uri, null); } /// /// Performs an asynchronous HTTP GET request that expects a containing HTML. /// /// The connection to use /// URI endpoint to send request to /// representing the received HTTP response public static Task> GetHtml(this IConnection connection, Uri uri) { Ensure.ArgumentNotNull(connection, nameof(connection)); Ensure.ArgumentNotNull(uri, nameof(uri)); return connection.GetHtml(uri, null); } /// /// Gets the API resource at the specified URI. /// /// Type of the API resource to get. /// The connection to use /// URI of the API resource to get /// The API resource. /// Thrown when an API error occurs. public static Task> GetResponse(this IConnection connection, Uri uri) { Ensure.ArgumentNotNull(connection, nameof(connection)); Ensure.ArgumentNotNull(uri, nameof(uri)); return connection.Get(uri, null); } /// /// Gets the API resource at the specified URI. /// /// Type of the API resource to get. /// The connection to use /// URI of the API resource to get /// A token used to cancel the GetResponse request /// The API resource. /// Thrown when an API error occurs. public static Task> GetResponse(this IConnection connection, Uri uri, CancellationToken cancellationToken) { Ensure.ArgumentNotNull(connection, nameof(connection)); Ensure.ArgumentNotNull(uri, nameof(uri)); return connection.Get(uri, null, null, cancellationToken); } /// /// Returns true if the API call represents a true response, or false if it represents a false response. /// Throws an exception if the HTTP status does not match either a true or false response. /// /// /// Some API endpoints return a 204 for "true" and 404 for false. See https://developer.github.com/v3/activity/starring/#check-if-you-are-starring-a-repository /// for one example. This encapsulates that logic. /// /// Thrown if the status is neither 204 nor 404 /// True for a 204 response, False for a 404 /// public static bool IsTrue(this IResponse response) { Ensure.ArgumentNotNull(response, nameof(response)); if (response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.NoContent) { throw new ApiException("Invalid Status Code returned. Expected a 204 or a 404", response.StatusCode); } return response.StatusCode == HttpStatusCode.NoContent; } } }