using System;
#if NET_45
using System.Collections.Generic;
#endif
using System.Threading.Tasks;
namespace Octokit
{
///
/// A client for GitHub's Repositories API.
///
///
/// See the Repositories API documentation for more details.
///
public class RepositoriesClient : ApiClient, IRepositoriesClient
{
///
/// Initializes a new GitHub Repos API client.
///
/// An API connection.
public RepositoriesClient(IApiConnection apiConnection) : base(apiConnection)
{
CommitStatus = new CommitStatusClient(apiConnection);
_hooks = new Lazy(() => new RepositoryHooksClient(apiConnection));
}
///
/// Creates a new repository for the current user.
///
///
/// See the API documentation for more information.
///
/// A instance describing the new repository to create.
/// Thrown when a general API error occurs.
/// A instance for the created repository.
public Task Create(NewRepository newRepository)
{
Ensure.ArgumentNotNull(newRepository, "newRepository");
if (string.IsNullOrEmpty(newRepository.Name))
throw new ArgumentException("The new repository's name must not be null.");
return ApiConnection.Post(ApiUrls.Repositories(), newRepository);
}
///
/// Creates a new repository in the specified organization.
///
///
/// See the API documentation for more information.
///
/// Login of the organization in which to create the repostiory.
/// A instance describing the new repository to create.
/// Thrown when a general API error occurs.
/// A instance for the created repository
public Task Create(string organizationLogin, NewRepository newRepository)
{
Ensure.ArgumentNotNull(organizationLogin, "organizationLogin");
Ensure.ArgumentNotNull(newRepository, "newRepository");
if (string.IsNullOrEmpty(newRepository.Name))
throw new ArgumentException("The new repository's name must not be null.");
return ApiConnection.Post(ApiUrls.OrganizationRepositories(organizationLogin), newRepository);
}
///
/// Deletes the specified repository.
///
///
/// See the API documentation for more information.
/// Deleting a repository requires admin access. If OAuth is used, the `delete_repo` scope is required.
///
/// The owner of the repository.
/// The name of the repository.
/// Thrown when a general API error occurs.
public Task Delete(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = "repos/{0}/{1}".FormatUri(owner, name);
return ApiConnection.Delete(endpoint);
}
///
/// Gets 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.
/// A
public Task Get(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var endpoint = "repos/{0}/{1}".FormatUri(owner, name);
return ApiConnection.Get(endpoint);
}
///
/// Gets all repositories owned by the current user.
///
///
/// See the API documentation for more information.
/// The default page size on GitHub.com is 30.
///
/// Thrown if the client is not authenticated.
/// Thrown when a general API error occurs.
/// A of .
public Task> GetAllForCurrent()
{
return ApiConnection.GetAll(ApiUrls.Repositories());
}
///
/// Gets all repositories owned by the specified user.
///
///
/// See the API documentation for more information.
/// The default page size on GitHub.com is 30.
///
/// Thrown when a general API error occurs.
/// A of .
public Task> GetAllForUser(string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, "login");
return ApiConnection.GetAll(ApiUrls.Repositories(login));
}
///
/// Gets all repositories owned by the specified organization.
///
///
/// See the API documentation for more information.
/// The default page size on GitHub.com is 30.
///
/// Thrown when a general API error occurs.
/// A of .
public Task> GetAllForOrg(string organization)
{
Ensure.ArgumentNotNullOrEmptyString(organization, "organization");
return ApiConnection.GetAll(ApiUrls.OrganizationRepositories(organization));
}
///
/// 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 = "repos/{0}/{1}/readme".FormatUri(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");
var endpoint = "repos/{0}/{1}/readme".FormatUri(owner, name);
return ApiConnection.GetHtml(endpoint, null);
}
///
/// A client for GitHub's Commit Status API.
///
///
/// See the Commit Status API documentation for more
/// details. Also check out the blog post
/// that announced this feature.
///
public ICommitStatusClient CommitStatus { get; private set; }
Lazy _hooks;
///
/// Gets a client for GitHub's Repository Hooks
///
public IRepositoryHooksClient Hooks
{
get { return _hooks.Value; }
}
}
}