using System.Collections.Generic; using System.Net; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Activity Starring API. /// /// /// See the Activity Starring API documentation for more information. /// public class StarredClient : ApiClient, IStarredClient { /// /// Instantiates a new GitHub Activity Starring API client. /// /// An API connection public StarredClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Retrieves all of the stargazers for the passed repository. /// /// The owner of the repository /// The name of the repository /// Thrown if the client is not authenticated. public Task> GetAllStargazers(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return GetAllStargazers(owner, name, ApiOptions.None); } /// /// Retrieves all of the stargazers for the passed repository. /// /// The ID of the repository /// Thrown if the client is not authenticated. public Task> GetAllStargazers(int repositoryId) { return GetAllStargazers(repositoryId, ApiOptions.None); } /// /// Retrieves all of the stargazers for the passed repository. /// /// The owner of the repository /// The name of the repository /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllStargazers(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Stargazers(owner, name), options); } /// /// Retrieves all of the stargazers for the passed repository. /// /// The ID of the repository /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllStargazers(int repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Stargazers(repositoryId), options); } /// /// Retrieves all of the stargazers for the passed repository with star creation timestamps. /// /// The owner of the repository /// The name of the repository /// Thrown if the client is not authenticated. public Task> GetAllStargazersWithTimestamps(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return GetAllStargazersWithTimestamps(owner, name, ApiOptions.None); } /// /// Retrieves all of the stargazers for the passed repository with star creation timestamps. /// /// The ID of the repository /// Thrown if the client is not authenticated. public Task> GetAllStargazersWithTimestamps(int repositoryId) { return GetAllStargazersWithTimestamps(repositoryId, ApiOptions.None); } /// /// Retrieves all of the stargazers for the passed repository with star creation timestamps. /// /// The owner of the repository /// The name of the repository /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllStargazersWithTimestamps(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Stargazers(owner, name), null, AcceptHeaders.StarCreationTimestamps, options); } /// /// Retrieves all of the stargazers for the passed repository with star creation timestamps. /// /// The ID of the repository /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllStargazersWithTimestamps(int repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Stargazers(repositoryId), null, AcceptHeaders.StarCreationTimestamps, options); } /// /// Retrieves all of the starred (ies) for the current user. /// /// Thrown if the client is not authenticated. public Task> GetAllForCurrent() { return GetAllForCurrent(ApiOptions.None); } /// /// Retrieves all of the starred (ies) for the current user. /// /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForCurrent(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Starred(), options); } /// /// Retrieves all of the starred (ies) for the current user with star creation timestamps. /// /// Thrown if the client is not authenticated. public Task> GetAllForCurrentWithTimestamps() { return GetAllForCurrentWithTimestamps(ApiOptions.None); } /// /// Retrieves all of the starred (ies) for the current user with star creation timestamps. /// /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForCurrentWithTimestamps(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Starred(), null, AcceptHeaders.StarCreationTimestamps, options); } /// /// Retrieves all of the starred (ies) for the current user. /// /// Star-specific request parameters that sort the results /// Thrown if the client is not authenticated. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "But i think i do need star-specific request parameters")] public Task> GetAllForCurrent(StarredRequest request) { Ensure.ArgumentNotNull(request, "request"); return GetAllForCurrent(request, ApiOptions.None); } /// /// Retrieves all of the starred (ies) for the current user. /// /// Star-specific request parameters that sort the results /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForCurrent(StarredRequest request, ApiOptions options) { Ensure.ArgumentNotNull(request, "request"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Starred(), request.ToParametersDictionary(), options); } /// /// Retrieves all of the starred (ies) for the current user with star creation timestamps. /// /// Star-specific request parameters that sort the results /// Thrown if the client is not authenticated. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "But i think i do need star-specific request parameters")] public Task> GetAllForCurrentWithTimestamps(StarredRequest request) { Ensure.ArgumentNotNull(request, "request"); return GetAllForCurrentWithTimestamps(request, ApiOptions.None); } /// /// Retrieves all of the starred (ies) for the current user with star creation timestamps. /// /// Star-specific request parameters that sort the results /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForCurrentWithTimestamps(StarredRequest request, ApiOptions options) { Ensure.ArgumentNotNull(request, "request"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.Starred(), request.ToParametersDictionary(), AcceptHeaders.StarCreationTimestamps, options); } /// /// Retrieves all of the (ies) starred by the specified user. /// /// The login of the user /// Thrown if the client is not authenticated. public Task> GetAllForUser(string user) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); return GetAllForUser(user, ApiOptions.None); } /// /// Retrieves all of the (ies) starred by the specified user. /// /// The login of the user /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForUser(string user, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.StarredByUser(user), options); } /// /// Retrieves all of the (ies) starred by the specified user with star creation timestamps. /// /// The login of the user /// Thrown if the client is not authenticated. public Task> GetAllForUserWithTimestamps(string user) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); return GetAllForUserWithTimestamps(user, ApiOptions.None); } /// /// Retrieves all of the (ies) starred by the specified user with star creation timestamps. /// /// The login of the user /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForUserWithTimestamps(string user, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.StarredByUser(user), null, AcceptHeaders.StarCreationTimestamps, options); } /// /// Retrieves all of the (ies) starred by the specified user. /// /// The login of the user /// Star-specific request parameters that sort the results /// Thrown if the client is not authenticated. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] public Task> GetAllForUser(string user, StarredRequest request) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(request, "request"); return GetAllForUser(user, request, ApiOptions.None); } /// /// Retrieves all of the (ies) starred by the specified user. /// /// The login of the user /// Star-specific request parameters that sort the results /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForUser(string user, StarredRequest request, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(request, "request"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.StarredByUser(user), request.ToParametersDictionary(), options); } /// /// Retrieves all of the (ies) starred by the specified user with star creation timestamps. /// /// The login of the user /// Star-specific request parameters that sort the results /// Thrown if the client is not authenticated. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] public Task> GetAllForUserWithTimestamps(string user, StarredRequest request) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(request, "request"); return GetAllForUserWithTimestamps(user, request, ApiOptions.None); } /// /// Retrieves all of the (ies) starred by the specified user with star creation timestamps. /// /// The login of the user /// Star-specific request parameters that sort the results /// Options for changing the API response /// Thrown if the client is not authenticated. public Task> GetAllForUserWithTimestamps(string user, StarredRequest request, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); Ensure.ArgumentNotNull(request, "request"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.StarredByUser(user), request.ToParametersDictionary(), AcceptHeaders.StarCreationTimestamps, options); } /// /// Check if a repository is starred by the current authenticated user. /// /// The owner of the repository /// The name of the repository /// Thrown if the client is not authenticated. public async Task CheckStarred(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); try { var response = await Connection.Get(ApiUrls.Starred(owner, name), null, null).ConfigureAwait(false); return response.HttpResponse.StatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Stars a repository for the authenticated user. /// /// The owner of the repository to star /// The name of the repository to star public async Task StarRepo(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); try { var response = await Connection.Put(ApiUrls.Starred(owner, name), null, null).ConfigureAwait(false); return response.HttpResponse.StatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Unstars a repository for the authenticated user. /// /// The owner of the repository to unstar /// The name of the repository to unstar public async Task RemoveStarFromRepo(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); try { var statusCode = await Connection.Delete(ApiUrls.Starred(owner, name)).ConfigureAwait(false); return statusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } } }