using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; namespace Octokit { public class RepositoryInvitationsClient : ApiClient, IRepositoryInvitationsClient { public RepositoryInvitationsClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Accept a repository invitation. /// /// /// See the API documentation for more information. /// /// The id of the invitation /// Thrown when a general API error occurs. [ManualRoute("PATCH", "/user/repository_invitations/{invitation_id}")] public async Task Accept(int invitationId) { var endpoint = ApiUrls.UserInvitations(invitationId); try { var httpStatusCode = await Connection.Patch(endpoint).ConfigureAwait(false); return httpStatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Decline a repository invitation. /// /// /// See the API documentation for more information. /// /// The id of the invitation /// Thrown when a general API error occurs. [ManualRoute("DELETE", "/user/repository_invitations/{invitation_id}")] public async Task Decline(int invitationId) { var endpoint = ApiUrls.UserInvitations(invitationId); try { var httpStatusCode = await Connection.Delete(endpoint).ConfigureAwait(false); return httpStatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Deletes a repository invitation. /// /// /// See the API documentation for more information. /// /// The id of the repository /// The id of the invitation /// Thrown when a general API error occurs. [ManualRoute("DELETE", "/repos/:owner/:repo/invitations/{invitation_id}")] public async Task Delete(long repositoryId, int invitationId) { var endpoint = ApiUrls.RepositoryInvitations(repositoryId, invitationId); try { var httpStatusCode = await Connection.Delete(endpoint).ConfigureAwait(false); return httpStatusCode == HttpStatusCode.NoContent; } catch (NotFoundException) { return false; } } /// /// Gets all invitations for the current user. /// /// /// See the API documentation for more information. /// /// Thrown when a general API error occurs. [ManualRoute("GET", "/user/repository_invitations")] public Task> GetAllForCurrent() { return GetAllForCurrent(ApiOptions.None); } /// /// Gets all invitations for the current user. /// /// /// See the API documentation for more information. /// /// Options for changing the API response /// Thrown when a general API error occurs. [ManualRoute("GET", "/user/repository_invitations")] public Task> GetAllForCurrent(ApiOptions options) { Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.UserInvitations(), options); } /// /// Gets all the invitations on a repository. /// /// /// See the API documentation for more information. /// /// The id of the repository /// Thrown when a general API error occurs. [ManualRoute("GET", "/repositories/{id}/invitations")] public Task> GetAllForRepository(long repositoryId) { return GetAllForRepository(repositoryId, ApiOptions.None); } /// /// Gets all the invitations 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. [ManualRoute("GET", "/repositories/{id}/invitations")] public Task> GetAllForRepository(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryInvitations(repositoryId), options); } /// /// Updates a repository invitation. /// /// /// See the API documentation for more information. /// /// The id of the repository /// The id of the invitation /// The permission for the collsborator /// Thrown when a general API error occurs. [ManualRoute("PATCH", "/repositories/{id}/invitations/{invitation_id}")] public Task Edit(long repositoryId, int invitationId, InvitationUpdate permissions) { Ensure.ArgumentNotNull(permissions, nameof(permissions)); return ApiConnection.Patch(ApiUrls.RepositoryInvitations(repositoryId, invitationId), permissions); } } }