Skip to content

Commit

Permalink
Merge pull request #440 from nigel-sampson/http-accepts
Browse files Browse the repository at this point in the history
Accepts parameter for Patch
  • Loading branch information
haacked committed Apr 16, 2014
2 parents 2f17209 + 2b470ed commit 9e489b6
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Octokit.Tests/Http/ApiConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,29 @@ public async Task MakesGetRequestForItem()
connection.Received().GetAsync<object>(getUri);
}

[Fact]
public async Task MakesGetRequestForItemWithAcceptsOverride()
{
var getUri = new Uri("anything", UriKind.Relative);
var accepts = "custom/accepts";
IResponse<object> response = new ApiResponse<object> { BodyAsObject = new object() };
var connection = Substitute.For<IConnection>();
connection.GetAsync<object>(Args.Uri, null, Args.String).Returns(Task.FromResult(response));
var apiConnection = new ApiConnection(connection);

var data = await apiConnection.Get<object>(getUri, null, accepts);

Assert.Same(response.BodyAsObject, data);
connection.Received().GetAsync<object>(getUri, null, accepts);
}

[Fact]
public async Task EnsuresArgumentNotNull()
{
var getUri = new Uri("anything", UriKind.Relative);
var client = new ApiConnection(Substitute.For<IConnection>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.Get<object>(null));
await AssertEx.Throws<ArgumentNullException>(async () => await client.Get<object>(getUri, new Dictionary<string, string>(), null));
}
}

Expand Down Expand Up @@ -122,13 +140,31 @@ public async Task MakesPatchRequestWithSuppliedData()
connection.Received().PatchAsync<object>(patchUri, sentData);
}

[Fact]
public async Task MakesPatchRequestWithAcceptsOverride()
{
var patchUri = new Uri("anything", UriKind.Relative);
var sentData = new object();
var accepts = "custom/accepts";
IResponse<object> response = new ApiResponse<object> { BodyAsObject = new object() };
var connection = Substitute.For<IConnection>();
connection.PatchAsync<object>(Args.Uri, Args.Object, Args.String).Returns(Task.FromResult(response));
var apiConnection = new ApiConnection(connection);

var data = await apiConnection.Patch<object>(patchUri, sentData, accepts);

Assert.Same(data, response.BodyAsObject);
connection.Received().PatchAsync<object>(patchUri, sentData, accepts);
}

[Fact]
public async Task EnsuresArgumentNotNull()
{
var connection = new ApiConnection(Substitute.For<IConnection>());
var patchUri = new Uri("", UriKind.Relative);
await AssertEx.Throws<ArgumentNullException>(async () => await connection.Patch<object>(null, new object()));
await AssertEx.Throws<ArgumentNullException>(async () => await connection.Patch<object>(patchUri, null));
await AssertEx.Throws<ArgumentNullException>(async () => await connection.Patch<object>(patchUri, new object(), null));
}
}

Expand Down
18 changes: 18 additions & 0 deletions Octokit.Tests/Http/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,24 @@ public async Task RunsConfiguredAppWithAppropriateEnv()
req.ContentType == "application/x-www-form-urlencoded" &&
req.Endpoint == new Uri("endpoint", UriKind.Relative)), Args.CancellationToken);
}

[Fact]
public async Task RunsConfiguredAppWithAcceptsOverride()
{
string data = SimpleJson.SerializeObject(new object());
var httpClient = Substitute.For<IHttpClient>();
IResponse<string> response = new ApiResponse<string>();
httpClient.Send<string>(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
var connection = new Connection(new ProductHeaderValue("OctokitTests"),
ExampleUri,
Substitute.For<ICredentialStore>(),
httpClient,
Substitute.For<IJsonSerializer>());

await connection.PatchAsync<string>(new Uri("endpoint", UriKind.Relative), new object(), "custom/accepts");

httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req => req.Headers["Accept"] == "custom/accepts"), Args.CancellationToken);
}
}

public class ThePutAsyncMethod
Expand Down
38 changes: 38 additions & 0 deletions Octokit/Http/ApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ public async Task<T> Get<T>(Uri uri, IDictionary<string, string> parameters)
return response.BodyAsObject;
}

/// <summary>
/// Gets the API resource at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource to get.</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>The API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<T> Get<T>(Uri uri, IDictionary<string, string> parameters, string accepts)
{
Ensure.ArgumentNotNull(uri, "uri");
Ensure.ArgumentNotNull(accepts, "accepts");

var response = await Connection.GetAsync<T>(uri, parameters, accepts).ConfigureAwait(false);
return response.BodyAsObject;
}

/// <summary>
/// Gets the HTML content of the API resource at the specified URI.
/// </summary>
Expand Down Expand Up @@ -236,6 +254,26 @@ public async Task<T> Patch<T>(Uri uri, object data)
return response.BodyAsObject;
}

/// <summary>
/// Updates the API resource at the specified URI.
/// </summary>
/// <typeparam name="T">The API resource's type.</typeparam>
/// <param name="uri">URI of the API resource to update</param>
/// <param name="data">Object that describes the 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 updated API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public async Task<T> Patch<T>(Uri uri, object data, string accepts)
{
Ensure.ArgumentNotNull(uri, "uri");
Ensure.ArgumentNotNull(data, "data");
Ensure.ArgumentNotNull(accepts, "accepts");

var response = await Connection.PatchAsync<T>(uri, data, accepts).ConfigureAwait(false);

return response.BodyAsObject;
}

/// <summary>
/// Deletes the API object at the specified URI.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions Octokit/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ public Task<IResponse<T>> PatchAsync<T>(Uri uri, object body)
return SendData<T>(uri, HttpVerb.Patch, body, null, null, CancellationToken.None);
}

public Task<IResponse<T>> PatchAsync<T>(Uri uri, object body, string accepts)
{
Ensure.ArgumentNotNull(uri, "uri");
Ensure.ArgumentNotNull(body, "body");
Ensure.ArgumentNotNull(accepts, "accepts");

return SendData<T>(uri, HttpVerb.Patch, body, accepts, null, CancellationToken.None);
}

public Task<IResponse<T>> PostAsync<T>(Uri uri, object body, string accepts, string contentType)
{
Ensure.ArgumentNotNull(uri, "uri");
Expand Down
24 changes: 24 additions & 0 deletions Octokit/Http/IApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public interface IApiConnection
Justification = "It's fiiiine. It's fine. Trust us.")]
Task<T> Get<T>(Uri uri, IDictionary<string, string> parameters);

/// <summary>
/// Gets the API resource at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource to get.</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>The API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get",
Justification = "It's fiiiine. It's fine. Trust us.")]
Task<T> Get<T>(Uri uri, IDictionary<string, string> parameters, string accepts);

/// <summary>
/// Gets the HTML content of the API resource at the specified URI.
/// </summary>
Expand Down Expand Up @@ -139,6 +152,17 @@ public interface IApiConnection
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<T> Patch<T>(Uri uri, object data);

/// <summary>
/// Updates the API resource at the specified URI.
/// </summary>
/// <typeparam name="T">The API resource's type.</typeparam>
/// <param name="uri">URI of the API resource to update</param>
/// <param name="data">Object that describes the 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 updated API resource.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<T> Patch<T>(Uri uri, object data, string accepts);

/// <summary>
/// Deletes the API object at the specified URI.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions Octokit/Http/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public interface IConnection
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
Task<IResponse<T>> PatchAsync<T>(Uri uri, object body);

/// <summary>
/// Performs an asynchronous HTTP PATCH request.
/// Attempts to map the response body to an object of type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">The type to map the response to</typeparam>
/// <param name="uri">URI endpoint to send request to</param>
/// <param name="body">The object to serialize as the body of the request</param>
/// <param name="accepts">Specifies accepted response media types.</param>
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
Task<IResponse<T>> PatchAsync<T>(Uri uri, object body, string accepts);

/// <summary>
/// Performs an asynchronous HTTP POST request.
/// Attempts to map the response body to an object of type <typeparamref name="T"/>
Expand Down

0 comments on commit 9e489b6

Please sign in to comment.