diff --git a/Octokit.Reactive/Clients/IObservableRepositoryContentsClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryContentsClient.cs index 4b56ed1b4f..e6dfbf6508 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 @@ -77,6 +78,22 @@ public interface IObservableRepositoryContentsClient /// 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 ca2889e478..7280ea45c2 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 { @@ -123,6 +124,30 @@ public IObservable GetAllContents(string owner, string name, .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 6626f7ef13..1879c667b8 100644 --- a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs @@ -4,6 +4,7 @@ using NSubstitute; using Octokit.Tests.Helpers; using Xunit; +using System.Collections.Generic; namespace Octokit.Tests.Clients { @@ -108,5 +109,23 @@ public async Task EnsuresArgumentsNotNull() AssertEx.Throws(async () => await 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 45a217b0da..ebfd65d379 100644 --- a/Octokit/Clients/IRepositoryContentsClient.cs +++ b/Octokit/Clients/IRepositoryContentsClient.cs @@ -14,6 +14,7 @@ public interface IRepositoryContentsClient /// /// /// 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 @@ -23,6 +24,22 @@ public interface IRepositoryContentsClient /// 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 7b867f0198..640f3a6452 100644 --- a/Octokit/Clients/RepositoryContentsClient.cs +++ b/Octokit/Clients/RepositoryContentsClient.cs @@ -17,6 +17,7 @@ public RepositoryContentsClient(IApiConnection apiConnection) : base(apiConnecti /// /// /// 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 @@ -35,6 +36,32 @@ public async Task> GetAllContents(string owner, 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 357b66c31b..ad59aeaeea 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -1472,5 +1472,18 @@ public static Uri RepositoryArchiveLink(string owner, string name, ArchiveFormat { 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); + } } }