Files
octokit.net/Octokit/Clients/RepositoryContentsClient.cs
2015-05-08 18:31:49 +10:00

184 lines
9.1 KiB
C#

using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
/// <summary>
/// Client for accessing contents of files within a repository as base64 encoded content.
/// </summary>
public class RepositoryContentsClient : ApiClient, IRepositoryContentsClient
{
public RepositoryContentsClient(IApiConnection apiConnection) : base(apiConnection)
{
}
/// <summary>
/// Returns the contents of a file or directory in a repository.
/// </summary>
/// <remarks>
/// If given a path to a single file, this method returns a collection containing only that file.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The content path</param>
/// <returns>
/// A collection of <see cref="RepositoryContent"/> representing the content at the specified path
/// </returns>
public async Task<IReadOnlyList<RepositoryContent>> GetAllContents(string owner, string name, string path)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(path, "path");
var url = ApiUrls.RepositoryContent(owner, name, path);
return await ApiConnection.GetAll<RepositoryContent>(url);
}
/// <summary>
/// Gets the preferred README for the specified repository.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/repos/contents/#get-the-readme">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public async Task<Readme> GetReadme(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = ApiUrls.RepositoryReadme(owner, name);
var readmeInfo = await ApiConnection.Get<ReadmeResponse>(endpoint, null).ConfigureAwait(false);
return new Readme(readmeInfo, ApiConnection);
}
/// <summary>
/// Gets the perferred README's HTML for the specified repository.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/repos/contents/#get-the-readme">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public Task<string> GetReadmeHtml(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetHtml(ApiUrls.RepositoryReadme(owner, name), null);
}
/// <summary>
/// This method will return a 302 to a URL to download a tarball or zipball archive for a repository.
/// Please make sure your HTTP framework is configured to follow redirects or you will need to use the
/// Location header to make a second GET request.
/// Note: For private repositories, these links are temporary and expire quickly.
/// </summary>
/// <remarks>https://developer.github.com/v3/repos/contents/#get-archive-link</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns></returns>
public Task<string> GetArchiveLink(string owner, string name)
{
return GetArchiveLink(owner, name, ArchiveFormat.Tarball, string.Empty);
}
/// <summary>
/// This method will return a 302 to a URL to download a tarball or zipball archive for a repository.
/// Please make sure your HTTP framework is configured to follow redirects or you will need to use the
/// Location header to make a second GET request.
/// Note: For private repositories, these links are temporary and expire quickly.
/// </summary>
/// <remarks>https://developer.github.com/v3/repos/contents/#get-archive-link</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="archiveFormat">The format of the archive. Can be either tarball or zipball</param>
/// <returns></returns>
public Task<string> GetArchiveLink(string owner, string name, ArchiveFormat archiveFormat)
{
return GetArchiveLink(owner, name, archiveFormat, string.Empty);
}
/// <summary>
/// This method will return a 302 to a URL to download a tarball or zipball archive for a repository.
/// Please make sure your HTTP framework is configured to follow redirects or you will need to use the
/// Location header to make a second GET request.
/// Note: For private repositories, these links are temporary and expire quickly.
/// </summary>
/// <remarks>https://developer.github.com/v3/repos/contents/#get-archive-link</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="archiveFormat">The format of the archive. Can be either tarball or zipball</param>
/// <param name="reference">A valid Git reference.</param>
/// <returns></returns>
public Task<string> GetArchiveLink(string owner, string name, ArchiveFormat archiveFormat, string reference)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetRedirect(ApiUrls.RepositoryArchiveLink(owner, name, archiveFormat, reference));
}
/// <summary>
/// Creates a commit that creates a new file in a repository.
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The path to the file</param>
/// <param name="request">Information about the file to create</param>
/// <returns></returns>
public Task<RepositoryContentChangeSet> CreateFile(string owner, string name, string path, CreateFileRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(path, "path");
Ensure.ArgumentNotNull(request, "request");
var createUrl = ApiUrls.RepositoryContent(owner, name, path);
return ApiConnection.Put<RepositoryContentChangeSet>(createUrl, request);
}
/// <summary>
/// Creates a commit that updates the contents of a file in a repository.
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The path to the file</param>
/// <param name="request">Information about the file to update</param>
/// <returns>The updated content</returns>
public Task<RepositoryContentChangeSet> UpdateFile(string owner, string name, string path, UpdateFileRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(path, "path");
Ensure.ArgumentNotNull(request, "request");
var updateUrl = ApiUrls.RepositoryContent(owner, name, path);
return ApiConnection.Put<RepositoryContentChangeSet>(updateUrl, request);
}
/// <summary>
/// Creates a commit that deletes a file in a repository.
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="path">The path to the file</param>
/// <param name="request">Information about the file to delete</param>
public Task DeleteFile(string owner, string name, string path, DeleteFileRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(path, "path");
Ensure.ArgumentNotNull(request, "request");
var deleteUrl = ApiUrls.RepositoryContent(owner, name, path);
return ApiConnection.Delete(deleteUrl, request);
}
}
}