Fix releases client to pass accepts header

This commit is contained in:
Haacked
2013-10-15 11:47:44 -07:00
parent 25c83ac206
commit 50a197742d
11 changed files with 121 additions and 57 deletions
@@ -32,7 +32,7 @@ namespace Octokit.Tests.Clients
authEndpoint.GetAll();
client.Received().GetAll<Authorization>(Arg.Is<Uri>(u => u.ToString() == "/authorizations"), null);
client.Received().GetAll<Authorization>(Arg.Is<Uri>(u => u.ToString() == "/authorizations"));
}
}
@@ -54,7 +54,7 @@ namespace Octokit.Tests.Clients
orgs.GetAll("username");
client.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "/users/username/orgs"), null);
client.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "/users/username/orgs"));
}
[Fact]
@@ -76,7 +76,7 @@ namespace Octokit.Tests.Clients
orgs.GetAllForCurrent();
client.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "/user/orgs"), null);
client.Received().GetAll<Organization>(Arg.Is<Uri>(u => u.ToString() == "/user/orgs"));
}
}
}
+12 -6
View File
@@ -11,7 +11,6 @@ namespace Octokit.Tests.Clients
{
public class ReleasesClientTests
{
public class TheGetReleasesMethod
{
[Fact]
@@ -22,7 +21,9 @@ namespace Octokit.Tests.Clients
releasesClient.GetAll("fake", "repo");
client.Received().GetAll<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"), null);
client.Received().GetAll<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"),
null,
"application/vnd.github.manifold-preview");
}
[Fact]
@@ -46,7 +47,9 @@ namespace Octokit.Tests.Clients
releasesClient.CreateRelease("fake", "repo", data);
client.Received().Post<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"), data);
client.Received().Post<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"),
data,
"application/vnd.github.manifold-preview");
}
[Fact]
@@ -56,9 +59,12 @@ namespace Octokit.Tests.Clients
var data = new ReleaseUpdate("fake-tag");
Assert.Throws<ArgumentNullException>(() => new ReleaseUpdate(null));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.CreateRelease(null, "name", data));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.CreateRelease("owner", null, data));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.CreateRelease("owner", "name", null));
await AssertEx.Throws<ArgumentNullException>(async () =>
await releasesClient.CreateRelease(null, "name", data));
await AssertEx.Throws<ArgumentNullException>(async () =>
await releasesClient.CreateRelease("owner", null, data));
await AssertEx.Throws<ArgumentNullException>(async () =>
await releasesClient.CreateRelease("owner", "name", null));
}
}
@@ -151,7 +151,7 @@ namespace Octokit.Tests.Clients
repositoriesClient.GetAllForCurrent();
client.Received()
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "user/repos"), null);
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "user/repos"));
}
}
@@ -166,7 +166,7 @@ namespace Octokit.Tests.Clients
repositoriesClient.GetAllForUser("username");
client.Received()
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "/users/username/repos"), null);
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "/users/username/repos"));
}
[Fact]
@@ -189,7 +189,7 @@ namespace Octokit.Tests.Clients
repositoriesClient.GetAllForOrg("orgname");
client.Received()
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "/orgs/orgname/repos"), null);
.GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "/orgs/orgname/repos"));
}
[Fact]
+14 -8
View File
@@ -75,20 +75,30 @@ namespace Octokit.Tests.Http
BodyAsObject = new List<object> {new object(), new object()}
};
var connection = Substitute.For<IConnection>();
connection.GetAsync<List<object>>(Args.Uri, null).Returns(Task.FromResult(response));
connection.GetAsync<List<object>>(Args.Uri, null, null).Returns(Task.FromResult(response));
var apiConnection = new ApiConnection(connection);
var data = await apiConnection.GetAll<object>(getAllUri);
Assert.Equal(2, data.Count);
connection.Received().GetAsync<List<object>>(getAllUri, null);
connection.Received().GetAsync<List<object>>(getAllUri, null, null);
}
[Fact]
public async Task EnsuresArgumentNotNull()
{
var client = new ApiConnection(Substitute.For<IConnection>());
// One argument
await AssertEx.Throws<ArgumentNullException>(async () => await client.GetAll<object>(null));
// Two argument
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.GetAll<object>(null, new Dictionary<string, string>()));
// Three arguments
await AssertEx.Throws<ArgumentNullException>(async () =>
await client.GetAll<object>(null, new Dictionary<string, string>(), "accepts"));
}
}
@@ -129,13 +139,13 @@ namespace Octokit.Tests.Http
var sentData = new object();
IResponse<object> response = new ApiResponse<object> {BodyAsObject = new object()};
var connection = Substitute.For<IConnection>();
connection.PostAsync<object>(Args.Uri, Args.Object).Returns(Task.FromResult(response));
connection.PostAsync<object>(Args.Uri, Args.Object, null, null).Returns(Task.FromResult(response));
var apiConnection = new ApiConnection(connection);
var data = await apiConnection.Post<object>(postUri, sentData);
Assert.Same(data, response.BodyAsObject);
connection.Received().PostAsync<object>(postUri, sentData);
connection.Received().PostAsync<object>(postUri, sentData, null, null);
}
[Fact]
@@ -171,10 +181,6 @@ namespace Octokit.Tests.Http
await connection.Post<object>(null, new MemoryStream(), "anAccept", "some-content-type"));
await AssertEx.Throws<ArgumentNullException>(async () =>
await connection.Post<object>(postUri, null, "anAccept", "some-content-type"));
await AssertEx.Throws<ArgumentNullException>(async () =>
await connection.Post<object>(postUri, new MemoryStream(), null, "content-type"));
await AssertEx.Throws<ArgumentNullException>(async () =>
await connection.Post<object>(postUri, new MemoryStream(), "accepts", null));
}
}
+6 -11
View File
@@ -1,8 +1,6 @@
#if NET_45
using System.Collections.Generic;
#endif
using System;
using System.IO;
using System.Threading.Tasks;
namespace Octokit
@@ -19,10 +17,9 @@ namespace Octokit
Ensure.ArgumentNotNullOrEmptyString(name, "repository");
var endpoint = "/repos/{0}/{1}/releases".FormatUri(owner, name);
return await Client.GetAll<Release>(endpoint);
return await Client.GetAll<Release>(endpoint, null, "application/vnd.github.manifold-preview");
}
public async Task<Release> CreateRelease(string owner, string name, ReleaseUpdate data)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
@@ -30,22 +27,20 @@ namespace Octokit
Ensure.ArgumentNotNull(data, "data");
var endpoint = "/repos/{0}/{1}/releases".FormatUri(owner, name);
return await Client.Post<Release>(endpoint, data);
return await Client.Post<Release>(endpoint, data, "application/vnd.github.manifold-preview");
}
public async Task<ReleaseAsset> UploadAsset(Release release, ReleaseAssetUpload data)
{
Ensure.ArgumentNotNull(release, "release");
Ensure.ArgumentNotNull(data, "data");
var endpoint = release.UploadUrl.ExpandUriTemplate(new { name = data.FileName });
var endpoint = release.UploadUrl.ExpandUriTemplate(new {name = data.FileName});
return await Client.Post<ReleaseAsset>(
endpoint,
data.RawData,
"application/vnd.github.manifold-preview",
endpoint,
data.RawData,
"application/vnd.github.manifold-preview",
data.ContentType);
}
}
}
+41 -12
View File
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Octokit.Internal;
@@ -72,6 +71,18 @@ namespace Octokit
return response.Body;
}
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <param name="uri">URI of the API resource to get.</param>
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<IReadOnlyList<T>> GetAll<T>(Uri uri)
{
return await GetAll<T>(uri, null, null);
}
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
@@ -81,10 +92,24 @@ namespace Octokit
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters)
{
return await GetAll<T>(uri, parameters, null);
}
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <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><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters, string accepts)
{
Ensure.ArgumentNotNull(uri, "uri");
return await _pagination.GetAllPages(async () => await GetPage<T>(uri, parameters));
return await _pagination.GetAllPages(async () => await GetPage<T>(uri, parameters, accepts));
}
/// <summary>
@@ -100,9 +125,12 @@ namespace Octokit
Ensure.ArgumentNotNull(uri, "uri");
Ensure.ArgumentNotNull(data, "data");
var response = await Connection.PostAsync<T>(uri, data);
return await Post<T>(uri, data, null, null);
}
return response.BodyAsObject;
public async Task<T> Post<T>(Uri endpoint, object data, string contentType)
{
return await Post<T>(endpoint, data, contentType, null);
}
/// <summary>
@@ -110,21 +138,19 @@ namespace Octokit
/// </summary>
/// <typeparam name="T">The API resource's type.</typeparam>
/// <param name="uri">URI of the API resource to get.</param>
/// <param name="rawData">A <see cref="Stream"/> to use as the API request's body.</param>
/// <param name="data">Object that describes the new API resource; this will be serialized and used as the request's body.</param>
/// <param name="contentType">Content type of the API request.</param>
/// <param name="accepts">Accept header to use for the API request.</param>
/// <returns>The created API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<T> Post<T>(Uri uri, Stream rawData, string contentType, string accepts)
public async Task<T> Post<T>(Uri uri, object data, string contentType, string accepts)
{
Ensure.ArgumentNotNull(uri, "uri");
Ensure.ArgumentNotNull(rawData, "rawData");
Ensure.ArgumentNotNull(contentType, "contentType");
Ensure.ArgumentNotNull(accepts, "accepts");
Ensure.ArgumentNotNull(data, "data");
var response = await Connection.PostAsync<T>(
uri,
rawData,
data,
contentType,
accepts);
return response.BodyAsObject;
@@ -198,11 +224,14 @@ namespace Octokit
await Connection.DeleteAsync(uri);
}
async Task<IReadOnlyPagedCollection<T>> GetPage<T>(Uri endpoint, IDictionary<string, string> parameters)
async Task<IReadOnlyPagedCollection<T>> GetPage<T>(
Uri endpoint,
IDictionary<string, string> parameters,
string accepts)
{
Ensure.ArgumentNotNull(endpoint, "endpoint");
var response = await Connection.GetAsync<List<T>>(endpoint, parameters);
var response = await Connection.GetAsync<List<T>>(endpoint, parameters, accepts);
return new ReadOnlyPagedCollection<T>(response, Connection);
}
}
+8 -6
View File
@@ -74,12 +74,14 @@ namespace Octokit
{
Ensure.ArgumentNotNull(endpoint, "endpoint");
return await Run<T>(new Request
{
Method = HttpMethod.Get,
BaseAddress = BaseAddress,
Endpoint = endpoint.ApplyParameters(parameters)
});
return await GetAsync<T>(endpoint, parameters, null);
}
public async Task<IResponse<T>> GetAsync<T>(Uri endpoint, IDictionary<string, string> parameters, string accepts)
{
Ensure.ArgumentNotNull(endpoint, "endpoint");
return await SendData<T>(endpoint.ApplyParameters(parameters), HttpMethod.Get, null, accepts: accepts);
}
public async Task<IResponse<string>> GetHtml(Uri endpoint, IDictionary<string, string> parameters)
+33 -3
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;
namespace Octokit
@@ -33,6 +32,15 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<string> GetHtml(Uri uri, IDictionary<string, string> parameters);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <param name="uri">URI of the API resource to get.</param>
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
@@ -43,6 +51,17 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <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><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters, string accepts);
/// <summary>
/// Creates a new API resource in the list at the specified URI.
/// </summary>
@@ -58,12 +77,23 @@ namespace Octokit
/// </summary>
/// <typeparam name="T">The API resource's type.</typeparam>
/// <param name="uri">URI of the API resource to get.</param>
/// <param name="rawData">A <see cref="Stream"/> to use as the API request's body.</param>
/// <param name="data">Object that describes the new API resource; this will be serialized and used as the request's body.</param>
/// <param name="accepts">Accept header to use for the API request.</param>
/// <returns>The created API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<T> Post<T>(Uri uri, object data, string accepts);
/// <summary>
/// Creates a new API resource in the list at the specified URI.
/// </summary>
/// <typeparam name="T">The API resource's type.</typeparam>
/// <param name="uri">URI of the API resource to get.</param>
/// <param name="data">Object that describes the new API resource; this will be serialized and used as the request's body.</param>
/// <param name="contentType">Content type of the API request.</param>
/// <param name="accepts">Accept header to use for the API request.</param>
/// <returns>The created API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<T> Post<T>(Uri uri, Stream rawData, string contentType, string accepts);
Task<T> Post<T>(Uri uri, object data, string contentType, string accepts);
/// <summary>
/// Creates or replaces the API resource at the specified URI.
+1 -2
View File
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
namespace Octokit
@@ -9,13 +8,13 @@ namespace Octokit
{
Task<IResponse<string>> GetHtml(Uri endpoint, IDictionary<string, string> parameters);
Task<IResponse<T>> GetAsync<T>(Uri endpoint, IDictionary<string, string> parameters);
Task<IResponse<T>> GetAsync<T>(Uri endpoint, IDictionary<string, string> parameters, string accepts);
Task<IResponse<T>> PatchAsync<T>(Uri endpoint, object body);
Task<IResponse<T>> PostAsync<T>(Uri endpoint, object body);
Task<IResponse<T>> PostAsync<T>(Uri endpoint, object body, string contentType, string accepts);
Task<IResponse<T>> PutAsync<T>(Uri endpoint, object body);
Task<IResponse<T>> PutAsync<T>(Uri endpoint, object body, string twoFactorAuthenticationCode);
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
Task DeleteAsync(Uri endpoint);
Uri BaseAddress { get; }
-3
View File
@@ -32,9 +32,6 @@ namespace Octokit.Internal
request.Headers["Accept"] = "application/vnd.github.v3+json; charset=utf-8";
}
if (request.Endpoint != null && request.Endpoint.ToString().Contains("releases"))
request.Headers["Accept"] = "application/vnd.github.manifold-preview; charset=utf-8";
if (request.Method == HttpMethod.Get || request.Body == null) return;
if (request.Body is string || request.Body is Stream) return;