#if NET_45 using System.Collections.Generic; using System.Threading.Tasks; #endif namespace Octokit { /// /// A client for GitHub's Collaborators on a Repository. /// /// /// See the Collaborators API documentation for more details. /// public class RepoCollaboratorsClient : ApiClient, IRepoCollaboratorsClient { /// /// Initializes a new GitHub Repo Collaborators API client. /// /// An API connection. public RepoCollaboratorsClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Thrown when a general API error occurs. public Task> GetAll(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return GetAll(owner, name, ApiOptions.None); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Thrown when a general API error occurs. public Task> GetAll(int repositoryId) { return GetAll(repositoryId, ApiOptions.None); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Options for changing the API response /// Thrown when a general API error occurs. public Task> GetAll(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepoCollaborators(owner, name), options); } /// /// Gets all the collaborators on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Options for changing the API response /// Thrown when a general API error occurs. public Task> GetAll(int repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return ApiConnection.GetAll(ApiUrls.RepoCollaborators(repositoryId), options); } /// /// Checks if a user is a collaborator on a repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the prospective collaborator /// Thrown when a general API error occurs. public async Task IsCollaborator(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); try { var response = await Connection.Get(ApiUrls.RepoCollaborator(owner, name, user), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Checks if a user is a collaborator on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the prospective collaborator /// Thrown when a general API error occurs. public async Task IsCollaborator(int repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); try { var response = await Connection.Get(ApiUrls.RepoCollaborator(repositoryId, user), null, null).ConfigureAwait(false); return response.HttpResponse.IsTrue(); } catch (NotFoundException) { return false; } } /// /// Adds a new collaborator to the repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the new collaborator /// Thrown when a general API error occurs. public Task Add(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); return ApiConnection.Put(ApiUrls.RepoCollaborator(owner, name, user)); } /// /// Adds a new collaborator to the repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the new collaborator /// Thrown when a general API error occurs. public Task Add(int repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); return ApiConnection.Put(ApiUrls.RepoCollaborator(repositoryId, user)); } /// /// Deletes a collaborator from the repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Username of the deleted collaborator /// Thrown when a general API error occurs. public Task Delete(string owner, string name, string user) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(user, "user"); return ApiConnection.Delete(ApiUrls.RepoCollaborator(owner, name, user)); } /// /// Deletes a collaborator from the repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Username of the deleted collaborator /// Thrown when a general API error occurs. public Task Delete(int repositoryId, string user) { Ensure.ArgumentNotNullOrEmptyString(user, "user"); return ApiConnection.Delete(ApiUrls.RepoCollaborator(repositoryId, user)); } } }