Skip to content

Commit

Permalink
use centrifugo v5 HTTP API format (#63)
Browse files Browse the repository at this point in the history
This PR migrates the library to use new API format introduced in Centrifugo v5 - https://centrifugal.dev/blog/2023/06/29/centrifugo-v5-released#new-http-api-format
  • Loading branch information
AntistressStore authored Jun 20, 2024
1 parent 59717fa commit 2fe5924
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
Expand All @@ -50,3 +49,4 @@ crashlytics-build.properties
composer.lock
vendor/
.phpunit.result.cache
.php-cs-fixer.cache
17 changes: 12 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ private function request($method, $params)
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('method' => $method, 'params' => $params)));
curl_setopt($ch, CURLOPT_POSTFIELDS, \json_encode($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders());
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_URL, $this->getUrl($method));
$data = curl_exec($ch);
$error = curl_error($ch);
$headers = curl_getinfo($ch);
Expand All @@ -450,11 +450,18 @@ private function request($method, $params)
return $data;
}

private function getUrl($method)
{
return $this->url.'/'.$method;
}

private function getHeaders()
{
return array(
$headers = [
'Content-Type: application/json',
'Authorization: apikey ' . $this->apikey,
);
'X-API-Key: '.$this->apikey
];

return $headers;
}
}
33 changes: 33 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@ public function testSetters()
$this->assertNotNull($this->client->setConnectTimeoutOption(1));
$this->assertNotNull($this->client->setTimeoutOption(1));
}

public function testGetUrl(){
$client = new \ReflectionClass($this->client);

$method = $client->getMethod('getUrl');

$method->setAccessible(true);

$this->assertEquals("http://localhost:8000/api/publish", $method->invokeArgs($this->client,['publish']));
}

public function testGetHeaders(){

$phpcent = new \phpcent\Client("http://localhost:8000/api");

$apiKey = '12w5ec80-1bd0-4c0x-a2e9-2c96501d2123';

$phpcent->setApiKey($apiKey);

$client = new \ReflectionClass($phpcent);

$method = $client->getMethod('getHeaders');

$method->setAccessible(true);

$this->assertEquals(
[
'Content-Type: application/json',
'X-API-Key: '.$apiKey
]
, $method->invoke($phpcent));
}

public function testPublish()
{
$this->client->setUseAssoc(true);
Expand Down

0 comments on commit 2fe5924

Please sign in to comment.