using System; using System.Reactive; using System.Reactive.Linq; using System.Reactive.Threading.Tasks; using Octokit.Reactive.Internal; namespace Octokit.Reactive { /// /// Client for accessing contents of files within a repository as base64 encoded content. /// /// /// See the Repository Contents API documentation for more information. /// public class ObservableRepositoryContentsClient : IObservableRepositoryContentsClient { readonly IGitHubClient _client; /// /// Creates an instance of . /// /// public ObservableRepositoryContentsClient(IGitHubClient client) { Ensure.ArgumentNotNull(client, nameof(client)); _client = client; } /// /// Returns the HTML rendered README. /// /// The owner of the repository /// The name of the repository public IObservable GetReadme(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return _client.Repository.Content.GetReadme(owner, name).ToObservable(); } /// /// Returns the HTML rendered README. /// /// The Id of the repository public IObservable GetReadme(long repositoryId) { return _client.Repository.Content.GetReadme(repositoryId).ToObservable(); } /// /// Returns just the HTML portion of the README without the surrounding HTML document. /// /// The owner of the repository /// The name of the repository public IObservable GetReadmeHtml(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return _client.Repository.Content.GetReadmeHtml(owner, name).ToObservable(); } /// /// Returns just the HTML portion of the README without the surrounding HTML document. /// /// The Id of the repository public IObservable GetReadmeHtml(long repositoryId) { return _client.Repository.Content.GetReadmeHtml(repositoryId).ToObservable(); } /// /// Get an archive of a given repository's contents /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The owner of the repository /// The name of the repository public IObservable GetArchive(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return GetArchive(owner, name, ArchiveFormat.Tarball); } /// /// Get an archive of a given repository's contents /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The Id of the repository public IObservable GetArchive(long repositoryId) { return GetArchive(repositoryId, ArchiveFormat.Tarball); } /// /// Get an archive of a given repository's contents, in a specific format /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The owner of the repository /// The name of the repository /// The format of the archive. Can be either tarball or zipball public IObservable GetArchive(string owner, string name, ArchiveFormat archiveFormat) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return GetArchive(owner, name, archiveFormat, string.Empty); } /// /// Get an archive of a given repository's contents, in a specific format /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The Id of the repository /// The format of the archive. Can be either tarball or zipball public IObservable GetArchive(long repositoryId, ArchiveFormat archiveFormat) { return GetArchive(repositoryId, archiveFormat, string.Empty); } /// /// Get an archive of a given repository's contents, using a specific format and reference /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The owner of the repository /// The name of the repository /// The format of the archive. Can be either tarball or zipball /// A valid Git reference. public IObservable GetArchive(string owner, string name, ArchiveFormat archiveFormat, string reference) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(reference, nameof(reference)); return GetArchive(owner, name, archiveFormat, reference, TimeSpan.FromMinutes(60)); } /// /// Get an archive of a given repository's contents, using a specific format and reference /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The Id of the repository /// The format of the archive. Can be either tarball or zipball /// A valid Git reference. public IObservable GetArchive(long repositoryId, ArchiveFormat archiveFormat, string reference) { Ensure.ArgumentNotNull(reference, nameof(reference)); return GetArchive(repositoryId, archiveFormat, reference, TimeSpan.FromMinutes(60)); } /// /// Get an archive of a given repository's contents, in a specific format /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The owner of the repository /// The name of the repository /// The format of the archive. Can be either tarball or zipball /// A valid Git reference. /// Time span until timeout public IObservable GetArchive(string owner, string name, ArchiveFormat archiveFormat, string reference, TimeSpan timeout) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(reference, nameof(reference)); Ensure.GreaterThanZero(timeout, nameof(timeout)); return _client.Repository.Content.GetArchive(owner, name, archiveFormat, reference, timeout).ToObservable(); } /// /// Get an archive of a given repository's contents, in a specific format /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The Id of the repository /// The format of the archive. Can be either tarball or zipball /// A valid Git reference. /// Time span until timeout public IObservable GetArchive(long repositoryId, ArchiveFormat archiveFormat, string reference, TimeSpan timeout) { Ensure.GreaterThanZero(timeout, nameof(timeout)); Ensure.ArgumentNotNull(reference, nameof(reference)); return _client.Repository.Content.GetArchive(repositoryId, archiveFormat, reference, timeout).ToObservable(); } /// /// Returns the contents of a file or directory in a repository. /// /// /// If given a path to a single file, this method returns a collection containing only that file. /// /// The owner of the repository /// The name of the repository /// The content path public IObservable GetAllContents(string owner, string name, string path) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); return _client .Connection .GetAndFlattenAllPages(ApiUrls.RepositoryContent(owner, name, path)); } /// /// Returns the contents of a file or directory in a repository. /// /// /// If given a path to a single file, this method returns a collection containing only that file. /// /// The Id of the repository /// The content path public IObservable GetAllContents(long repositoryId, string path) { Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); return _client .Connection .GetAndFlattenAllPages(ApiUrls.RepositoryContent(repositoryId, path)); } /// /// Returns the contents of the root directory in a repository. /// /// The owner of the repository /// The name of the repository public IObservable GetAllContents(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return _client .Connection .GetAndFlattenAllPages(ApiUrls.RepositoryContent(owner, name, string.Empty)); } /// /// Returns the raw content of the file at the given or null if the path is a directory. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The content path public IObservable GetRawContent(string owner, string name, string path) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); return _client .Connection .GetRaw(ApiUrls.RepositoryContent(owner, name, path), null) .ToObservable() .Select(e => e.Body); } /// /// Returns the contents of the root directory in a repository. /// /// The Id of the repository public IObservable GetAllContents(long repositoryId) { return _client .Connection .GetAndFlattenAllPages(ApiUrls.RepositoryContent(repositoryId, string.Empty)); } /// /// Returns the contents of a file or directory in a repository. /// /// /// If given a path to a single file, this method returns a collection containing only that file. /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The name of the commit/branch/tag. Default: the repository’s default branch (usually main) /// The content path public IObservable GetAllContentsByRef(string owner, string name, string reference, string path) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); return _client.Connection.GetAndFlattenAllPages(ApiUrls.RepositoryContent(owner, name, path, reference)); } /// /// Returns the raw content of the file at the given or null if the path is a directory. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// The content path /// The name of the commit/branch/tag. public IObservable GetRawContentByRef(string owner, string name, string path, string reference) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); return _client .Connection .GetRaw(ApiUrls.RepositoryContent(owner, name, path, reference), null) .ToObservable() .Select(e => e.Body); } /// /// Returns the contents of a file or directory in a repository. /// /// /// If given a path to a single file, this method returns a collection containing only that file. /// See the API documentation for more information. /// /// The Id of the repository /// The name of the commit/branch/tag. Default: the repository’s default branch (usually main) /// The content path public IObservable GetAllContentsByRef(long repositoryId, string reference, string path) { Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); return _client.Connection.GetAndFlattenAllPages(ApiUrls.RepositoryContent(repositoryId, path, reference)); } /// /// Returns the contents of the home directory in a repository. /// /// The owner of the repository /// The name of the repository /// The name of the commit/branch/tag. Default: the repository’s default branch (usually main) public IObservable GetAllContentsByRef(string owner, string name, string reference) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return _client.Connection.GetAndFlattenAllPages(ApiUrls.RepositoryContent(owner, name, string.Empty, reference)); } /// /// Returns the contents of the home directory in a repository. /// /// The Id of the repository /// The name of the commit/branch/tag. Default: the repository’s default branch (usually main ) public IObservable GetAllContentsByRef(long repositoryId, string reference) { Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return _client.Connection.GetAndFlattenAllPages(ApiUrls.RepositoryContent(repositoryId, string.Empty, reference)); } /// /// Creates a commit that creates a new file in a repository. /// /// The owner of the repository /// The name of the repository /// The path to the file /// Information about the file to create public IObservable CreateFile(string owner, string name, string path, CreateFileRequest request) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); Ensure.ArgumentNotNull(request, nameof(request)); return _client.Repository.Content.CreateFile(owner, name, path, request).ToObservable(); } /// /// Creates a commit that creates a new file in a repository. /// /// The Id of the repository /// The path to the file /// Information about the file to create public IObservable CreateFile(long repositoryId, string path, CreateFileRequest request) { Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); Ensure.ArgumentNotNull(request, nameof(request)); return _client.Repository.Content.CreateFile(repositoryId, path, request).ToObservable(); } /// /// Creates a commit that updates the contents of a file in a repository. /// /// The owner of the repository /// The name of the repository /// The path to the file /// Information about the file to update public IObservable UpdateFile(string owner, string name, string path, UpdateFileRequest request) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); Ensure.ArgumentNotNull(request, nameof(request)); return _client.Repository.Content.UpdateFile(owner, name, path, request).ToObservable(); } /// /// Creates a commit that updates the contents of a file in a repository. /// /// The Id of the repository /// The path to the file /// Information about the file to update public IObservable UpdateFile(long repositoryId, string path, UpdateFileRequest request) { Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); Ensure.ArgumentNotNull(request, nameof(request)); return _client.Repository.Content.UpdateFile(repositoryId, path, request).ToObservable(); } /// /// Creates a commit that deletes a file in a repository. /// /// The owner of the repository /// The name of the repository /// The path to the file /// Information about the file to delete public IObservable DeleteFile(string owner, string name, string path, DeleteFileRequest request) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); Ensure.ArgumentNotNull(request, nameof(request)); return _client.Repository.Content.DeleteFile(owner, name, path, request).ToObservable(); } /// /// Creates a commit that deletes a file in a repository. /// /// The Id of the repository /// The path to the file /// Information about the file to delete public IObservable DeleteFile(long repositoryId, string path, DeleteFileRequest request) { Ensure.ArgumentNotNullOrEmptyString(path, nameof(path)); Ensure.ArgumentNotNull(request, nameof(request)); return _client.Repository.Content.DeleteFile(repositoryId, path, request).ToObservable(); } } }