Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix batch publish #198

Merged
merged 7 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ 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).

```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);
sacOO7 marked this conversation as resolved.
Show resolved Hide resolved
```
- 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

Expand Down
4 changes: 2 additions & 2 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
68 changes: 68 additions & 0 deletions tests/AblyRestRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace tests;
use Ably\AblyRest;
require_once __DIR__ . '/factories/TestApp.php';

class AblyRestRequestTest extends \PHPUnit\Framework\TestCase {

protected static $testApp;
protected static $defaultOptions;
protected static $ably;

public static function setUpBeforeClass(): void {
self::$testApp = new TestApp();
self::$defaultOptions = self::$testApp->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' );

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);
$this->assertEquals(1, $item->messageId);
}

foreach (["channel1", "channel2", "channel3", "channel4"] as $channelName) {
$channel = self::$ably->channel($channelName);
$paginatedHistory = $channel->history();
foreach ($paginatedHistory->items as $msg) {
$this->assertEquals("1", $msg->id);
$this->assertEquals("foo", $msg->data);
}
}
}
}
Loading