From 90aa5696a0fefa54064a5ca9b0b72e7f0d399565 Mon Sep 17 00:00:00 2001 From: Nigel Sampson Date: Wed, 26 Mar 2014 22:58:37 +1300 Subject: [PATCH 1/3] Accepts parameter for Patch --- Octokit/Http/ApiConnection.cs | 19 +++++++++++++++++++ Octokit/Http/Connection.cs | 8 ++++++++ Octokit/Http/IApiConnection.cs | 11 +++++++++++ Octokit/Http/IConnection.cs | 11 +++++++++++ 4 files changed, 49 insertions(+) diff --git a/Octokit/Http/ApiConnection.cs b/Octokit/Http/ApiConnection.cs index 1b4c15241f..7276e3b82b 100644 --- a/Octokit/Http/ApiConnection.cs +++ b/Octokit/Http/ApiConnection.cs @@ -236,6 +236,25 @@ public async Task Patch(Uri uri, object data) return response.BodyAsObject; } + /// + /// Updates the API resource at the specified URI. + /// + /// The API resource's type. + /// URI of the API resource to update + /// Object that describes the API resource; this will be serialized and used as the request's body + /// Accept header to use for the API request + /// The updated API resource. + /// Thrown when an API error occurs. + public async Task Patch(Uri uri, object data, string accepts) + { + Ensure.ArgumentNotNull(uri, "uri"); + Ensure.ArgumentNotNull(data, "data"); + + var response = await Connection.PatchAsync(uri, data, accepts).ConfigureAwait(false); + + return response.BodyAsObject; + } + /// /// Deletes the API object at the specified URI. /// diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index 9f0f3e7b13..d7b79b79ac 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -177,6 +177,14 @@ public Task> PatchAsync(Uri uri, object body) return SendData(uri, HttpVerb.Patch, body, null, null, CancellationToken.None); } + public Task> PatchAsync(Uri uri, object body, string accepts) + { + Ensure.ArgumentNotNull(uri, "uri"); + Ensure.ArgumentNotNull(body, "body"); + + return SendData(uri, HttpVerb.Patch, body, accepts, null, CancellationToken.None); + } + public Task> PostAsync(Uri uri, object body, string accepts, string contentType) { Ensure.ArgumentNotNull(uri, "uri"); diff --git a/Octokit/Http/IApiConnection.cs b/Octokit/Http/IApiConnection.cs index 353d6c25f4..3daa3157db 100644 --- a/Octokit/Http/IApiConnection.cs +++ b/Octokit/Http/IApiConnection.cs @@ -139,6 +139,17 @@ public interface IApiConnection /// Thrown when an API error occurs. Task Patch(Uri uri, object data); + /// + /// Updates the API resource at the specified URI. + /// + /// The API resource's type. + /// URI of the API resource to update + /// Object that describes the API resource; this will be serialized and used as the request's body + /// Accept header to use for the API request + /// The updated API resource. + /// Thrown when an API error occurs. + Task Patch(Uri uri, object data, string accepts); + /// /// Deletes the API object at the specified URI. /// diff --git a/Octokit/Http/IConnection.cs b/Octokit/Http/IConnection.cs index f1f8f20154..94ed197899 100644 --- a/Octokit/Http/IConnection.cs +++ b/Octokit/Http/IConnection.cs @@ -53,6 +53,17 @@ public interface IConnection /// representing the received HTTP response Task> PatchAsync(Uri uri, object body); + /// + /// Performs an asynchronous HTTP PATCH request. + /// Attempts to map the response body to an object of type + /// + /// The type to map the response to + /// URI endpoint to send request to + /// The object to serialize as the body of the request + /// Specifies accepted response media types. + /// representing the received HTTP response + Task> PatchAsync(Uri uri, object body, string accepts); + /// /// Performs an asynchronous HTTP POST request. /// Attempts to map the response body to an object of type From 034da197da9a5ca7cb173b5fb8c1608fe1b37ea9 Mon Sep 17 00:00:00 2001 From: Nigel Sampson Date: Wed, 26 Mar 2014 23:08:00 +1300 Subject: [PATCH 2/3] Accepts parameter for Get --- Octokit/Http/ApiConnection.cs | 17 +++++++++++++++++ Octokit/Http/IApiConnection.cs | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Octokit/Http/ApiConnection.cs b/Octokit/Http/ApiConnection.cs index 7276e3b82b..ec97a9268f 100644 --- a/Octokit/Http/ApiConnection.cs +++ b/Octokit/Http/ApiConnection.cs @@ -58,6 +58,23 @@ public async Task Get(Uri uri, IDictionary parameters) return response.BodyAsObject; } + /// + /// Gets the API resource at the specified URI. + /// + /// Type of the API resource to get. + /// URI of the API resource to get + /// Parameters to add to the API request + /// Accept header to use for the API request + /// The API resource. + /// Thrown when an API error occurs. + public async Task Get(Uri uri, IDictionary parameters, string accepts) + { + Ensure.ArgumentNotNull(uri, "uri"); + + var response = await Connection.GetAsync(uri, parameters, accepts).ConfigureAwait(false); + return response.BodyAsObject; + } + /// /// Gets the HTML content of the API resource at the specified URI. /// diff --git a/Octokit/Http/IApiConnection.cs b/Octokit/Http/IApiConnection.cs index 3daa3157db..9781a82d25 100644 --- a/Octokit/Http/IApiConnection.cs +++ b/Octokit/Http/IApiConnection.cs @@ -29,6 +29,19 @@ public interface IApiConnection Justification = "It's fiiiine. It's fine. Trust us.")] Task Get(Uri uri, IDictionary parameters); + /// + /// Gets the API resource at the specified URI. + /// + /// Type of the API resource to get. + /// URI of the API resource to get + /// Parameters to add to the API request + /// Accept header to use for the API request + /// The API resource. + /// Thrown when an API error occurs. + [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", + Justification = "It's fiiiine. It's fine. Trust us.")] + Task Get(Uri uri, IDictionary parameters, string accepts); + /// /// Gets the HTML content of the API resource at the specified URI. /// From 2b470ed5dcfbb71c8280239df637eac0ff03eb88 Mon Sep 17 00:00:00 2001 From: Nigel Sampson Date: Tue, 8 Apr 2014 10:28:48 +1200 Subject: [PATCH 3/3] Unit tests accepts overrides --- Octokit.Tests/Http/ApiConnectionTests.cs | 36 ++++++++++++++++++++++++ Octokit.Tests/Http/ConnectionTests.cs | 18 ++++++++++++ Octokit/Http/ApiConnection.cs | 2 ++ Octokit/Http/Connection.cs | 1 + 4 files changed, 57 insertions(+) diff --git a/Octokit.Tests/Http/ApiConnectionTests.cs b/Octokit.Tests/Http/ApiConnectionTests.cs index 2a0d5bd2e6..8d3089e798 100644 --- a/Octokit.Tests/Http/ApiConnectionTests.cs +++ b/Octokit.Tests/Http/ApiConnectionTests.cs @@ -30,11 +30,29 @@ public async Task MakesGetRequestForItem() connection.Received().GetAsync(getUri); } + [Fact] + public async Task MakesGetRequestForItemWithAcceptsOverride() + { + var getUri = new Uri("anything", UriKind.Relative); + var accepts = "custom/accepts"; + IResponse response = new ApiResponse { BodyAsObject = new object() }; + var connection = Substitute.For(); + connection.GetAsync(Args.Uri, null, Args.String).Returns(Task.FromResult(response)); + var apiConnection = new ApiConnection(connection); + + var data = await apiConnection.Get(getUri, null, accepts); + + Assert.Same(response.BodyAsObject, data); + connection.Received().GetAsync(getUri, null, accepts); + } + [Fact] public async Task EnsuresArgumentNotNull() { + var getUri = new Uri("anything", UriKind.Relative); var client = new ApiConnection(Substitute.For()); await AssertEx.Throws(async () => await client.Get(null)); + await AssertEx.Throws(async () => await client.Get(getUri, new Dictionary(), null)); } } @@ -122,6 +140,23 @@ public async Task MakesPatchRequestWithSuppliedData() connection.Received().PatchAsync(patchUri, sentData); } + [Fact] + public async Task MakesPatchRequestWithAcceptsOverride() + { + var patchUri = new Uri("anything", UriKind.Relative); + var sentData = new object(); + var accepts = "custom/accepts"; + IResponse response = new ApiResponse { BodyAsObject = new object() }; + var connection = Substitute.For(); + connection.PatchAsync(Args.Uri, Args.Object, Args.String).Returns(Task.FromResult(response)); + var apiConnection = new ApiConnection(connection); + + var data = await apiConnection.Patch(patchUri, sentData, accepts); + + Assert.Same(data, response.BodyAsObject); + connection.Received().PatchAsync(patchUri, sentData, accepts); + } + [Fact] public async Task EnsuresArgumentNotNull() { @@ -129,6 +164,7 @@ public async Task EnsuresArgumentNotNull() var patchUri = new Uri("", UriKind.Relative); await AssertEx.Throws(async () => await connection.Patch(null, new object())); await AssertEx.Throws(async () => await connection.Patch(patchUri, null)); + await AssertEx.Throws(async () => await connection.Patch(patchUri, new object(), null)); } } diff --git a/Octokit.Tests/Http/ConnectionTests.cs b/Octokit.Tests/Http/ConnectionTests.cs index ad28c2b137..cce92d1d81 100644 --- a/Octokit.Tests/Http/ConnectionTests.cs +++ b/Octokit.Tests/Http/ConnectionTests.cs @@ -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(); + IResponse response = new ApiResponse(); + httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); + var connection = new Connection(new ProductHeaderValue("OctokitTests"), + ExampleUri, + Substitute.For(), + httpClient, + Substitute.For()); + + await connection.PatchAsync(new Uri("endpoint", UriKind.Relative), new object(), "custom/accepts"); + + httpClient.Received(1).Send(Arg.Is(req => req.Headers["Accept"] == "custom/accepts"), Args.CancellationToken); + } } public class ThePutAsyncMethod diff --git a/Octokit/Http/ApiConnection.cs b/Octokit/Http/ApiConnection.cs index ec97a9268f..ae43af8ebf 100644 --- a/Octokit/Http/ApiConnection.cs +++ b/Octokit/Http/ApiConnection.cs @@ -70,6 +70,7 @@ public async Task Get(Uri uri, IDictionary parameters) public async Task Get(Uri uri, IDictionary parameters, string accepts) { Ensure.ArgumentNotNull(uri, "uri"); + Ensure.ArgumentNotNull(accepts, "accepts"); var response = await Connection.GetAsync(uri, parameters, accepts).ConfigureAwait(false); return response.BodyAsObject; @@ -266,6 +267,7 @@ public async Task Patch(Uri uri, object data, string accepts) { Ensure.ArgumentNotNull(uri, "uri"); Ensure.ArgumentNotNull(data, "data"); + Ensure.ArgumentNotNull(accepts, "accepts"); var response = await Connection.PatchAsync(uri, data, accepts).ConfigureAwait(false); diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index d7b79b79ac..c9ace05734 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -181,6 +181,7 @@ public Task> PatchAsync(Uri uri, object body, string accepts) { Ensure.ArgumentNotNull(uri, "uri"); Ensure.ArgumentNotNull(body, "body"); + Ensure.ArgumentNotNull(accepts, "accepts"); return SendData(uri, HttpVerb.Patch, body, accepts, null, CancellationToken.None); }