using System; using System.Reactive; using System.Reactive.Threading.Tasks; using Octokit.Reactive.Internal; namespace Octokit.Reactive { /// /// A client for GitHub's Repository Webhooks API. /// /// /// See the Webhooks API documentation for more information. /// public class ObservableRepositoryHooksClient : IObservableRepositoryHooksClient { readonly IRepositoryHooksClient _client; readonly IConnection _connection; public ObservableRepositoryHooksClient(IGitHubClient client) { Ensure.ArgumentNotNull(client, "client"); _client = client.Repository.Hooks; _connection = client.Connection; } /// /// Gets the list of hooks defined for a repository /// /// The repository's owner /// The repository's name /// See API documentation for more information. public IObservable GetAll(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return GetAll(owner, name, ApiOptions.None); } /// /// Gets the list of hooks defined for a repository /// /// The Id of the repository /// See API documentation for more information. public IObservable GetAll(int repositoryId) { return GetAll(repositoryId, ApiOptions.None); } /// /// Gets the list of hooks defined for a repository /// /// The repository's owner /// The repository's name /// Options for changing the API response /// See API documentation for more information. public IObservable GetAll(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(options, "options"); return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryHooks(owner, name), options); } /// /// Gets the list of hooks defined for a repository /// /// The Id of the repository /// Options for changing the API response /// See API documentation for more information. public IObservable GetAll(int repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryHooks(repositoryId), options); } /// /// Gets a single hook by Id /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. public IObservable Get(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return _client.Get(owner, name, hookId).ToObservable(); } /// /// Gets a single hook by Id /// /// The Id of the repository /// The repository's hook id /// See API documentation for more information. public IObservable Get(int repositoryId, int hookId) { return _client.Get(repositoryId, hookId).ToObservable(); } /// /// Creates a hook for a repository /// /// The repository's owner /// The repository's name /// The hook's parameters /// See API documentation for more information. public IObservable Create(string owner, string name, NewRepositoryHook hook) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(hook, "hook"); return _client.Create(owner, name, hook).ToObservable(); } /// /// Creates a hook for a repository /// /// The Id of the repository /// The hook's parameters /// See API documentation for more information. public IObservable Create(int repositoryId, NewRepositoryHook hook) { Ensure.ArgumentNotNull(hook, "hook"); return _client.Create(repositoryId, hook).ToObservable(); } /// /// Edits a hook for a repository /// /// The repository's owner /// The repository's name /// The repository's hook id /// The requested changes to an edit repository hook /// See API documentation for more information. public IObservable Edit(string owner, string name, int hookId, EditRepositoryHook hook) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(hook, "hook"); return _client.Edit(owner, name, hookId, hook).ToObservable(); } /// /// Edits a hook for a repository /// /// The Id of the repository /// The repository's hook id /// The requested changes to an edit repository hook /// See API documentation for more information. public IObservable Edit(int repositoryId, int hookId, EditRepositoryHook hook) { Ensure.ArgumentNotNull(hook, "hook"); return _client.Edit(repositoryId, hookId, hook).ToObservable(); } /// /// Tests a hook for a repository /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. /// This will trigger the hook with the latest push to the current repository if the hook is subscribed to push events. If the hook /// is not subscribed to push events, the server will respond with 204 but no test POST will be generated. public IObservable Test(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return _client.Test(owner, name, hookId).ToObservable(); } /// /// Tests a hook for a repository /// /// The Id of the repository /// The repository's hook id /// See API documentation for more information. /// This will trigger the hook with the latest push to the current repository if the hook is subscribed to push events. If the hook /// is not subscribed to push events, the server will respond with 204 but no test POST will be generated. public IObservable Test(int repositoryId, int hookId) { return _client.Test(repositoryId, hookId).ToObservable(); } /// /// This will trigger a ping event to be sent to the hook. /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. public IObservable Ping(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return _client.Ping(owner, name, hookId).ToObservable(); } /// /// This will trigger a ping event to be sent to the hook. /// /// The Id of the repository /// The repository's hook id /// See API documentation for more information. public IObservable Ping(int repositoryId, int hookId) { return _client.Ping(repositoryId, hookId).ToObservable(); } /// /// Deletes a hook for a repository /// /// The repository's owner /// The repository's name /// The repository's hook id /// See API documentation for more information. public IObservable Delete(string owner, string name, int hookId) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return _client.Delete(owner, name, hookId).ToObservable(); } /// /// Deletes a hook for a repository /// /// The Id of the repository /// The repository's hook id /// See API documentation for more information. public IObservable Delete(int repositoryId, int hookId) { return _client.Delete(repositoryId, hookId).ToObservable(); } } }