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, nameof(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, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(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(long 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, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNull(options, nameof(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(long repositoryId, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(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, nameof(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, nameof(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, nameof(user));
Ensure.ArgumentNotNull(options, nameof(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, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(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(long 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, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNull(newSubscription, nameof(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(long repositoryId, NewSubscription newSubscription)
{
Ensure.ArgumentNotNull(newSubscription, nameof(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, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
return _client.UnwatchRepo(owner, name).ToObservable();
}
///
/// Unstars a repository for the authenticated user.
///
/// The Id of the repository
public IObservable UnwatchRepo(long repositoryId)
{
return _client.UnwatchRepo(repositoryId).ToObservable();
}
}
}