using System.Collections.Generic; using System.Threading.Tasks; namespace Octokit { /// /// Client for accessing contents of files within a repository as base64 encoded content. /// public class RepositoryContentsClient : ApiClient, IRepositoryContentsClient { public RepositoryContentsClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// 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. /// /// The owner of the repository /// The name of the repository /// The content path /// /// A collection of representing the content at the specified path /// public async Task> 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(url); } /// /// Gets the preferred README for the specified repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Thrown when a general API error occurs. /// public async Task GetReadme(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); var endpoint = ApiUrls.RepositoryReadme(owner, name); var readmeInfo = await ApiConnection.Get(endpoint, null).ConfigureAwait(false); return new Readme(readmeInfo, ApiConnection); } /// /// Gets the perferred README's HTML for the specified repository. /// /// /// See the API documentation for more information. /// /// The owner of the repository /// The name of the repository /// Thrown when a general API error occurs. /// public Task GetReadmeHtml(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); return ApiConnection.GetHtml(ApiUrls.RepositoryReadme(owner, name), null); } /// /// 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. /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The owner of the repository /// The name of the repository /// public Task GetArchiveLink(string owner, string name) { return GetArchiveLink(owner, name, ArchiveFormat.Tarball, string.Empty); } /// /// 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. /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The owner of the repository /// The name of the repository /// The format of the archive. Can be either tarball or zipball /// public Task GetArchiveLink(string owner, string name, ArchiveFormat archiveFormat) { return GetArchiveLink(owner, name, archiveFormat, string.Empty); } /// /// 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. /// /// https://developer.github.com/v3/repos/contents/#get-archive-link /// The owner of the repository /// The name of the repository /// The format of the archive. Can be either tarball or zipball /// A valid Git reference. /// public Task 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)); } /// /// Creates a commit that creates a new file in a repository. /// /// The owner of the repository /// The name of the repository /// The path to the file /// Information about the file to create /// public Task 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(createUrl, request); } /// /// Creates a commit that updates the contents of a file in a repository. /// /// The owner of the repository /// The name of the repository /// The path to the file /// Information about the file to update /// The updated content public Task 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(updateUrl, request); } /// /// Creates a commit that deletes a file in a repository. /// /// The owner of the repository /// The name of the repository /// The path to the file /// Information about the file to delete 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); } } }