Add method for getting repo archive link

This commit is contained in:
Henrik Andersson
2015-03-21 21:54:01 +10:00
parent b3ba03fc34
commit 09d1932b4a
5 changed files with 94 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Octokit.Internal;
namespace Octokit
{
@@ -46,6 +47,32 @@ namespace Octokit
/// <returns></returns>
Task<string> GetReadmeHtml(string owner, string name);
/// <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>
Task<string> GetArchiveLink(string owner, string name);
/// <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>
Task<string> GetArchiveLink(string owner, string name, ArchiveFormat archiveFormat, string reference);
/// <summary>
/// Creates a commit that creates a new file in a repository.
/// </summary>
@@ -75,4 +102,14 @@ namespace Octokit
/// <param name="request">Information about the file to delete</param>
Task DeleteFile(string owner, string name, string path, DeleteFileRequest request);
}
public enum ArchiveFormat
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Tarball")]
[Parameter(Value = "tarball")]
Tarball,
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Zipball")]
[Parameter(Value = "zipball")]
Zipball
}
}

View File

@@ -74,6 +74,42 @@ namespace Octokit
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, "master");
}
/// <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");
Ensure.ArgumentNotNullOrEmptyString(reference, "reference");
return ApiConnection.GetRedirect(ApiUrls.RepositoryArchiveLink(owner, name, archiveFormat, reference));
}
/// <summary>
/// Creates a commit that creates a new file in a repository.
/// </summary>

View File

@@ -1467,5 +1467,10 @@ namespace Octokit
{
return "repos/{0}/{1}/contents/{2}".FormatUri(owner, name, path);
}
public static Uri RepositoryArchiveLink(string owner, string name, ArchiveFormat archiveFormat, string reference)
{
return "repos/{0}/{1}/{2}/{3}".FormatUri(owner, name, archiveFormat.ToParameter(), reference);
}
}
}

View File

@@ -386,6 +386,20 @@ namespace Octokit
return Connection.Delete(uri, data);
}
public async Task<string> GetRedirect(Uri uri)
{
Ensure.ArgumentNotNull(uri, "uri");
var response = await Connection.GetResponse<string>(uri);
if (response.HttpResponse.StatusCode == HttpStatusCode.Redirect)
{
return response.HttpResponse.Headers["Location"];
}
throw new ApiException("Redirect Operation expect status code or Redirect.",
response.HttpResponse.StatusCode);
}
/// <summary>
/// Executes a GET to the API object at the specified URI. This operation is appropriate for
/// API calls which queue long running calculations.

View File

@@ -232,6 +232,8 @@ namespace Octokit
/// <returns>A <see cref="Task"/> for the request's execution.</returns>
Task Delete(Uri uri, object data);
Task<string> GetRedirect(Uri uri);
/// <summary>
/// Executes a GET to the API object at the specified URI. This operation is appropriate for
/// API calls which queue long running calculations.