From 2fe5924f3b29812bc26cbcfbc36990cf10d7ddbf Mon Sep 17 00:00:00 2001 From: SERGEY <80528523+AntistressStore@users.noreply.github.com> Date: Thu, 20 Jun 2024 07:46:47 +0300 Subject: [PATCH] use centrifugo v5 HTTP API format (#63) 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 --- .gitignore | 2 +- src/Client.php | 17 ++++++++++++----- tests/ClientTest.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 7fd0b81..f7cbd83 100644 --- a/.gitignore +++ b/.gitignore @@ -29,7 +29,6 @@ ## File-based project format: *.ipr *.iws - ## Plugin-specific files: # IntelliJ @@ -50,3 +49,4 @@ crashlytics-build.properties composer.lock vendor/ .phpunit.result.cache +.php-cs-fixer.cache diff --git a/src/Client.php b/src/Client.php index 274880b..ce27964 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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); @@ -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; } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 25b55f0..f2d5699 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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);