From ce7d790abecb96b3703e28fd238b30a97638f67f Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Tue, 9 Apr 2024 14:02:35 +0530 Subject: [PATCH 1/7] Fixed array push for single element --- src/Http.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Http.php b/src/Http.php index 566af24..2d35c60 100644 --- a/src/Http.php +++ b/src/Http.php @@ -134,10 +134,10 @@ public function request( $method, $url, $headers = [], $params = [] ) { $this->curl->setOpt( $ch, CURLOPT_POSTFIELDS, $params ); if ($this->postDataFormat == 'json') { - array_push( $headers, 'Content-Type: application/json' ); + $headers[] = 'Content-Type: application/json'; } elseif ($this->postDataFormat == 'msgpack') { - array_push( $headers, 'Content-Type: application/x-msgpack' ); + $headers[] = 'Content-Type: application/x-msgpack'; } } else { throw new AblyRequestException( 'Unknown $params format', -1, -1 ); From 772a04747292b77598c3853422c34a7f71f15867 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Tue, 9 Apr 2024 16:02:44 +0530 Subject: [PATCH 2/7] Added explicit documentation for php batch publish --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 1871dcd..8e00340 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,28 @@ If you're using Laravel and want to support **realtime broadcasting and events** > If you want **ably-php** as a rest dependency across service providers, check [ably-php-laravel](https://packagist.org/packages/ably/ably-php-laravel). **ably-php-laravel** is a simple wrapper over **ably-php** with laravel-specific classes. This has limited use-cases and **laravel-broadcaster** is recommended over **ably-php-laravel** for most use-cases. +### Making explicit HTTP requests to Ably Rest Endpoints / Batch publish +- The `AblyRest->Request` method can be used to make explicit HTTP requests to the [Ably REST API](https://ably.com/docs/api/rest-api). +- 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 objectPayload = new + { + channels = new[] { "channel1", "channel2", "channel3", "channel4" }, + messages = new[] + { + new + { + name = "eventName", + data = "foo", + } + } + }; + var jsonPayload = JsonConvert.SerializeObject(objectPayload); + var paginatedResponse = await ablyRest.RequestV2(HttpMethod.Post, "/messages", null, jsonPayload, null); +``` +- See the [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints. ## Support, feedback and troubleshooting From 481e3486b2e15bbed583ea1cfea9b72dd625055f Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 10 Apr 2024 17:31:55 +0530 Subject: [PATCH 3/7] Added explicit test for rest batch publish --- tests/AblyRestRequestTest.php | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/AblyRestRequestTest.php diff --git a/tests/AblyRestRequestTest.php b/tests/AblyRestRequestTest.php new file mode 100644 index 0000000..6ef57ab --- /dev/null +++ b/tests/AblyRestRequestTest.php @@ -0,0 +1,65 @@ +getOptions(); + self::$ably = new AblyRest( array_merge( self::$defaultOptions, [ + 'key' => self::$testApp->getAppKeyDefault()->string, + ] ) ); + } + + public static function tearDownAfterClass(): void { + self::$testApp->release(); + } + + /** + * Batch publishes messages for given list of channels + * RSC19 + * https://ably.com/docs/api/rest-api#batch-publish + * @throws \Ably\Exceptions\AblyRequestException + */ + public function testBatchPublishMultipleChannelsUsingPostRequest() { + + $payload = array( + "channels" => ["channel1", "channel2", "channel3", "channel4"], + "messages" => array( + "id" => "1", + "data" => "foo" + ) + ); + + $batchPublishPaginatedResult = self::$ably->request("POST","/messages", [], $payload); + $this->assertNotNull($batchPublishPaginatedResult); + $this->assertEquals(201, $batchPublishPaginatedResult->statusCode); + $this->assertTrue($batchPublishPaginatedResult->success); + $this->assertNull($batchPublishPaginatedResult->errorCode); + $this->assertNull($batchPublishPaginatedResult->errorMessage); + $this->assertTrue( $batchPublishPaginatedResult->isLast(), 'Expected not to be the last page' ); + + $this->assertEquals("application/x-msgpack", $batchPublishPaginatedResult->headers["Content-Type"]); + $this->assertCount(4, $batchPublishPaginatedResult->items); + foreach ($batchPublishPaginatedResult->items as $key=> $item) { + $this->assertEquals("channel".($key + 1), $item->channel); + $this->assertEquals(1, $item->messageId); + } + + foreach (["channel1", "channel2", "channel3", "channel4"] as $channelName) { + print $channelName; + $channel = self::$ably->channel($channelName); + $paginatedHistory = $channel->history(); + foreach ($paginatedHistory->items as $msg) { + $this->assertEquals("1", $msg->id); + $this->assertEquals("foo", $msg->data); + } + } + } +} From 7eafc1d9b5a4296dc823beacd407f6473efe3f04 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 10 Apr 2024 17:32:08 +0530 Subject: [PATCH 4/7] Updated PHP doc for batch publish --- README.md | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8e00340..a0fecfd 100644 --- a/README.md +++ b/README.md @@ -148,21 +148,15 @@ If you're using Laravel and want to support **realtime broadcasting and events** - 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 objectPayload = new - { - channels = new[] { "channel1", "channel2", "channel3", "channel4" }, - messages = new[] - { - new - { - name = "eventName", - data = "foo", - } - } - }; - var jsonPayload = JsonConvert.SerializeObject(objectPayload); - var paginatedResponse = await ablyRest.RequestV2(HttpMethod.Post, "/messages", null, jsonPayload, null); +```php + $payload = array( + "channels" => ["channel1", "channel2", "channel3", "channel4"], + "messages" => array( + "id" => "1", + "data" => "foo" + ) + ); + $batchPublishPaginatedResult = $client->request("POST", "/messages", [], $payload); ``` - See the [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints. From 641c808080e74ffbb0b09ea784310f845caa746e Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 10 Apr 2024 17:36:50 +0530 Subject: [PATCH 5/7] Fixed test assertion for json protocol --- tests/AblyRestRequestTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/AblyRestRequestTest.php b/tests/AblyRestRequestTest.php index 6ef57ab..753a92c 100644 --- a/tests/AblyRestRequestTest.php +++ b/tests/AblyRestRequestTest.php @@ -45,7 +45,11 @@ public function testBatchPublishMultipleChannelsUsingPostRequest() { $this->assertNull($batchPublishPaginatedResult->errorMessage); $this->assertTrue( $batchPublishPaginatedResult->isLast(), 'Expected not to be the last page' ); - $this->assertEquals("application/x-msgpack", $batchPublishPaginatedResult->headers["Content-Type"]); + if (self::$ably->options->useBinaryProtocol) { + $this->assertEquals("application/x-msgpack", $batchPublishPaginatedResult->headers["Content-Type"]); + } else { + $this->assertEquals("application/json", $batchPublishPaginatedResult->headers["Content-Type"]); + } $this->assertCount(4, $batchPublishPaginatedResult->items); foreach ($batchPublishPaginatedResult->items as $key=> $item) { $this->assertEquals("channel".($key + 1), $item->channel); @@ -53,7 +57,6 @@ public function testBatchPublishMultipleChannelsUsingPostRequest() { } foreach (["channel1", "channel2", "channel3", "channel4"] as $channelName) { - print $channelName; $channel = self::$ably->channel($channelName); $paginatedHistory = $channel->history(); foreach ($paginatedHistory->items as $msg) { From b37daab4f0621f912dc1fa8f9e603f3e40b14e66 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 10 Apr 2024 18:13:32 +0530 Subject: [PATCH 6/7] Added information regarding messagepack encoding under reade#batch publish section --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a0fecfd..935e487 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,8 @@ If you're using Laravel and want to support **realtime broadcasting and events** $batchPublishPaginatedResult = $client->request("POST", "/messages", [], $payload); ``` - See the [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints. +- Ably uses `msgpack` as a default encoding for messages. Read [encode using msgpack for better efficiency](https://faqs.ably.com/do-you-binary-encode-your-messages-for-greater-efficiency). +- If you want to send payload as `json`, please set `useBinaryProtocol` as `false` in `clientOptions`. ## Support, feedback and troubleshooting From be405704415943c7773aa767bbad2cda5d513d26 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 10 Apr 2024 19:29:28 +0530 Subject: [PATCH 7/7] Added serialization note for batch publish documentation --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 935e487..41baa65 100644 --- a/README.md +++ b/README.md @@ -149,13 +149,14 @@ If you're using Laravel and want to support **realtime broadcasting and events** - 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). ```php + // batch publish needs php array to be passed and serialization is handled based on useBinaryProtocol $payload = array( "channels" => ["channel1", "channel2", "channel3", "channel4"], "messages" => array( "id" => "1", "data" => "foo" ) - ); + ); $batchPublishPaginatedResult = $client->request("POST", "/messages", [], $payload); ``` - See the [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints.