diff --git a/Octokit.Reactive/Clients/IObservableRepositoryContentsClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryContentsClient.cs index a5221a6b..60a2cd71 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoryContentsClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoryContentsClient.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reactive; namespace Octokit.Reactive @@ -110,6 +111,22 @@ namespace Octokit.Reactive /// IObservable GetAllContents(string owner, string name, string 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. + /// 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. Default: the repository’s default branch (usually master) + /// + /// A collection of representing the content at the specified path + /// + IObservable GetAllContents(string owner, string name, string path, string reference); + /// /// Creates a commit that creates a new file in a repository. /// diff --git a/Octokit.Reactive/Clients/ObservableRepositoryContentsClient.cs b/Octokit.Reactive/Clients/ObservableRepositoryContentsClient.cs index a4a5e4b2..177c5ada 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoryContentsClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoryContentsClient.cs @@ -1,6 +1,7 @@ using System; using System.Reactive.Threading.Tasks; using Octokit.Reactive.Internal; +using System.Collections.Generic; namespace Octokit.Reactive { @@ -165,6 +166,30 @@ namespace Octokit.Reactive .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. + /// 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. Default: the repository’s default branch (usually master) + /// + /// A collection of representing the content at the specified path + /// + public IObservable GetAllContents(string owner, string name, string path, string reference) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(path, "path"); + Ensure.ArgumentNotNullOrEmptyString(reference, "reference"); + + return _client.Connection.GetAndFlattenAllPages(ApiUrls.RepositoryContent(owner, name, path, reference)); + } + /// /// Creates a commit that creates a new file in a repository. /// diff --git a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs index efc963ad..0688d9db 100644 --- a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using NSubstitute; using Octokit.Tests.Helpers; using Xunit; +using System.Collections.Generic; namespace Octokit.Tests.Clients { @@ -108,5 +109,23 @@ namespace Octokit.Tests.Clients await Assert.ThrowsAsync(() => contentsClient.GetArchiveLink("owner", "")); } } + + public class TheGetContentsMethod + { + [Fact] + public async Task ReturnsContents() + { + List result = new List() { new RepositoryContent() { } }; + + var connection = Substitute.For(); + connection.GetAll(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList)); + var contentsClient = new RepositoryContentsClient(connection); + + var contents = await contentsClient.GetAllContents("fake", "repo", "readme.md", "master"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/contents/readme.md?ref=master")); + Assert.Equal(1, contents.Count); + } + } } } \ No newline at end of file diff --git a/Octokit/Clients/IRepositoryContentsClient.cs b/Octokit/Clients/IRepositoryContentsClient.cs index e02cd689..d2923478 100644 --- a/Octokit/Clients/IRepositoryContentsClient.cs +++ b/Octokit/Clients/IRepositoryContentsClient.cs @@ -15,6 +15,7 @@ namespace Octokit /// /// /// 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 @@ -24,6 +25,22 @@ namespace Octokit /// Task> GetAllContents(string owner, string name, string 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. + /// 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. Default: the repository’s default branch (usually master) + /// + /// A collection of representing the content at the specified path + /// + Task> GetAllContents(string owner, string name, string path, string reference); + /// /// Gets the preferred README for the specified repository. /// diff --git a/Octokit/Clients/RepositoryContentsClient.cs b/Octokit/Clients/RepositoryContentsClient.cs index 4ad75d5e..f23b10b4 100644 --- a/Octokit/Clients/RepositoryContentsClient.cs +++ b/Octokit/Clients/RepositoryContentsClient.cs @@ -18,6 +18,7 @@ namespace Octokit /// /// /// 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 @@ -36,6 +37,32 @@ namespace Octokit return await ApiConnection.GetAll(url); } + /// + /// 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 content path + /// The name of the commit/branch/tag. Default: the repository�s default branch (usually master) + /// + /// A collection of representing the content at the specified path + /// + public async Task> GetAllContents(string owner, string name, string path, string reference) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(path, "path"); + Ensure.ArgumentNotNullOrEmptyString(reference, "reference"); + + var url = ApiUrls.RepositoryContent(owner, name, path, reference); + + return await ApiConnection.GetAll(url); + } + /// /// Gets the preferred README for the specified repository. /// diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 3bc5ec9e..afbcaacd 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -1541,5 +1541,18 @@ namespace Octokit { return "repos/{0}/{1}/{2}/{3}".FormatUri(owner, name, archiveFormat.ToParameter(), reference); } + + /// + /// Creates the relative for getting the contents of the specified repository and path + /// + /// The owner of the repository + /// The name of the repository + /// The path of the contents to get + /// The name of the commit/branch/tag. Default: the repository’s default branch (usually master) + /// The for getting the contents of the specified repository and path + public static Uri RepositoryContent(string owner, string name, string path, string reference) + { + return "repos/{0}/{1}/contents/{2}?ref={3}".FormatUri(owner, name, path, reference); + } } }