using System;
using System.Reactive;
using System.Reactive.Threading.Tasks;
using Octokit.Reactive.Internal;
namespace Octokit.Reactive
{
///
/// A client for GitHub's Org Teams API.
///
///
/// See the Orgs API documentation for more information.
///
public class ObservableTeamsClient : IObservableTeamsClient
{
readonly IConnection _connection;
readonly ITeamsClient _client;
///
/// Initializes a new Organization Teams API client.
///
/// An used to make the requests
public ObservableTeamsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, nameof(client));
_connection = client.Connection;
_client = client.Organization.Team;
}
///
/// Gets a single by identifier.
///
///
/// https://developer.github.com/v3/orgs/teams/#get-team
///
/// The team identifier.
/// The with the given identifier.
public IObservable Get(int id)
{
return _client.Get(id).ToObservable();
}
///
/// Returns all s for the current org.
///
/// Thrown when a general API error occurs.
/// A list of the orgs's teams s.
public IObservable GetAll(string org)
{
Ensure.ArgumentNotNullOrEmptyString(org, nameof(org));
return GetAll(org, ApiOptions.None);
}
///
/// Returns all s for the current org.
///
/// Organization to list all teams of.
/// Options to change API behaviour.
/// Thrown when a general API error occurs.
/// A list of the orgs's teams s.
public IObservable GetAll(string org, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(org, nameof(org));
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages(ApiUrls.OrganizationTeams(org), options);
}
///
/// Returns all s for the current user.
///
/// Thrown when a general API error occurs.
/// A list of the user's s.
public IObservable GetAllForCurrent()
{
return GetAllForCurrent(ApiOptions.None);
}
///
/// Returns all s for the current user.
///
/// Options to change API behaviour.
/// Thrown when a general API error occurs.
/// A list of the user's s.
public IObservable GetAllForCurrent(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages(ApiUrls.UserTeams(), options);
}
///
/// Returns all child teams of the given team.
///
/// The team identifier
///
/// https://developer.github.com/v3/orgs/teams/#list-child-teams
///
public IObservable GetAllChildTeams(int id)
{
return GetAllChildTeams(id, ApiOptions.None);
}
///
/// Returns all child teams of the given team.
///
///
/// https://developer.github.com/v3/orgs/teams/#list-child-teams
///
/// The team identifier
/// Options to change API behaviour.
public IObservable GetAllChildTeams(int id, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages(ApiUrls.TeamChildTeams(id), options);
}
///
/// Returns all members of the given team.
///
/// The team identifier
///
/// https://developer.github.com/v3/orgs/teams/#list-team-members
///
/// Thrown when a general API error occurs.
/// A list of the team's member s.
public IObservable GetAllMembers(int id)
{
return GetAllMembers(id, ApiOptions.None);
}
///
/// Returns all members of the given team.
///
///
/// https://developer.github.com/v3/orgs/teams/#list-team-members
///
/// The team identifier
/// Options to change API behaviour.
/// Thrown when a general API error occurs.
/// A list of the team's member s.
public IObservable GetAllMembers(int id, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages(ApiUrls.TeamMembers(id), options);
}
///
/// Returns all members with the specified role in the given team of the given role.
///
///
/// https://developer.github.com/v3/orgs/teams/#list-team-members
///
/// The team identifier
/// The request filter
public IObservable GetAllMembers(int id, TeamMembersRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return GetAllMembers(id, request, ApiOptions.None);
}
///
/// Returns all members with the specified role in the given team of the given role.
///
///
/// https://developer.github.com/v3/orgs/teams/#list-team-members
///
/// The team identifier
/// The request filter
/// Options to change API behaviour.
public IObservable GetAllMembers(int id, TeamMembersRequest request, ApiOptions options)
{
Ensure.ArgumentNotNull(request, nameof(request));
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages(ApiUrls.TeamMembers(id), request.ToParametersDictionary(), options);
}
///
/// Returns newly created for the current org.
///
/// Thrown when a general API error occurs.
/// Newly created
public IObservable Create(string org, NewTeam team)
{
Ensure.ArgumentNotNullOrEmptyString(org, nameof(org));
Ensure.ArgumentNotNull(team, nameof(team));
return _client.Create(org, team).ToObservable();
}
///
/// Returns updated for the current org.
///
/// Thrown when a general API error occurs.
/// Updated
public IObservable Update(int id, UpdateTeam team)
{
Ensure.ArgumentNotNull(team, nameof(team));
return _client.Update(id, team).ToObservable();
}
///
/// Delete a team - must have owner permissions to this
///
/// Thrown when a general API error occurs.
///
public IObservable Delete(int id)
{
return _client.Delete(id).ToObservable();
}
///
/// Adds a to a .
///
///
/// See the API documentation for more information.
///
/// The team identifier.
/// The user to add to the team.
/// Additional parameters for the request
public IObservable AddOrEditMembership(int id, string login, UpdateTeamMembership request)
{
Ensure.ArgumentNotNullOrEmptyString(login, nameof(login));
Ensure.ArgumentNotNull(request, nameof(request));
return _client.AddOrEditMembership(id, login, request).ToObservable();
}
///
/// Removes a from a .
///
///
/// See the API documentation for more information.
///
/// The team identifier.
/// The user to remove from the team.
/// if the user was removed from the team; otherwise.
public IObservable RemoveMembership(int id, string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, nameof(login));
return _client.RemoveMembership(id, login).ToObservable();
}
///
/// Gets whether the user with the given
/// is a member of the team with the given .
/// A is thrown if the user is not a member.
///
///
/// See the API documentation for more information.
///
/// The team to check.
/// The user to check.
public IObservable GetMembershipDetails(int id, string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, nameof(login));
return _client.GetMembershipDetails(id, login).ToObservable();
}
///
/// Returns all team's repositories.
///
/// Thrown when a general API error occurs.
/// The team's repositories
public IObservable GetAllRepositories(int id)
{
return GetAllRepositories(id, ApiOptions.None);
}
///
/// Returns all team's repositories.
///
/// Team Id.
/// Options to change API behaviour.
/// Thrown when a general API error occurs.
/// The team's repositories
public IObservable GetAllRepositories(int id, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages(ApiUrls.TeamRepositories(id), options);
}
///
/// Adds a to a .
///
/// The team identifier.
/// Org to associate the repo with.
/// Name of the repo.
/// Thrown if you attempt to add a repository to a team that is not owned by the organization.
///
/// See the API documentation for more information.
///
/// if the repository was added to the team; otherwise.
public IObservable AddRepository(int id, string organization, string repoName)
{
Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization));
Ensure.ArgumentNotNullOrEmptyString(repoName, nameof(repoName));
return _client.AddRepository(id, organization, repoName).ToObservable();
}
///
/// Adds a to a .
///
/// The team identifier.
/// Org to associate the repo with.
/// Name of the repo.
/// The permission to grant the team on this repository.
/// Thrown if you attempt to add a repository to a team that is not owned by the organization.
///
/// See the API documentation for more information.
///
/// if the repository was added to the team; otherwise.
public IObservable AddRepository(int id, string organization, string repoName, RepositoryPermissionRequest permission)
{
Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization));
Ensure.ArgumentNotNullOrEmptyString(repoName, nameof(repoName));
return _client.AddRepository(id, organization, repoName, permission).ToObservable();
}
///
/// Remove a repository from the team
///
/// Thrown when a general API error occurs.
///
public IObservable RemoveRepository(int id, string organization, string repoName)
{
Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization));
Ensure.ArgumentNotNullOrEmptyString(repoName, nameof(repoName));
return _client.RemoveRepository(id, organization, repoName).ToObservable();
}
///
/// Gets whether or not the given repository is managed by the given team.
///
/// The team identifier
/// Owner of the org the team is associated with.
/// Name of the repo.
///
/// See the API documentation for more information.
///
/// if the repository is managed by the given team; otherwise.
public IObservable IsRepositoryManagedByTeam(int id, string owner, string repo)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(repo, nameof(repo));
return _client.IsRepositoryManagedByTeam(id, owner, repo).ToObservable();
}
///
/// List all pending invitations for the given team.
///
///
/// See the API Documentation
/// for more information.
///
/// The team identifier
///
public IObservable GetAllPendingInvitations(int id)
{
Ensure.ArgumentNotNull(id, nameof(id));
return GetAllPendingInvitations(id, ApiOptions.None);
}
///
/// List all pending invitations for the given team.
///
///
/// See the API Documentation
/// for more information.
///
/// The team identifier
/// Options to change API behaviour
///
public IObservable GetAllPendingInvitations(int id, ApiOptions options)
{
Ensure.ArgumentNotNull(id, nameof(id));
Ensure.ArgumentNotNull(options, nameof(options));
return _connection.GetAndFlattenAllPages(ApiUrls.TeamPendingInvitations(id), null, AcceptHeaders.OrganizationMembershipPreview, options);
}
}
}