Example of getting content

This commit is contained in:
Kristian Hellang
2014-03-19 00:08:56 +01:00
committed by Haacked
parent bed18b9980
commit e9969d7350
13 changed files with 289 additions and 51 deletions

View File

@@ -19,6 +19,8 @@
<Word>Prerelease</Word>
<Word>Commitish</Word>
<Word>Mergeable</Word>
<Word>Symlink</Word>
<Word>Submodule</Word>
</Recognized>
</Words>
<Acronyms>

View File

@@ -0,0 +1,21 @@
using System.Threading.Tasks;
namespace Octokit.Tests.Integration.Clients
{
public class RepositoryContentsClientTests
{
public class TheGetRootMethod
{
[IntegrationTest]
public async Task GetsRootContents()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};
var contents = await github.Repository.Contents.GetForPath("octokit", "octokit.net", "Octokit.Reactive/ObservableGitHubClient.cs");
}
}
}
}

View File

@@ -9,18 +9,18 @@
<ProjectGuid>{01687D54-1D87-4562-A721-C57F1C94052C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Octokit.Tests.Integration</RootNamespace>
<AssemblyName>Octokit.Tests.Integration</AssemblyName>
<RootNamespace>Octokit.Tests</RootNamespace>
<AssemblyName>Octokit.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>89e72d09</NuGetPackageImportStamp>
<NuGetPackageImportStamp>f1b9719f</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<OutputPath>bin\Debug\Net45\</OutputPath>
<DefineConstants>TRACE;DEBUG;NET_45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>4014, 1998</NoWarn>
@@ -29,14 +29,19 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\Net45\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;NET_45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>4014, 1998</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="NSubstitute, Version=1.8.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NSubstitute.1.8.0.0\lib\net45\NSubstitute.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
@@ -50,9 +55,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll</HintPath>
</Reference>
<Reference Include="System.XML" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Xml" />
<Reference Include="xunit.abstractions">
<HintPath>..\packages\xunit.abstractions.2.0.0-beta5-build2785\lib\net35\xunit.abstractions.dll</HintPath>
</Reference>
@@ -62,81 +66,146 @@
<Reference Include="xunit.core">
<HintPath>..\packages\xunit.core.2.0.0-beta5-build2785\lib\portable-net45+aspnetcore50+win+wpa81+wp80+monotouch+monoandroid\xunit.core.dll</HintPath>
</Reference>
<Reference Include="xunit.execution, Version=2.0.0.2785, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\xunit.core.2.0.0-beta5-build2785\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid\xunit.execution.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Clients\AssigneesClientTests.cs" />
<Compile Include="Clients\BlobClientTests.cs" />
<Compile Include="Clients\BranchesClientTests.cs" />
<Compile Include="Clients\CommitsClientTests.cs" />
<Compile Include="Clients\CommitStatusClientTests.cs" />
<Compile Include="Authentication\CredentialsTests.cs" />
<Compile Include="Clients\OauthClientTests.cs" />
<Compile Include="Clients\RepositoryCommentsClientTests.cs" />
<Compile Include="Clients\DeploymentsClientTests.cs" />
<Compile Include="Clients\DeploymentStatusClientTests.cs" />
<Compile Include="Clients\EventsClientTests.cs" />
<Compile Include="Clients\IssuesLabelsClientTests.cs" />
<Compile Include="Clients\GistsClientTests.cs" />
<Compile Include="Clients\IssuesEventsClientTests.cs" />
<Compile Include="Clients\MilestonesClientTests.cs" />
<Compile Include="Clients\PullRequestsClientTests.cs" />
<Compile Include="Clients\ReferencesClientTests.cs" />
<Compile Include="Clients\RepositoryCommitsClientTests.cs" />
<Compile Include="Clients\FeedsClientTests.cs" />
<Compile Include="Clients\RepositoryDeployKeysClientTests.cs" />
<Compile Include="Clients\RepositoryContentsClientTests.cs" />
<Compile Include="Clients\SearchClientTests.cs" />
<Compile Include="Clients\GistCommentsClientTests.cs" />
<Compile Include="Clients\GistsClientTests.cs" />
<Compile Include="Clients\BlobClientTests.cs" />
<Compile Include="Clients\IssuesLabelsClientTests.cs" />
<Compile Include="Clients\ReferencesClientTests.cs" />
<Compile Include="Clients\RepoCollaboratorsClientTests.cs" />
<Compile Include="Clients\EventsClientTests.cs" />
<Compile Include="Clients\AssigneesClientTests.cs" />
<Compile Include="Clients\CommitsClientTests.cs" />
<Compile Include="Clients\CommitStatusClientTests.cs" />
<Compile Include="Clients\StatisticsClientTests.cs" />
<Compile Include="Clients\TreeClientTests.cs" />
<Compile Include="Clients\UserEmailsClientTests.cs" />
<Compile Include="Clients\FollowersClientTests.cs" />
<Compile Include="Clients\UserKeysClientTests.cs" />
<Compile Include="Helpers\RepositorySetupHelper.cs" />
<Compile Include="HttpClientAdapterTests.cs" />
<Compile Include="Clients\TeamsClientTests.cs" />
<Compile Include="Helpers\IntegrationTestAttribute.cs" />
<Compile Include="Clients\PullRequestReviewCommentsClientTests.cs" />
<Compile Include="Clients\TeamsClientTests.cs" />
<Compile Include="Clients\GitDatabaseClientTests.cs" />
<Compile Include="Clients\OrganizationMembersClientTests.cs" />
<Compile Include="Clients\PullRequestsClientTests.cs" />
<Compile Include="Clients\TagsClientTests.cs" />
<Compile Include="Clients\IssuesEventsClientTests.cs" />
<Compile Include="Clients\IssueCommentsClientTests.cs" />
<Compile Include="Clients\MilestonesClientTests.cs" />
<Compile Include="Clients\IssuesClientTests.cs" />
<Compile Include="Clients\StarredClientTests.cs" />
<Compile Include="Clients\NotificationsClientTests.cs" />
<Compile Include="Clients\ReleasesClientTests.cs" />
<Compile Include="Clients\SshKeysClientTests.cs" />
<Compile Include="Clients\TreesClientTests.cs" />
<Compile Include="Clients\UserEmailsClientTests.cs" />
<Compile Include="Clients\WatchedClientTests.cs" />
<Compile Include="Clients\FollowersClientTests.cs" />
<Compile Include="Exceptions\ApiExceptionTests.cs" />
<Compile Include="Exceptions\ApiValidationExceptionTests.cs" />
<Compile Include="Exceptions\RepositoryExistsExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\ForbiddenExceptionTests.cs" />
<Compile Include="Exceptions\LoginAttemptsExceededExceptionTests.cs" />
<Compile Include="Exceptions\RateLimitExceededExceptionTests.cs" />
<Compile Include="Exceptions\TwoFactorRequiredExceptionTests.cs" />
<Compile Include="Helpers\NSubstituteExtensions.cs" />
<Compile Include="Helpers\ReflectionExtensions.cs" />
<Compile Include="Helpers\UriExtensionsTests.cs" />
<Compile Include="Http\ApiConnectionTests.cs" />
<Compile Include="Clients\AuthorizationsClientTests.cs" />
<Compile Include="Clients\MiscellaneousClientTests.cs" />
<Compile Include="Helpers\OrganizationTestAttribute.cs" />
<Compile Include="Clients\OrganizationsClientTests.cs" />
<Compile Include="Helpers\Arg.cs" />
<Compile Include="Helpers\AssertEx.cs" />
<Compile Include="Http\HttpClientAdapterTests.cs" />
<Compile Include="Http\ApiInfoParserTests.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Authentication\BasicAuthenticatorTests.cs" />
<Compile Include="Http\JsonHttpPipelineTests.cs" />
<Compile Include="Fixtures\EmbeddedResource.cs" />
<Compile Include="Fixtures\Fixtures.cs" />
<Compile Include="GitHubClientTests.cs" />
<Compile Include="Authentication\TokenAuthenticatorTests.cs" />
<Compile Include="Http\ConnectionTests.cs" />
<Compile Include="Http\RateLimitTests.cs" />
<Compile Include="Http\ResponseTests.cs" />
<Compile Include="Http\RequestTests.cs" />
<Compile Include="Models\CommitTests.cs" />
<Compile Include="Models\DeploymentStatusTests.cs" />
<Compile Include="Models\DeploymentTests.cs" />
<Compile Include="Models\NewReferenceTests.cs" />
<Compile Include="Models\MilestoneRequestTests.cs" />
<Compile Include="Models\IssueRequestTests.cs" />
<Compile Include="Models\ModelExtensionsTests.cs" />
<Compile Include="Models\PullRequestRequestTests.cs" />
<Compile Include="Models\PunchCardTests.cs" />
<Compile Include="Models\ReadOnlyPagedCollectionTests.cs" />
<Compile Include="Models\RepositoryUpdateTests.cs" />
<Compile Include="Models\RequestParametersTests.cs" />
<Compile Include="Models\SearchCodeRequestTests.cs" />
<Compile Include="Models\SearchUsersRequestTests.cs" />
<Compile Include="Models\SearchIssuesRequestTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\StringExtensionsTests.cs" />
<Compile Include="Clients\RepositoriesClientTests.cs" />
<Compile Include="Reactive\AuthorizationExtensionsTests.cs" />
<Compile Include="Reactive\ObservableBlobClientTests.cs" />
<Compile Include="Reactive\ObservableCommitsClientTests.cs" />
<Compile Include="Reactive\ObservableDeploymentsClientTests.cs" />
<Compile Include="Reactive\ObservableDeploymentStatusClientTests.cs" />
<Compile Include="Reactive\ObservableEventsClientTests.cs" />
<Compile Include="Reactive\ObservableFeedsClientTests.cs" />
<Compile Include="Reactive\ObservableIssueCommentsClientTests.cs" />
<Compile Include="Reactive\ObservableIssuesClientTests.cs" />
<Compile Include="Reactive\ObservableMilestonesClientTests.cs" />
<Compile Include="Reactive\ObservableOrganizationMembersClientTests.cs" />
<Compile Include="Reactive\ObservablePullRequestsClientTests.cs" />
<Compile Include="Reactive\ObservableReleasesClientTests.cs" />
<Compile Include="Reactive\ObservablePullRequestReviewCommentsClientTests.cs" />
<Compile Include="Reactive\ObservableRepositoriesClientTests.cs" />
<Compile Include="Clients\ReleasesClientTests.cs" />
<Compile Include="Clients\RepositoriesClientTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Clients\UsersClientTests.cs" />
<Compile Include="Reactive\ObservableRespositoryDeployKeysClientTests.cs" />
<Compile Include="Reactive\ObservableRepositoryDeployKeysClientTests.cs" />
<Compile Include="Reactive\ObservableStarredClientTests.cs" />
<Compile Include="Reactive\ObservableStatisticsClientTests.cs" />
<Compile Include="Reactive\ObservableTreesClientTests.cs" />
<Compile Include="Reactive\ObservableFollowersTest.cs" />
<Compile Include="Reactive\ObservableUserEmailsClientTests.cs" />
<Compile Include="Reactive\ObservableTeamsClientTests.cs" />
<Compile Include="SelfTests.cs" />
<Compile Include="SimpleJsonSerializerTests.cs" />
<Compile Include="Clients\UsersClientTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Octokit.Reactive\Octokit.Reactive.csproj">
<Project>{674b69b8-0780-4d54-ae2b-c15821fa51cb}</Project>
<Name>Octokit.Reactive</Name>
</ProjectReference>
<ProjectReference Include="..\Octokit.Tests\Octokit.Tests.csproj">
<Project>{149448d4-c2f2-4df9-86bd-03e3272f093b}</Project>
<Name>Octokit.Tests</Name>
</ProjectReference>
<ProjectReference Include="..\Octokit\Octokit.csproj">
<Project>{08dd4305-7787-4823-a53f-4d0f725a07f3}</Project>
<Name>Octokit</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Fixtures\user_full.json" />
<EmbeddedResource Include="Fixtures\authorizations.json" />
<EmbeddedResource Include="Fixtures\authorization.json" />
<EmbeddedResource Include="Fixtures\repository.json" />
<EmbeddedResource Include="Fixtures\repositories.json" />
<EmbeddedResource Include="Fixtures\release_asset.json" />
<EmbeddedResource Include="Fixtures\release.json" />
<EmbeddedResource Include="Fixtures\releases.json" />
<None Include="app.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<EmbeddedResource Include="Fixtures\user.json" />
<None Include="packages.Octokit.Tests.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="fixtures\hello-world.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>

View File

@@ -1,9 +1,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
public interface IRepositoryContentsClient
{
Task<IReadOnlyList<DirectoryContent>> GetRoot(string owner, string name);
Task<IReadOnlyList<DirectoryContent>> GetForPath(string owner, string name, string path);
/// <summary>
/// Gets the preferred README for the specified repository.
/// </summary>

View File

@@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Octokit
@@ -8,6 +11,47 @@ namespace Octokit
{
}
public Task<IReadOnlyList<DirectoryContent>> GetRoot(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll<DirectoryContent>(ApiUrls.RepositoryContent(owner, name));
}
public async Task<IReadOnlyList<DirectoryContent>> GetForPath(string owner, string name, string path)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(path, "path");
// First, find content in parent directory.
var content = await FindContent(owner, name, path);
if (content == null)
{
// We've asked for a file/folder that don't exist.
return new List<DirectoryContent>();
}
var url = ApiUrls.RepositoryContent(owner, name, path);
// Check which type the content is before fetching/deserializing it.
switch (content.Type)
{
case ContentType.Dir:
return await ApiConnection.GetAll<DirectoryContent>(url);
case ContentType.File:
return new List<DirectoryContent> { await ApiConnection.Get<FileContent>(url) };
case ContentType.Symlink:
return new List<DirectoryContent> { await ApiConnection.Get<SymlinkContent>(url) };
case ContentType.Submodule:
return new List<DirectoryContent> { await ApiConnection.Get<SubmoduleContent>(url) };
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Gets the preferred README for the specified repository.
/// </summary>
@@ -46,5 +90,22 @@ namespace Octokit
return ApiConnection.GetHtml(ApiUrls.RepositoryReadme(owner, name), null);
}
private async Task<DirectoryContent> FindContent(string owner, string name, string path)
{
var pathParts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var fileOrDirectoryName = pathParts.Last();
var parentPath = string.Join("/", pathParts.TakeWhile(x => x != fileOrDirectoryName));
var parentContentsUri = !string.IsNullOrEmpty(parentPath)
? ApiUrls.RepositoryContent(owner, name, parentPath)
: ApiUrls.RepositoryContent(owner, name);
var parentContents = await ApiConnection.GetAll<DirectoryContent>(parentContentsUri);
return parentContents.FirstOrDefault(x => x.Name == fileOrDirectoryName);
}
}
}

View File

@@ -1356,5 +1356,28 @@ namespace Octokit
{
return "repos/{0}/{1}/readme".FormatUri(owner, name);
}
/// <summary>
/// Creates the relative <see cref="Uri"/> for getting the contents of the specified repository's root
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>The <see cref="Uri"/> for getting the contents of the specified repository's root</returns>
public static Uri RepositoryContent(string owner, string name)
{
return "repos/{0}/{1}/contents".FormatUri(owner, name);
}
/// <summary>
/// Creates the relative <see cref="Uri"/> for getting the contents of the specified repository and path
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The path of the contents to get</param>
/// <returns>The <see cref="Uri"/> for getting the contents of the specified repository and path</returns>
public static Uri RepositoryContent(string owner, string name, string path)
{
return "repos/{0}/{1}/contents/{2}".FormatUri(owner, name, path);
}
}
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Diagnostics.CodeAnalysis;
namespace Octokit
{
public class DirectoryContent
{
public string Name { get; set; }
public string Path { get; set; }
public string Sha { get; set; }
public int? Size { get; set; }
[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "Matches the property name used by the API")]
public ContentType Type { get; set; }
public Uri Url { get; set; }
public Uri GitUrl { get; set; }
public Uri HtmlUrl { get; set; }
}
public class FileContent : DirectoryContent
{
public string Encoding { get; set; }
public string Content { get; set; }
}
public class SymlinkContent : DirectoryContent
{
public string Target { get; set; }
}
public class SubmoduleContent : DirectoryContent
{
public Uri SubmoduleGitUrl { get; set; }
}
public enum ContentType
{
File,
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Dir", Justification = "Matches the value returned by the API")]
Dir,
Symlink,
Submodule
}
}

View File

@@ -349,6 +349,7 @@
<Compile Include="Models\Request\NewThreadSubscription.cs" />
<Compile Include="Clients\IRepositoryContentsClient.cs" />
<Compile Include="Clients\RepositoryContentsClient.cs" />
<Compile Include="Models\Response\DirectoryContent.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -359,6 +359,7 @@
<Compile Include="Models\Request\NewThreadSubscription.cs" />
<Compile Include="Clients\IRepositoryContentsClient.cs" />
<Compile Include="Clients\RepositoryContentsClient.cs" />
<Compile Include="Models\Response\DirectoryContent.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>

View File

@@ -354,6 +354,7 @@
<Compile Include="Models\Request\NewThreadSubscription.cs" />
<Compile Include="Clients\IRepositoryContentsClient.cs" />
<Compile Include="Clients\RepositoryContentsClient.cs" />
<Compile Include="Models\Response\DirectoryContent.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View File

@@ -347,6 +347,7 @@
<Compile Include="Models\Response\ThreadSubscription.cs" />
<Compile Include="Models\Request\MarkAsReadRequest.cs" />
<Compile Include="Models\Request\NewThreadSubscription.cs" />
<Compile Include="Models\Response\DirectoryContent.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">

View File

@@ -351,6 +351,7 @@
<Compile Include="Models\Request\NewThreadSubscription.cs" />
<Compile Include="Clients\IRepositoryContentsClient.cs" />
<Compile Include="Clients\RepositoryContentsClient.cs" />
<Compile Include="Models\Response\DirectoryContent.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">

View File

@@ -95,6 +95,7 @@
<Compile Include="Models\Response\CompareResult.cs" />
<Compile Include="Models\Request\NewCommitComment.cs" />
<Compile Include="Models\Response\CommitComment.cs" />
<Compile Include="Models\Response\DirectoryContent.cs" />
<Compile Include="Models\Response\DeploymentStatus.cs" />
<Compile Include="Clients\DeploymentStatusClient.cs" />
<Compile Include="Clients\IDeploymentStatusClient.cs" />