mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-20 06:05:12 +00:00
Example of getting content
This commit is contained in:
committed by
Haacked
parent
bed18b9980
commit
e9969d7350
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user