From 1fb6e57af2c6191ef8f2ca88b6d90359b02cc051 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 15 Sep 2023 16:35:23 +0530 Subject: [PATCH 1/6] Added a separate ably request method with string json body insetad of JToken --- README.md | 2 +- src/IO.Ably.Shared/AblyRest.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7050b8a4c..1abf7ed0a 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,7 @@ DateTimeOffset time = await client.TimeAsync(); } ] }"; - var paginatedResponse = await ablyRest.Request(HttpMethod.Post, "/messages", null, JObject.Parse(jsonPayload), null); + var paginatedResponse = await ablyRest.Request(HttpMethod.Post, "/messages", null, jsonPayload, null); ``` - Follow official [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints. diff --git a/src/IO.Ably.Shared/AblyRest.cs b/src/IO.Ably.Shared/AblyRest.cs index 78e5943ff..2d55801ca 100644 --- a/src/IO.Ably.Shared/AblyRest.cs +++ b/src/IO.Ably.Shared/AblyRest.cs @@ -318,12 +318,29 @@ internal async Task HttpPaginatedRequestInternal(Paginate /// (optional; may be null): an instance of RequestBody. It will be sent as a json object. /// (optional; may be null): any additional headers to send; see API-specific documentation. /// a page of results. + [Obsolete("Use Request method with json string request body instead")] public async Task Request(string method, string path, Dictionary requestParams = null, JToken body = null, Dictionary headers = null) { var httpMethod = new HttpMethod(method); return await Request(httpMethod, path, requestParams, body, headers); } + /// + /// Make a generic HTTP request against an endpoint representing a collection + /// of some type; this is to provide a forward compatibility path for new APIs. + /// + /// http method. + /// the path component of the resource URI. + /// (optional; may be null): any parameters to send with the request; see API-specific documentation. + /// (optional; may be null): a json string RequestBody. It will be sent as a json object. + /// (optional; may be null): any additional headers to send; see API-specific documentation. + /// a page of results. + public async Task Request(string method, string path, Dictionary requestParams = null, string body = null, Dictionary headers = null) + { + var httpMethod = new HttpMethod(method); + return await Request(httpMethod, path, requestParams, JToken.Parse(body), headers); + } + /// /// Make a generic HTTP request against an endpoint representing a collection /// of some type; this is to provide a forward compatibility path for new APIs. From f685fd9cb42e25ae7afa61895da5645018eee3e9 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 15 Sep 2023 16:58:58 +0530 Subject: [PATCH 2/6] Removed obsolete request method with jtoken param --- src/IO.Ably.Shared/AblyRest.cs | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/IO.Ably.Shared/AblyRest.cs b/src/IO.Ably.Shared/AblyRest.cs index 2d55801ca..4b7e74a53 100644 --- a/src/IO.Ably.Shared/AblyRest.cs +++ b/src/IO.Ably.Shared/AblyRest.cs @@ -308,23 +308,6 @@ internal async Task HttpPaginatedRequestInternal(Paginate return await ExecuteHttpPaginatedRequest(request, requestParams, HttpPaginatedRequestInternal); } - /// - /// Make a generic HTTP request against an endpoint representing a collection - /// of some type; this is to provide a forward compatibility path for new APIs. - /// - /// http method. - /// the path component of the resource URI. - /// (optional; may be null): any parameters to send with the request; see API-specific documentation. - /// (optional; may be null): an instance of RequestBody. It will be sent as a json object. - /// (optional; may be null): any additional headers to send; see API-specific documentation. - /// a page of results. - [Obsolete("Use Request method with json string request body instead")] - public async Task Request(string method, string path, Dictionary requestParams = null, JToken body = null, Dictionary headers = null) - { - var httpMethod = new HttpMethod(method); - return await Request(httpMethod, path, requestParams, body, headers); - } - /// /// Make a generic HTTP request against an endpoint representing a collection /// of some type; this is to provide a forward compatibility path for new APIs. @@ -338,7 +321,13 @@ public async Task Request(string method, string path, Dic public async Task Request(string method, string path, Dictionary requestParams = null, string body = null, Dictionary headers = null) { var httpMethod = new HttpMethod(method); - return await Request(httpMethod, path, requestParams, JToken.Parse(body), headers); + JToken requestBody = null; + if (body != null) + { + requestBody = JToken.Parse(body); + } + + return await Request(httpMethod, path, requestParams, requestBody, headers); } /// From 3c97ebf7324aa781f75f3f55bd318498d28ac277 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 15 Sep 2023 22:31:23 +0530 Subject: [PATCH 3/6] Updated requestsandboxtest to use the client request post method --- src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs b/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs index 8f3088d11..69e714edb 100644 --- a/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs @@ -5,6 +5,7 @@ using System.Net.Http; using System.Threading.Tasks; using FluentAssertions; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RichardSzalay.MockHttp; using Xunit; @@ -162,7 +163,7 @@ async Task ValidateMessages(string channelName, Action validate) data = "foo", } }; - var paginatedResponse = await client.Request(HttpMethod.Post, "/messages", null, JObject.FromObject(objectPayload), null); + var paginatedResponse = await client.Request(HttpMethod.Post, "/messages", null, JToken.FromObject(objectPayload), null); ValidateResponse(paginatedResponse); @@ -189,7 +190,7 @@ await ValidateMessages(channel, (message, messageIndex) => } ] }"; - paginatedResponse = await client.Request(HttpMethod.Post, "/messages", null, JObject.Parse(jsonPayload), null); + paginatedResponse = await client.Request(HttpMethod.Post.Method, "/messages", null, jsonPayload, null); ValidateResponse(paginatedResponse, 5); From 5a196b22ec6cb6ce6f0d82f884bd76d18e01858a Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sat, 16 Sep 2023 00:59:23 +0530 Subject: [PATCH 4/6] Updated example documentation for batch publish --- README.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1abf7ed0a..af388059e 100644 --- a/README.md +++ b/README.md @@ -419,16 +419,19 @@ DateTimeOffset time = await client.TimeAsync(); - It automatically adds necessary auth headers based on the initial auth config and supports pagination. - The following is an example of using the batch publish API based on the [Ably batch publish rest endpoint documentation](https://ably.com/docs/api/rest-api#batch-publish). ```csharp - var jsonPayload = - @"{ - ""channels"" : [ ""channel1"", ""channel2"" ], - ""messages"" : [ + var objectPayload = new + { + channels = new[] { "channel1", "channel2", "channel3", "channel4" }, + messages = new[] + { + new { - ""name"": ""eventName"", - ""data"" : ""message"", + name = "eventName", + data = "foo", } - ] - }"; + } + }; + var jsonPayload = JsonConvert.SerializeObject(objectPayload); var paginatedResponse = await ablyRest.Request(HttpMethod.Post, "/messages", null, jsonPayload, null); ``` - Follow official [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints. From e0f018cae29c7c996a4abd20202dd490c0fe32ad Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 27 Sep 2023 21:01:45 +0530 Subject: [PATCH 5/6] Deprecated ablyrest#Request method, refactored to RequestV2 method --- src/IO.Ably.Shared/AblyRest.cs | 9 ++++++++- src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/IO.Ably.Shared/AblyRest.cs b/src/IO.Ably.Shared/AblyRest.cs index 4b7e74a53..452114685 100644 --- a/src/IO.Ably.Shared/AblyRest.cs +++ b/src/IO.Ably.Shared/AblyRest.cs @@ -308,6 +308,13 @@ internal async Task HttpPaginatedRequestInternal(Paginate return await ExecuteHttpPaginatedRequest(request, requestParams, HttpPaginatedRequestInternal); } + [Obsolete("Use RequestV2 instead")] + public async Task Request(string method, string path, Dictionary requestParams = null, JToken body = null, Dictionary headers = null) + { + var httpMethod = new HttpMethod(method); + return await Request(httpMethod, path, requestParams, body, headers); + } + /// /// Make a generic HTTP request against an endpoint representing a collection /// of some type; this is to provide a forward compatibility path for new APIs. @@ -318,7 +325,7 @@ internal async Task HttpPaginatedRequestInternal(Paginate /// (optional; may be null): a json string RequestBody. It will be sent as a json object. /// (optional; may be null): any additional headers to send; see API-specific documentation. /// a page of results. - public async Task Request(string method, string path, Dictionary requestParams = null, string body = null, Dictionary headers = null) + public async Task RequestV2(string method, string path, Dictionary requestParams = null, string body = null, Dictionary headers = null) { var httpMethod = new HttpMethod(method); JToken requestBody = null; diff --git a/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs b/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs index 69e714edb..78369caf8 100644 --- a/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Rest/RequestSandBoxSpecs.cs @@ -76,12 +76,12 @@ public async Task Request_ShouldAcceptCorrectHttpVerbs(Protocol protocol) mockHttp.When(new HttpMethod(verb), "https://localhost/*").Respond(HttpStatusCode.OK, "application/json", "{ \"verb\": \"" + verb + "\" }"); client.HttpClient.Client = mockHttp.ToHttpClient(); - var response = await client.Request(verb, "/"); + var response = await client.RequestV2(verb, "/"); response.Success.Should().BeTrue($"'{verb}' verb should be supported "); response.StatusCode.Should().Be(HttpStatusCode.OK); response.Items.First()["verb"].ToString().Should().Be(verb); - var failedResponse = await client.Request("INVALID_HTTP_VERB", "/"); + var failedResponse = await client.RequestV2("INVALID_HTTP_VERB", "/"); failedResponse.Success.Should().BeFalse($"'INVALID_HTTP_VERB' should fail because '{verb}' verb is expected by mock HTTP server."); } } @@ -100,7 +100,7 @@ public async Task Request_SimpleGet(Protocol protocol) var testParams = new Dictionary { { "testParams", "testParamValue" } }; var testHeaders = new Dictionary { { "X-Test-Header", "testHeaderValue" } }; - var paginatedResponse = await client.Request(HttpMethod.Get.Method, _channelPath, testParams, null, testHeaders); + var paginatedResponse = await client.RequestV2(HttpMethod.Get.Method, _channelPath, testParams, null, testHeaders); _lastRequest.Headers.Should().ContainKey("Authorization"); _lastRequest.Headers.Should().ContainKey("X-Test-Header"); @@ -190,7 +190,7 @@ await ValidateMessages(channel, (message, messageIndex) => } ] }"; - paginatedResponse = await client.Request(HttpMethod.Post.Method, "/messages", null, jsonPayload, null); + paginatedResponse = await client.RequestV2(HttpMethod.Post.Method, "/messages", null, jsonPayload, null); ValidateResponse(paginatedResponse, 5); @@ -375,7 +375,7 @@ public async Task RequestFails_Non200StatusResponseShouldNotRaiseException(Proto })); client.HttpClient.SetPreferredHost("echo.ably.io/respondwith?status=400"); - var response = await client.Request(HttpMethod.Post.Method, "/"); + var response = await client.RequestV2(HttpMethod.Post.Method, "/"); response.Success.Should().BeFalse(); response.StatusCode.Should().Be(HttpStatusCode.BadRequest); From 110ae69d592c8fdb9c5034c1c7e9f192ddb3c36a Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 27 Sep 2023 21:07:48 +0530 Subject: [PATCH 6/6] Added missing doc to deprecated request method and updated readme --- README.md | 2 +- src/IO.Ably.Shared/AblyRest.cs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index af388059e..76cce65ec 100644 --- a/README.md +++ b/README.md @@ -432,7 +432,7 @@ DateTimeOffset time = await client.TimeAsync(); } }; var jsonPayload = JsonConvert.SerializeObject(objectPayload); - var paginatedResponse = await ablyRest.Request(HttpMethod.Post, "/messages", null, jsonPayload, null); + var paginatedResponse = await ablyRest.RequestV2(HttpMethod.Post, "/messages", null, jsonPayload, null); ``` - Follow official [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints. diff --git a/src/IO.Ably.Shared/AblyRest.cs b/src/IO.Ably.Shared/AblyRest.cs index 452114685..8abb88d48 100644 --- a/src/IO.Ably.Shared/AblyRest.cs +++ b/src/IO.Ably.Shared/AblyRest.cs @@ -308,6 +308,16 @@ internal async Task HttpPaginatedRequestInternal(Paginate return await ExecuteHttpPaginatedRequest(request, requestParams, HttpPaginatedRequestInternal); } + /// + /// Make a generic HTTP request against an endpoint representing a collection + /// of some type; this is to provide a forward compatibility path for new APIs. + /// + /// http method. + /// the path component of the resource URI. + /// (optional; may be null): any parameters to send with the request; see API-specific documentation. + /// (optional; may be null): RequestBody encoded into a JToken. It will be sent as a json object. + /// (optional; may be null): any additional headers to send; see API-specific documentation. + /// a page of results. [Obsolete("Use RequestV2 instead")] public async Task Request(string method, string path, Dictionary requestParams = null, JToken body = null, Dictionary headers = null) {