Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Octokit/Clients/ReleasesClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading;
using System.IO;
using System;

namespace Octokit
{
Expand Down Expand Up @@ -501,6 +503,37 @@ public Task<ReleaseAsset> GetAsset(long repositoryId, int assetId)
return ApiConnection.Get<ReleaseAsset>(endpoint);
}

/// <summary>
/// Gets a raw read-only stream containing the file data for the specified asset
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/repos/releases/#get-a-single-release-asset">API documentation</a> for more information.
/// </remarks>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="assetId">The id of the <see cref="ReleaseAsset"/></param>
[ManualRoute("GET", "/repositories/{id}/releases/assets/{asset_id}")]
public Task<Stream> DownloadAsset(long repositoryId, int assetId)
{
var endpoint = ApiUrls.Asset(repositoryId, assetId);
return ApiConnection.GetRawStream(endpoint, null, "application/octet-stream");
}

/// <summary>
/// Gets a raw read-only stream containing the file data for the specified asset
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/repos/releases/#get-a-single-release-asset">API documentation</a> for more information.
/// </remarks>
/// <param name="owner">The repository's owner</param>
/// <param name="name">The repository's name</param>
/// <param name="assetId">The id of the <see cref="ReleaseAsset"/></param>
[ManualRoute("GET", "/repositories/{id}/releases/assets/{asset_id}")]
public Task<Stream> DownloadAsset(string owner, string name, int assetId)
{
var endpoint = ApiUrls.Asset(owner, name, assetId);
return ApiConnection.GetRawStream(endpoint, null, "application/octet-stream");
}

/// <summary>
/// Edits the <see cref="ReleaseAsset"/> for the specified release of the specified repository.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions Octokit/Http/ApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ public async Task<string> GetHtml(Uri uri, IDictionary<string, string> parameter
return response.Body;
}

/// <summary>
/// Gets the raw content of the API resource at the specified URI.
/// </summary>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <param name="parameters">Parameters to add to the API request</param>
/// <returns>The API resource's raw content or <c>null</c> if the <paramref name="uri"/> points to a directory.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<byte[]> GetRaw(Uri uri, IDictionary<string, string> parameters, string accepts)
{
Ensure.ArgumentNotNull(uri, nameof(uri));

var response = await Connection.GetRaw(uri, parameters, accepts).ConfigureAwait(false);
return response.Body;
}

/// <inheritdoc/>
public async Task<Stream> GetRawStream(Uri uri, IDictionary<string, string> parameters, string accepts)
{
Ensure.ArgumentNotNull(uri, nameof(uri));

var response = await Connection.GetRawStream(uri, parameters, accepts).ConfigureAwait(false);
return response.Body;
}

/// <summary>
/// Gets the raw content of the API resource at the specified URI.
/// </summary>
Expand Down
65 changes: 62 additions & 3 deletions Octokit/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,62 @@ public Task<IApiResponse<string>> GetHtml(Uri uri, IDictionary<string, string> p
});
}

/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
/// <param name="uri">URI endpoint to send request to</param>
/// <param name="parameters">Querystring parameters for the request</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
/// <remarks>The <see cref="IResponse.Body"/> property will be <c>null</c> if the <paramref name="uri"/> points to a directory instead of a file</remarks>
public Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> parameters, string accepts)
{
Ensure.ArgumentNotNull(uri, nameof(uri));

var req = new Request
{
Method = HttpMethod.Get,
BaseAddress = BaseAddress,
Endpoint = uri.ApplyParameters(parameters),
};
req.Headers.Add("Accept", accepts);

return GetRaw(req);
}

/// <inheritdoc/>
public Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> parameters, TimeSpan timeout, string accepts)
{
Ensure.ArgumentNotNull(uri, nameof(uri));

var req = new Request
{
Method = HttpMethod.Get,
BaseAddress = BaseAddress,
Endpoint = uri.ApplyParameters(parameters),
Timeout = timeout
};
req.Headers.Add("Accept", accepts);

return GetRaw(req);
}

/// <inheritdoc/>
public Task<IApiResponse<Stream>> GetRawStream(Uri uri, IDictionary<string, string> parameters, string accepts)
{
Ensure.ArgumentNotNull(uri, nameof(uri));

var req = new Request
{
Method = HttpMethod.Get,
BaseAddress = BaseAddress,
Endpoint = uri.ApplyParameters(parameters)
};
req.Headers.Add("Accept", accepts);

return GetRawStream(req);
}

/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
Expand Down Expand Up @@ -737,14 +793,16 @@ public IResponseCache ResponseCache

async Task<IApiResponse<string>> GetHtml(IRequest request)
{
request.Headers.Add("Accept", AcceptHeaders.StableVersionHtml);
if (request.Headers.ContainsKey("Accept") is false)
request.Headers.Add("Accept", AcceptHeaders.StableVersionHtml);
var response = await RunRequest(request, CancellationToken.None).ConfigureAwait(false);
return new ApiResponse<string>(response, response.Body as string);
}

async Task<IApiResponse<byte[]>> GetRaw(IRequest request)
{
request.Headers.Add("Accept", AcceptHeaders.RawContentMediaType);
if (request.Headers.ContainsKey("Accept") is false)
request.Headers.Add("Accept", AcceptHeaders.RawContentMediaType);
var response = await RunRequest(request, CancellationToken.None).ConfigureAwait(false);

if (response.Body is Stream stream)
Expand All @@ -757,7 +815,8 @@ async Task<IApiResponse<byte[]>> GetRaw(IRequest request)

async Task<IApiResponse<Stream>> GetRawStream(IRequest request)
{
request.Headers.Add("Accept", AcceptHeaders.RawContentMediaType);
if (request.Headers.ContainsKey("Accept") is false)
request.Headers.Add("Accept", AcceptHeaders.RawContentMediaType);
var response = await RunRequest(request, CancellationToken.None).ConfigureAwait(false);

return new ApiResponse<Stream>(response, response.Body as Stream);
Expand Down
20 changes: 20 additions & 0 deletions Octokit/Http/IApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ public interface IApiConnection
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<string> GetHtml(Uri uri, IDictionary<string, string> parameters);

/// <summary>
/// Gets the raw content of the API resource at the specified URI.
/// </summary>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <param name="parameters">Parameters to add to the API request</param>
/// <returns>The API resource's raw content or <c>null</c> if the <paramref name="uri"/> points to a directory.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<byte[]> GetRaw(Uri uri, IDictionary<string, string> parameters, string accepts);

/// <summary>
/// Gets the raw stream of the API resource at the specified URI.
/// </summary>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="parameters">Parameters to add to the API request</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <returns>The API resource's raw stream or <c>null</c> if the <paramref name="uri"/> points to a directory.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<Stream> GetRawStream(Uri uri, IDictionary<string, string> parameters, string accepts);

/// <summary>
/// Gets the raw content of the API resource at the specified URI.
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions Octokit/Http/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,37 @@ public interface IConnection : IApiInfoProvider
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
Task<IApiResponse<string>> GetHtml(Uri uri, IDictionary<string, string> parameters);

/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
/// <param name="uri">URI endpoint to send request to</param>
/// <param name="parameters">Querystring parameters for the request</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
/// <remarks>The <see cref="IResponse.Body"/> property will be <c>null</c> if the <paramref name="uri"/> points to a directory instead of a file</remarks>
Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> parameters, string accepts);

/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
/// <param name="uri">URI endpoint to send request to</param>
/// <param name="parameters">Querystring parameters for the request</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <param name="timeout">The Timeout value</param>
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
/// <remarks>The <see cref="IResponse.Body"/> property will be <c>null</c> if the <paramref name="uri"/> points to a directory instead of a file</remarks>
Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> parameters, TimeSpan timeout, string accepts);

/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
/// <param name="uri">URI endpoint to send request to</param>
/// <param name="parameters">Querystring parameters for the request</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
/// <remarks>The <see cref="IResponse.Body"/> property will be <c>null</c> if the <paramref name="uri"/> points to a directory instead of a file</remarks>
Task<IApiResponse<Stream>> GetRawStream(Uri uri, IDictionary<string, string> parameters, string accepts);

/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
Expand Down