using System;
using System.Reactive.Threading.Tasks;
using Octokit.Reactive.Internal;
namespace Octokit.Reactive
{
///
/// A client for GitHub's Watching API.
///
///
/// See the Watching API documentation for more information.
///
public class ObservableWatchedClient : IObservableWatchedClient
{
private readonly IWatchedClient _client;
private readonly IConnection _connection;
public ObservableWatchedClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");
_client = client.Activity.Watching;
_connection = client.Connection;
}
///
/// Retrieves all of the watchers for the passed repository
///
/// The owner of the repository
/// The name of the repository
/// Thrown if the client is not authenticated
public IObservable GetAllWatchers(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return GetAllWatchers(owner, name, ApiOptions.None);
}
///
/// Retrieves all of the watchers for the passed repository
///
/// The ID of the repository
/// Thrown if the client is not authenticated
public IObservable GetAllWatchers(int repositoryId)
{
return GetAllWatchers(repositoryId, ApiOptions.None);
}
///
/// Retrieves all of the watchers for the passed repository
///
/// The owner of the repository
/// The name of the repository
/// Options for changing the API's response.
/// Thrown if the client is not authenticated
public IObservable GetAllWatchers(string owner, string name, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(options, "options");
return _connection.GetAndFlattenAllPages(ApiUrls.Watchers(owner, name), options);
}
///
/// Retrieves all of the watchers for the passed repository
///
/// The ID of the repository
/// Options for changing the API's response.
/// Thrown if the client is not authenticated
public IObservable GetAllWatchers(int repositoryId, ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");
return _connection.GetAndFlattenAllPages(ApiUrls.Watchers(repositoryId), options);
}
///
/// Retrieves all of the watched (ies) for the current user
///
/// Thrown if the client is not authenticated
public IObservable GetAllForCurrent()
{
return GetAllForCurrent(ApiOptions.None);
}
///
/// Retrieves all of the watched (ies) for the current user
///
/// Options for changing the API's response.
/// Thrown if the client is not authenticated
public IObservable GetAllForCurrent(ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");
return _connection.GetAndFlattenAllPages(ApiUrls.Watched(), options);
}
///
/// Retrieves all of the (ies) watched by the specified user
///
/// The login of the user
/// Thrown if the client is not authenticated
public IObservable GetAllForUser(string user)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");
return GetAllForUser(user, ApiOptions.None);
}
///
/// Retrieves all of the (ies) watched by the specified user
///
/// The login of the user
/// Options for changing the API's response.
/// Thrown if the client is not authenticated
public IObservable GetAllForUser(string user, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(user, "user");
Ensure.ArgumentNotNull(options, "options");
return _connection.GetAndFlattenAllPages(ApiUrls.WatchedByUser(user), options);
}
///
/// Check if a repository is watched by the current authenticated user
///
/// The owner of the repository
/// The name of the repository
/// Thrown if the client is not authenticated
public IObservable CheckWatched(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return _client.CheckWatched(owner, name).ToObservable();
}
///
/// Check if a repository is watched by the current authenticated user
///
/// The ID of the repository
/// Thrown if the client is not authenticated
public IObservable CheckWatched(int repositoryId)
{
return _client.CheckWatched(repositoryId).ToObservable();
}
///
/// Stars a repository for the authenticated user.
///
/// The owner of the repository to star
/// The name of the repository to star
/// A instance describing the new subscription to create
public IObservable WatchRepo(string owner, string name, NewSubscription newSubscription)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(newSubscription, "newSubscription");
return _client.WatchRepo(owner, name, newSubscription).ToObservable();
}
///
/// Stars a repository for the authenticated user.
///
/// The ID of the repository
/// A instance describing the new subscription to create
public IObservable WatchRepo(int repositoryId, NewSubscription newSubscription)
{
Ensure.ArgumentNotNull(newSubscription, "newSubscription");
return _client.WatchRepo(repositoryId, newSubscription).ToObservable();
}
///
/// Unstars a repository for the authenticated user.
///
/// The owner of the repository to unstar
/// The name of the repository to unstar
public IObservable UnwatchRepo(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return _client.UnwatchRepo(owner, name).ToObservable();
}
///
/// Unstars a repository for the authenticated user.
///
/// The ID of the repository
public IObservable UnwatchRepo(int repositoryId)
{
return _client.UnwatchRepo(repositoryId).ToObservable();
}
}
}