From 8f7b5ce137d4a2c2b6f87bfd140ea1072469e0e4 Mon Sep 17 00:00:00 2001 From: Alexander Korelskiy Date: Tue, 6 Apr 2021 15:37:31 +0300 Subject: [PATCH 1/2] Update json rpc client, update generator to generate typed arguments and result for functions, fix tests. --- README.md | 33 ++- composer.json | 5 +- generator/JsonRpcClientGenerator.php | 168 ++++++++++----- src/EazyJsonRpc/BaseJsonRpcCall.php | 131 ++++++------ src/EazyJsonRpc/BaseJsonRpcClient.php | 135 ++++++++++-- src/EazyJsonRpc/BaseJsonRpcException.php | 41 ++++ src/EazyJsonRpc/HttpException.php | 7 + tests/DatePingServiceClientTest.php | 50 ++--- tests/DateTimeRpcServiceClientTest.php | 7 +- tests/DateTimeServiceClientTest.php | 46 ++-- tests/DateTimeServiceTest.php | 2 + tests/JsonRpcClient/DatePingServiceClient.php | 199 ++++++++++++++++++ tests/JsonRpcClient/DateTimeServiceClient.php | 106 ++++++++++ tests/README.md | 4 +- tests/bootstrap.php | 4 +- tests/lib/DatePingServiceClient.php | 77 ------- tests/lib/DateTimeRpcService.php | 4 +- tests/lib/DateTimeService.php | 3 +- tests/lib/DateTimeServiceClient.php | 66 ------ 19 files changed, 732 insertions(+), 356 deletions(-) create mode 100644 src/EazyJsonRpc/BaseJsonRpcException.php create mode 100644 src/EazyJsonRpc/HttpException.php create mode 100644 tests/JsonRpcClient/DatePingServiceClient.php create mode 100644 tests/JsonRpcClient/DateTimeServiceClient.php delete mode 100644 tests/lib/DatePingServiceClient.php delete mode 100644 tests/lib/DateTimeServiceClient.php diff --git a/README.md b/README.md index 8413ca8..3430804 100644 --- a/README.md +++ b/README.md @@ -11,21 +11,44 @@ SMD Schema available via /server.php?smd __Public Namespace__ * Inherits your exposed class from BaseJsonRpcServer or create `new BaseJsonRpcServer( $instance );` -* `$server->execute();` +* `$server->Execute();` __Multiple Namespaces__ * Create `new BaseJsonRpcServer();` * Call `$server->RegisterInstance( $instance, $namespace )` as many times as you need -* `$server->execute();` +* `$server->Execute();` Client ------ -* Generate Client from SMD Schema from generator/ `php JsonRpcClientGenerator ` -* Create client instance `$client = ::GetInstance();` or `$client = new ( );` -* Use it `$result = $client->Method()`; :) +* Generate Client from SMD Schema from generator/ `php JsonRpcClientGenerator.php ` +* Use it: +``` +$client = ::GetInstance(); + +try { +$result = $client->Method(); +} catch (BaseJsonRpcException $e) { + // work with exception +} +``` + +Client with typed returns by rpcgen +------ + +* Generate Client from SMD Schema with [rpcgen](https://github.com/vmkteam/rpcgen) and save it to `RpcClient.php` +* Use it: +``` +$client = RpcClient::GetInstance(); + +try { +$result = $client->Method(); +} catch (BaseJsonRpcException $e) { + // work with exception +} +``` Doc ------ diff --git a/composer.json b/composer.json index 8ba9dea..b4583d3 100644 --- a/composer.json +++ b/composer.json @@ -29,9 +29,10 @@ }, "target-dir":"", "require":{ - "php":">=5.4.0", + "php":">=7.2.0", "ext-curl": "*", - "ext-json": "*" + "ext-json": "*", + "netresearch/jsonmapper": "^4.0" }, "require-dev":{ "phpunit/phpunit": "4.8.*" diff --git a/generator/JsonRpcClientGenerator.php b/generator/JsonRpcClientGenerator.php index c660396..bc98836 100644 --- a/generator/JsonRpcClientGenerator.php +++ b/generator/JsonRpcClientGenerator.php @@ -39,7 +39,7 @@ class JsonRpcClientGenerator { * @param array $smd SMD Schema * @param string $className */ - public function __construct( $url, $smd, $className ) { + public function __construct( string $url, array $smd, string $className ) { $this->url = $url; $this->smd = $smd; $this->className = $className; @@ -56,12 +56,22 @@ private function getHeader() { $date = date( 'd.m.Y G:i' ); $result = <<className} extends \EazyJsonRpc\BaseJsonRpcClient { + class {$this->className} extends BaseJsonRpcClient { php; return $result; @@ -73,85 +83,105 @@ class {$this->className} extends \EazyJsonRpc\BaseJsonRpcClient { * @param $methodData * @return string */ - public function getMethod( $methodName, $methodData ) { - $newDocLine = PHP_EOL . str_repeat( ' ', 9 ) . '*'; - $description = !empty( $methodData['description'] ) ? $methodData['description'] : $methodName; + public function getMethod( $methodName, $methodData ): string { + $newDocLine = PHP_EOL . str_repeat( ' ', 8 ) . '*'; + $description = sprintf( '<%s> RPC method', $methodName ); + $description .= !empty( $methodData['description'] ) ? $newDocLine . ' ' . trim( $methodData['description'] ) : ''; + $description = str_replace( "\n", PHP_EOL, $description ); $strDocParams = ''; - $strParamsArr = [ ]; - $callParamsArr = [ ]; + $strParamsArr = []; + $callParamsArr = []; $methodName = str_replace( '.', '_', $methodName ); - // Add Default Parameter = IsNotification - $methodData['parameters'][] = [ - 'name' => 'isNotification', - 'optional' => 'true', - 'type' => 'bool', - 'default' => false, - 'description' => 'set to true if call is notification', - ]; - // params if ( !empty( $methodData['parameters'] ) ) { // Set Doc Params foreach ( $methodData['parameters'] as $param ) { - $name = $param['name']; - $strParam = '$' . $name; - $strDocParamsArr = [ $newDocLine ]; - $strDocParamsArr[] = '@param'; - + $name = $param['name']; + $strDocParam = $newDocLine; + $strDocParam .= " @param"; if ( !empty( $param['type'] ) ) { - $strDocParamsArr[] = $param['type']; + $strDocParam .= " " . $this->getPhpType( $param['type'] ); + } + $strParam = $this->getPhpType( $param['type'] ) . ' $' . $name; + $optionalParam = !empty( $param['optional'] ); + if ( $optionalParam ) { + $strDocParam .= '|null'; } - $strDocParamsArr[] = '$' . $name; - if ( !empty( $param['optional'] ) ) { - $strDocParamsArr[] = '[optional]'; + $strDocParam .= ' $' . $name; + if ( $optionalParam ) { + $strDocParam .= ' [optional]'; } if ( !empty( $param['description'] ) ) { - $strDocParamsArr[] = $param['description']; + $strDocParam .= " " . $param['description']; } if ( array_key_exists( 'default', $param ) ) { $strParam .= sprintf( ' = %s', var_export( $param['default'], true ) ); + } else { + if ( $optionalParam ) { + $strParam .= ' = null'; + } } - $strDocParams .= rtrim( implode( ' ', $strDocParamsArr ) ); + $strDocParams .= rtrim( $strDocParam ); $strParamsArr[] = $strParam; $callParamsArr[$name] = sprintf( "'%s' => $%s", $name, $name ); } } - - $strParams = ' ' . trim( - str_replace( - [ "\n", ',)', 'array (' ], - [ '', ')', 'array(' ], - implode( ', ', $strParamsArr ) - ), ', ' ) . ' '; - - unset( $callParamsArr['isNotification'] ); - + $strDocParams .= $newDocLine . ' @param bool $isNotification [optional] set to true if call is notification'; + + $strParams = str_replace( + [ "\n", ',)', 'array (' ], + [ '', ')', 'array(' ], + implode( ', ', $strParamsArr ) + ); + + $strParams .= ', $isNotification = false '; + $strParams = ' ' . trim( $strParams, ', ' ) . ' '; + $returnType = ''; + $optionalReturn = ''; + $strDocReturns = $newDocLine . ' @return mixed'; + $strReturnType = ''; // returns - if ( !empty( $methodData['returns'] ) && !empty( $methodData['returns']['type'] ) && is_string( $methodData['returns']['type'] ) ) { - $strDocParams .= $newDocLine . ' @return \EazyJsonRpc\BaseJsonRpcCall (result: ' . $methodData['returns']['type'] . ')'; + if ( !empty( $methodData['returns'] ) ) { + $strDocReturns = ''; + if ( !empty( $methodData['returns']['type'] ) && is_string( $methodData['returns']['type'] ) ) { + $returnType = $this->getPhpType( $methodData['returns']['type'] ); + $strDocReturns .= $newDocLine . ' @return ' . $returnType; + } + if ( !empty( $methodData['returns']['optional'] ) ) { + $optionalReturn = '?'; + $strDocReturns .= '|null'; + } + if ( !empty( $methodData['returns']['description'] ) ) { + $strDocReturns .= ' ' . $methodData['returns']['description']; + } + if ( $returnType != 'mixed' ) { + $strReturnType = sprintf( ': %s%s', $optionalReturn, $returnType ); + } } - + $strDocParams .= $strDocReturns; $callParamsStr = implode( ', ', $callParamsArr ); if ( !empty( $callParamsStr ) ) { $callParamsStr = sprintf( ' %s ', $callParamsStr ); } - $result = <<call( __FUNCTION__, array({$callParamsStr}), \$this->getRequestId( \$isNotification ) ); + * {$description}{$strDocParams} + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function {$methodName}({$strParams})$strReturnType { + return \$this->call( __FUNCTION__, '$returnType', [{$callParamsStr}], \$this->getRequestId( \$isNotification ) ); } php; - return $result; } @@ -159,35 +189,57 @@ public function {$methodName}({$strParams}) { /** * Get Footer */ - private function getFooter() { - $url = $this->url; - $urlInfo = parse_url( $url ); + private function getFooter(): string { + $rpcUrl = $this->url; + $urlInfo = parse_url( $rpcUrl ); if ( !empty( $urlInfo ) ) { - $url = sprintf( '%s://%s%s', $urlInfo['scheme'], $urlInfo['host'], $this->smd['target'] ); + $rpcUrl = sprintf( '%s://%s%s', $urlInfo['scheme'], $urlInfo['host'], $this->smd['target'] ); } - $result = <<className} */ - public static function GetInstance() { - return new self( '{$url}' ); + public static function GetInstance( string \$url ): {$this->className} { + return new self( \$url ); } } php; + } - return $result; + + /** + * Return PHP type from SMD type + * @param string $smdType + * @return string + */ + private function getPhpType( string $smdType ): string { + switch ( $smdType ) { + case "string": + return "string"; + case "object": + case "array": + return "array"; + case "boolean": + return "bool"; + case "float": + return "float"; + case "integer": + return "int"; + } + return "mixed"; } /** * Save to File */ - public function Generate() { + public function Generate(): string { $this->result = $this->getHeader(); foreach ( $this->smd['services'] as $methodName => $methodData ) { @@ -205,7 +257,7 @@ public function Generate() { * Save To File * @return int */ - public function SaveToFile() { + public function SaveToFile(): int { return file_put_contents( $this->className . '.php', $this->Generate() ); } } diff --git a/src/EazyJsonRpc/BaseJsonRpcCall.php b/src/EazyJsonRpc/BaseJsonRpcCall.php index e81d6eb..4aaa9d4 100644 --- a/src/EazyJsonRpc/BaseJsonRpcCall.php +++ b/src/EazyJsonRpc/BaseJsonRpcCall.php @@ -1,85 +1,80 @@ Error; - } + /** + * Has Error + * @return bool + */ + public function HasError(): bool { + return (bool) $this->Error; + } - /** - * @param string $method - * @param array $params - * @param string $id - */ - public function __construct($method, $params, $id) - { - $this->Method = $method; - $this->Params = $params; - $this->Id = $id; - } + /** + * @param string $method + * @param array $params + * @param string $id + */ + public function __construct( $method, $params, $id ) { + $this->Method = $method; + $this->Params = $params; + $this->Id = $id; + } - /** - * Get Call Data - * @param BaseJsonRpcCall $call - * @return array - */ - public static function GetCallData(BaseJsonRpcCall $call) - { - return [ - 'jsonrpc' => '2.0', - 'id' => $call->Id, - 'method' => $call->Method, - 'params' => $call->Params, - ]; - } + /** + * Get Call Data + * @param BaseJsonRpcCall $call + * @return array + */ + public static function GetCallData( BaseJsonRpcCall $call ): array { + return [ + 'jsonrpc' => '2.0', + 'id' => $call->Id, + 'method' => $call->Method, + 'params' => $call->Params, + ]; + } - /** - * Set Result - * @param mixed $data - * @param bool $useObjects - */ - public function SetResult($data, $useObjects = false) - { - if ($useObjects) { - $this->Error = property_exists($data, 'error') ? $data->error : null; - $this->Result = property_exists($data, 'result') ? $data->result : null; - } else { - $this->Error = !empty($data['error']) ? $data['error'] : null; - $this->Result = !empty($data['result']) ? $data['result'] : null; + /** + * Set Result + * @param mixed $data + * @param bool $useObjects + */ + public function SetResult( $data, $useObjects = false ) { + if ( $useObjects ) { + $this->Error = property_exists( $data, 'error' ) ? $data->error : null; + $this->Result = property_exists( $data, 'result' ) ? $data->result : null; + } else { + $this->Error = !empty( $data['error'] ) ? $data['error'] : null; + $this->Result = !empty( $data['result'] ) ? $data['result'] : null; + } } - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/src/EazyJsonRpc/BaseJsonRpcClient.php b/src/EazyJsonRpc/BaseJsonRpcClient.php index 75381d0..31a88ab 100644 --- a/src/EazyJsonRpc/BaseJsonRpcClient.php +++ b/src/EazyJsonRpc/BaseJsonRpcClient.php @@ -2,6 +2,8 @@ namespace EazyJsonRpc; + use JsonMapper; + /** * Base JSON-RPC 2.0 Client * @package Eaze @@ -24,7 +26,7 @@ class BaseJsonRpcClient { public $CurlOptions = [ CURLOPT_POST => 1, CURLOPT_RETURNTRANSFER => 1, - CURLOPT_HTTPHEADER => [ 'Content-Type' => 'application/json' ], + CURLOPT_HTTPHEADER => [ 'Content-Type: application/json' ], ]; /** @@ -43,21 +45,20 @@ class BaseJsonRpcClient { * Batch Calls * @var BaseJsonRpcCall[] */ - private $batchCalls = [ ]; + private $batchCalls = []; /** * Batch Notifications * @var BaseJsonRpcCall[] */ - private $batchNotifications = [ ]; + private $batchNotifications = []; /** * Create New JsonRpc client * @param string $serverUrl - * @return BaseJsonRpcClient */ - public function __construct( $serverUrl ) { + public function __construct( string $serverUrl ) { $this->CurlOptions[CURLOPT_URL] = $serverUrl; } @@ -65,9 +66,9 @@ public function __construct( $serverUrl ) { /** * Get Next Request Id * @param bool $isNotification - * @return int + * @return int|null */ - protected function getRequestId( $isNotification = false ) { + protected function getRequestId( $isNotification = false ): ?int { return $isNotification ? null : $this->id++; } @@ -76,10 +77,10 @@ protected function getRequestId( $isNotification = false ) { * Begin Batch Call * @return bool */ - public function BeginBatch() { + public function BeginBatch(): bool { if ( !$this->isBatchCall ) { - $this->batchNotifications = [ ]; - $this->batchCalls = [ ]; + $this->batchNotifications = []; + $this->batchCalls = []; $this->isBatchCall = true; return true; } @@ -90,14 +91,23 @@ public function BeginBatch() { /** * Commit Batch + * @return array */ - public function CommitBatch() { - $result = false; + public function CommitBatch(): array { if ( !$this->isBatchCall || ( !$this->batchCalls && !$this->batchNotifications ) ) { - return $result; + return []; } - $result = $this->processCalls( array_merge( $this->batchCalls, $this->batchNotifications ) ); + $this->processCalls( array_merge( $this->batchCalls, $this->batchNotifications ) ); + + $result = []; + foreach ( $this->batchCalls as $i => $call ) { + if ( $call->HasError() ) { + $result[] = new BaseJsonRpcException( $call ); + } else { + $result[] = $call->Result; + } + } $this->RollbackBatch(); return $result; @@ -108,9 +118,9 @@ public function CommitBatch() { * Rollback Calls * @return bool */ - public function RollbackBatch() { + public function RollbackBatch(): bool { $this->isBatchCall = false; - $this->batchCalls = [ ]; + $this->batchCalls = []; return true; } @@ -120,10 +130,13 @@ public function RollbackBatch() { * Process Call * @param string $method * @param array $parameters - * @param int $id + * @param null $id + * @param string $returnType * @return mixed + * @throws BaseJsonRpcException + * @throws \JsonMapper_Exception */ - protected function call( $method, array $parameters = [ ], $id = null ) { + protected function call( string $method, string $returnType, array $parameters = [], $id = null ) { $method = str_replace( '_', '.', $method ); $call = new BaseJsonRpcCall( $method, $parameters, $id ); if ( $this->isBatchCall ) { @@ -136,7 +149,11 @@ protected function call( $method, array $parameters = [ ], $id = null ) { $this->processCalls( [ $call ] ); } - return $call; + if ( $call->HasError() ) { + throw new BaseJsonRpcException( $call ); + } + + return $this->convertResult( $call, $returnType ); } @@ -145,9 +162,11 @@ protected function call( $method, array $parameters = [ ], $id = null ) { * @param string $method * @param array $parameters * @return BaseJsonRpcCall + * @throws BaseJsonRpcException + * @throws \JsonMapper_Exception */ - public function __call( $method, array $parameters = [ ] ) { - return $this->call( $method, $parameters, $this->getRequestId() ); + public function __call( string $method, array $parameters = [] ) { + return $this->call( $method, '', $parameters, $this->getRequestId() ); } @@ -155,11 +174,12 @@ public function __call( $method, array $parameters = [ ] ) { * Process Calls * @param BaseJsonRpcCall[] $calls * @return mixed + * @throws HttpException */ - protected function processCalls( $calls ) { + protected function processCalls( array $calls ): bool { // Prepare Data $singleCall = !$this->isBatchCall ? reset( $calls ) : null; - $result = $this->batchCalls ? array_values( array_map( '\EazyJsonRpc\BaseJsonRpcCall', $calls ) ) : BaseJsonRpcCall::GetCallData( $singleCall ); + $result = $this->batchCalls ? array_values( array_map( '\EazyJsonRpc\BaseJsonRpcCall::GetCallData', $calls ) ) : BaseJsonRpcCall::GetCallData( $singleCall ); // Send Curl Request $options = $this->CurlOptions + [ CURLOPT_POSTFIELDS => json_encode( $result ) ]; @@ -167,6 +187,16 @@ protected function processCalls( $calls ) { curl_setopt_array( $ch, $options ); $data = curl_exec( $ch ); + + if ( curl_errno( $ch ) ) { + throw new HttpException( 'Error with curl response: ' . curl_error( $ch ) ); + } + + $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); + if ( $httpCode != 200 ) { + throw new HttpException( 'Error with http response, got http status: ' . $httpCode ); + } + $data = json_decode( $data, !$this->UseObjectsInResults ); curl_close( $ch ); if ( $data === null ) { @@ -187,5 +217,64 @@ protected function processCalls( $calls ) { return true; } + + + /** + * Convert Result to concrete type + * @param BaseJsonRpcCall $call + * @param string $returnType + * @return array|bool|float|int|mixed|object|string + * @throws \JsonMapper_Exception + */ + private function convertResult( BaseJsonRpcCall $call, string $returnType ) { + $result = null; + $mapper = new JsonMapper(); + $mapper->bEnforceMapType = false; + switch ( true ) { + case substr( $returnType, -2 ) == '[]': + $result = $mapper->mapArray( $call->Result, [], rtrim( $returnType, '[]' ) ); + break; + case $returnType == 'mixed': + case $returnType == 'array': + $result = []; + if ( $call->Result ) { + $result = $call->Result; + } + break; + case $returnType == 'object': + $result = (object) []; + if ( $call->Result ) { + $result = $call->Result; + } + break; + case $returnType == 'int': + $result = 0; + if ( $call->Result ) { + $result = (int) $call->Result; + } + break; + case $returnType == 'float': + $result = 0; + if ( $call->Result ) { + $result = (float) $call->Result; + } + break; + case $returnType == 'bool': + $result = false; + if ( $call->Result ) { + $result = (bool) $call->Result; + } + break; + case $returnType == 'string': + $result = ''; + if ( $call->Result ) { + $result = (string) $call->Result; + } + break; + default: + $result = $mapper->map( $call->Result, new $returnType ); + } + return $result; + } } diff --git a/src/EazyJsonRpc/BaseJsonRpcException.php b/src/EazyJsonRpc/BaseJsonRpcException.php new file mode 100644 index 0000000..57b41ab --- /dev/null +++ b/src/EazyJsonRpc/BaseJsonRpcException.php @@ -0,0 +1,41 @@ +HasError() ) { + return; + } + $error = (array) $baseJsonRpcCall->Error; + + if ( isset( $error['data'] ) ) { + $this->data = $error['data']; + } + + $message = ''; + $code = 0; + if ( isset( $error['message'] ) ) { + $message = $error['message']; + } + + if ( isset( $error['code'] ) ) { + $code = $error['code']; + } + + parent::__construct( $message, $code, $previous ); + } + + + /** + * @return mixed + */ + public function getData() { + return $this->data; + } + + } diff --git a/src/EazyJsonRpc/HttpException.php b/src/EazyJsonRpc/HttpException.php new file mode 100644 index 0000000..08ac564 --- /dev/null +++ b/src/EazyJsonRpc/HttpException.php @@ -0,0 +1,7 @@ +object = new DatePingServiceClient( $this->url ); + $this->object = DatePingServiceClient::GetInstance( $this->url ); } public function testGetTime() { $response = $this->object->date_GetTime(); - static::assertEmpty( $response->Error ); - static::assertNotEmpty( $response->Result ); + static::assertNotEmpty( $response ); $response = $this->object->date_GetTime( 'UTC', 'd.m.Y' ); - static::assertAttributeEquals( date( 'd.m.Y' ), 'Result', $response ); + static::assertEquals( date( 'd.m.Y' ), $response ); } public function testPing() { $response = $this->object->ping_Ping(); - static::assertEquals( 'pong', $response->Result ); + static::assertEquals( 'pong', $response ); } public function testGetTimeZones() { $response = $this->object->date_GetTimeZones(); - static::assertEquals( getCachedTimeZones(), $response->Result ); + static::assertEquals( getCachedTimeZones(), $response ); } public function testGetRelativeTimeError() { - $response = $this->object->date_GetRelativeTime( '-0000-00-00', '1' ); - static::assertEmpty( $response->Result ); - static::assertNotEmpty( $response->Error ); - static::assertTrue( $response->HasError() ); + try { + $response = $this->object->date_GetRelativeTime( '-0000-00-00', '1' ); + static::assertEmpty( $response ); + } catch ( \EazyJsonRpc\BaseJsonRpcException $e ) { + static::assertNotEmpty( $e ); + } } @@ -53,20 +56,19 @@ public function testBatchCalls() { static::assertTrue( $this->object->BeginBatch() ); static::assertFalse( $this->object->BeginBatch() ); - $r1 = $this->object->date_GetRelativeTime( 'now' ); - $r2 = $this->object->date_GetRelativeTime( 'yesterday' ); - $r3 = $this->object->date_GetRelativeTime( 'yesterday', 'UTC', 'c', true ); - $r4 = $this->object->date_GetRelativeTime( 'yesterday' ); - $r5 = $this->object->ping_Ping( 'test' ); - - static::assertEmpty( $r2->Result ); + $this->object->date_GetRelativeTime( 'now' ); + $this->object->date_GetRelativeTime( 'yesterday' ); + $this->object->date_GetRelativeTime( 'yesterday', 'UTC', 'c', true ); + $this->object->date_GetRelativeTime( 'yesterday' ); + $this->object->ping_Ping( 'test' ); - static::assertTrue( $this->object->CommitBatch() ); - static::assertFalse( $this->object->CommitBatch() ); + $res = $this->object->CommitBatch(); + static::assertEmpty( $this->object->CommitBatch() ); + static::assertCount(4, $res); - static::assertNotEmpty( $r2->Result ); - static::assertEmpty( $r3->Result ); - static::assertEquals( $r4->Result, $r2->Result ); - static::assertEquals( $r5->Result, 'test' ); + list($r1, $r2, $r3, $r4) = $res; + static::assertNotEmpty( $r1 ); + static::assertEquals( $r2, $r3 ); + static::assertEquals( $r4, 'test' ); } } \ No newline at end of file diff --git a/tests/DateTimeRpcServiceClientTest.php b/tests/DateTimeRpcServiceClientTest.php index dd25c66..8f5a539 100644 --- a/tests/DateTimeRpcServiceClientTest.php +++ b/tests/DateTimeRpcServiceClientTest.php @@ -7,7 +7,7 @@ */ class DateTimeRpcServiceClientTest extends DateTimeServiceClientTest { - protected $url = 'http://eazyjsonrpc/tests/example-server.php?v2'; + protected $url = 'http://localhost:8000/tests/example-server.php?v2'; public function setUp() { @@ -15,10 +15,9 @@ public function setUp() { $this->object->UseObjectsInResults = true; } - public function testGetTimeZones() { $response = $this->object->GetTimeZones(); - static::assertCount( count(getCachedTimeZones()), $response->Result ); - static::assertInternalType( 'array', $response->Result ); + static::assertCount( count(getCachedTimeZones()), $response ); + static::assertInternalType( 'array', $response ); } } \ No newline at end of file diff --git a/tests/DateTimeServiceClientTest.php b/tests/DateTimeServiceClientTest.php index 4dd1671..92018b6 100644 --- a/tests/DateTimeServiceClientTest.php +++ b/tests/DateTimeServiceClientTest.php @@ -1,5 +1,8 @@ object = new DateTimeServiceClient( $this->url ); + $this->object = DateTimeServiceClient::GetInstance( $this->url ); } public function testGetTime() { $response = $this->object->GetTime(); - static::assertEmpty( $response->Error ); - static::assertNotEmpty( $response->Result ); + static::assertNotEmpty( $response ); $response = $this->object->GetTime( 'UTC', 'd.m.Y' ); - static::assertAttributeEquals( date( 'd.m.Y' ), 'Result', $response ); + static::assertEquals( date( 'd.m.Y' ), $response ); } public function testGetTimeZones() { $response = $this->object->GetTimeZones(); - static::assertEquals( getCachedTimeZones(), $response->Result ); + static::assertEquals( getCachedTimeZones(), $response ); } public function testGetRelativeTimeError() { - $response = $this->object->GetRelativeTime( '-0000-00-00', '1' ); - static::assertEmpty( $response->Result ); - static::assertNotEmpty( $response->Error ); - static::assertTrue( $response->HasError() ); - + try { + $response = $this->object->GetRelativeTime( '-0000-00-00', '1' ); + static::assertEmpty( $response ); + } catch ( BaseJsonRpcException $e ) { + static::assertNotEmpty( $e ); + } } @@ -47,18 +50,15 @@ public function testBatchCalls() { static::assertTrue( $this->object->BeginBatch() ); static::assertFalse( $this->object->BeginBatch() ); - $r1 = $this->object->GetRelativeTime( 'now' ); - $r2 = $this->object->GetRelativeTime( 'yesterday' ); - $r3 = $this->object->GetRelativeTime( 'yesterday', 'UTC', 'c', true ); - $r4 = $this->object->GetRelativeTime( 'yesterday' ); - - static::assertEmpty( $r2->Result ); - - static::assertTrue( $this->object->CommitBatch() ); - static::assertFalse( $this->object->CommitBatch() ); + $this->object->GetRelativeTime( 'now' ); + $this->object->GetRelativeTime( 'exception' ); + $this->object->GetRelativeTime( 'yesterday' ); + $this->object->GetRelativeTime( 'yesterday' ); - static::assertNotEmpty( $r2->Result ); - static::assertEmpty( $r3->Result ); - static::assertEquals( $r4->Result, $r2->Result ); + list ( $r1, $r2, $r3, $r4 ) = $this->object->CommitBatch(); + static::assertNotEmpty( $r1 ); + static::assertEmpty( $this->object->CommitBatch() ); + static::assertInstanceOf( BaseJsonRpcException::class, $r2 ); + static::assertEquals( $r3, $r4 ); } } \ No newline at end of file diff --git a/tests/DateTimeServiceTest.php b/tests/DateTimeServiceTest.php index 7d34dba..ce5b2c2 100644 --- a/tests/DateTimeServiceTest.php +++ b/tests/DateTimeServiceTest.php @@ -1,4 +1,6 @@ RPC method + * Get Current Time + * @param string|null $timezone [optional] + * @param string|null $format [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return string + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function date_GetTime( string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string { + return $this->call( __FUNCTION__, 'string', [ 'timezone' => $timezone, 'format' => $format ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Returns associative array containing dst, offset and the timezone name + * @param bool $isNotification [optional] set to true if call is notification + * @return array + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function date_GetTimeZones( $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Get Relative time + * @param string $text a date/time string + * @param string|null $timezone [optional] + * @param string|null $format [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return string + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function date_GetRelativeTime( string $text, string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string { + return $this->call( __FUNCTION__, 'string', [ 'text' => $text, 'timezone' => $timezone, 'format' => $format ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Implode Function + * @param string $glue + * @param array|null $pieces [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return string string + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function date_Implode( string $glue, array $pieces = array( 0 => '1', 1 => '2', 2 => '3'), $isNotification = false ): string { + return $this->call( __FUNCTION__, 'string', [ 'glue' => $glue, 'pieces' => $pieces ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Get Ping Result + * @param string|null $message [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return string pong + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function ping_Ping( string $message = 'pong', $isNotification = false ): string { + return $this->call( __FUNCTION__, 'string', [ 'message' => $message ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Get News List + * @param bool $isNotification [optional] set to true if call is notification + * @return array News + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function news_GetList( $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Get News By Id + * @param int $id + * @param bool $isNotification [optional] set to true if call is notification + * @return array News + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function news_GetById( int $id, $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [ 'id' => $id ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Get Tags + * @param bool $isNotification [optional] set to true if call is notification + * @return array Category Tag + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function news_GetTags( $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Get Categories + * @param bool $isNotification [optional] set to true if call is notification + * @return array + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function news_Categories( $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Search News + * @param array $s NewsSearch + * @param int $page + * @param int|null $count [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return array News + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function news_Search( array $s, int $page, int $count = 50, $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [ 's' => $s, 'page' => $page, 'count' => $count ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Name Value + * @param array $nv NameValue Parameter + * @param int|null $c [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return array + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function news_NameValue( array $nv, int $c = null, $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [ 'nv' => $nv, 'c' => $c ], $this->getRequestId( $isNotification ) ); + } + + + /** + * Get Instance + * @param $url string + * @return DatePingServiceClient + */ + public static function GetInstance( string $url = 'http://localhost/tests/example-server.php' ): DatePingServiceClient { + return new self( $url ); + } + + } \ No newline at end of file diff --git a/tests/JsonRpcClient/DateTimeServiceClient.php b/tests/JsonRpcClient/DateTimeServiceClient.php new file mode 100644 index 0000000..da6c46d --- /dev/null +++ b/tests/JsonRpcClient/DateTimeServiceClient.php @@ -0,0 +1,106 @@ + RPC method + * Get Current Time + * @param string|null $timezone [optional] + * @param string|null $format [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return string + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function GetTime( string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string { + return $this->call( __FUNCTION__, 'string', [ 'timezone' => $timezone, 'format' => $format ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Returns associative array containing dst, offset and the timezone name + * @param bool $isNotification [optional] set to true if call is notification + * @return array + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function GetTimeZones( $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Get Relative time + * @param string $text a date/time string + * @param string|null $timezone [optional] + * @param string|null $format [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return string + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function GetRelativeTime( string $text, string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string { + return $this->call( __FUNCTION__, 'string', [ 'text' => $text, 'timezone' => $timezone, 'format' => $format ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * Implode Function + * @param string $glue + * @param array|null $pieces [optional] + * @param bool $isNotification [optional] set to true if call is notification + * @return string string + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function Implode( string $glue, array $pieces = array( 0 => '1', 1 => '2', 2 => '3'), $isNotification = false ): string { + return $this->call( __FUNCTION__, 'string', [ 'glue' => $glue, 'pieces' => $pieces ], $this->getRequestId( $isNotification ) ); + } + + + /** + * RPC method + * ComplexResult Function + * @param bool $isNotification [optional] set to true if call is notification + * @return array + * @throws BaseJsonRpcException + * @throws HttpException + * @throws JsonMapper_Exception + */ + public function ComplexResult( $isNotification = false ): array { + return $this->call( __FUNCTION__, 'array', [], $this->getRequestId( $isNotification ) ); + } + + + /** + * Get Instance + * @param $url string + * @return DateTimeServiceClient + */ + public static function GetInstance( string $url = 'http://localhost/tests/example-server.php' ): DateTimeServiceClient { + return new self( $url ); + } + + } \ No newline at end of file diff --git a/tests/README.md b/tests/README.md index 7c4cd14..13333a7 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,7 +1,7 @@ eazy-jsonrpc tests ============ -To pass all tests you should create vhost http://eazyjsonrpc/ to eazy-jsonrpc folder +To pass all tests just run `php -S localhost:8000` in eazy-jsonrpc folder -Tests are using http://eazyjsonrpc/tests/example-server.php url. +Tests are using `http://localhost:8000/tests/example-server.php` url. diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6e8fc6c..19176d3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,9 +2,9 @@ include '../src/EazyJsonRpc/BaseJsonRpcServer.php'; include '../src/EazyJsonRpc/BaseJsonRpcServerSmd.php'; include '../src/EazyJsonRpc/BaseJsonRpcClient.php'; + include 'JsonRpcClient/DateTimeServiceClient.php'; + include 'JsonRpcClient/DatePingServiceClient.php'; include 'lib/DateTimeService.php'; - include 'lib/DateTimeServiceClient.php'; - include 'lib/DatePingServiceClient.php'; include 'lib/DateTimeRpcService.php'; /** diff --git a/tests/lib/DatePingServiceClient.php b/tests/lib/DatePingServiceClient.php deleted file mode 100644 index 9049f71..0000000 --- a/tests/lib/DatePingServiceClient.php +++ /dev/null @@ -1,77 +0,0 @@ -call( __FUNCTION__, array( 'timezone' => $timezone, 'format' => $format ), $this->getRequestId( $isNotification ) ); - } - - - /** - * Returns associative array containing dst, offset and the timezone name - * @param bool $isNotification [optional] set to true if call is notification - * @return \EazyJsonRpc\BaseJsonRpcCall (result: array) - */ - public function date_GetTimeZones( $isNotification = false ) { - return $this->call( __FUNCTION__, array(), $this->getRequestId( $isNotification ) ); - } - - - /** - * Get Relative time - * @param string $text a date/time string - * @param string $timezone [optional] - * @param string $format [optional] - * @param bool $isNotification [optional] set to true if call is notification - * @return \EazyJsonRpc\BaseJsonRpcCall (result: string) - */ - public function date_GetRelativeTime( $text, $timezone = 'UTC', $format = 'c', $isNotification = false ) { - return $this->call( __FUNCTION__, array( 'text' => $text, 'timezone' => $timezone, 'format' => $format ), $this->getRequestId( $isNotification ) ); - } - - - /** - * Implode Function - * @param string $glue - * @param array $pieces [optional] - * @param bool $isNotification [optional] set to true if call is notification - * @return \EazyJsonRpc\BaseJsonRpcCall (result: string) - */ - public function date_Implode( $glue, $pieces = array( 0 => '1', 1 => '2', 2 => '3'), $isNotification = false ) { - return $this->call( __FUNCTION__, array( 'glue' => $glue, 'pieces' => $pieces ), $this->getRequestId( $isNotification ) ); - } - - - /** - * Get Ping Result - * @param string $message [optional] - * @param bool $isNotification [optional] set to true if call is notification - * @return \EazyJsonRpc\BaseJsonRpcCall (result: string) - */ - public function ping_Ping( $message = 'pong', $isNotification = false ) { - return $this->call( __FUNCTION__, array( 'message' => $message ), $this->getRequestId( $isNotification ) ); - } - - - /** - * Get Instance - * @return DatePingServiceClient - */ - public static function GetInstance() { - return new self( 'http://eazyjsonrpc/tests/example-server.php' ); - } - - } \ No newline at end of file diff --git a/tests/lib/DateTimeRpcService.php b/tests/lib/DateTimeRpcService.php index dfd11d1..9f94c87 100644 --- a/tests/lib/DateTimeRpcService.php +++ b/tests/lib/DateTimeRpcService.php @@ -12,6 +12,7 @@ class DateTimeRpcService extends BaseJsonRpcServer { * @param string $timezone * @param string $format * @return string + * @throws Exception */ public function GetTime( $timezone = 'UTC', $format = 'c' ) { $result = new DateTime( 'now', new DateTimeZone( $timezone ) ); @@ -34,8 +35,9 @@ public function GetTimeZones() { * @param string $timezone * @param string $format * @return string + * @throws Exception */ - public function GetRelativeTime( $text, $timezone = 'UTC', $format = 'c' ) { + public function GetRelativeTime( string $text, $timezone = 'UTC', $format = 'c' ) { $result = new DateTime( $text, new DateTimeZone( $timezone ) ); return $result->format( $format ); } diff --git a/tests/lib/DateTimeService.php b/tests/lib/DateTimeService.php index f1a0df1..eee8131 100644 --- a/tests/lib/DateTimeService.php +++ b/tests/lib/DateTimeService.php @@ -33,8 +33,9 @@ public function GetTimeZones() { * @param string $timezone * @param string $format * @return string + * @throws Exception */ - public function GetRelativeTime( $text, $timezone = 'UTC', $format = 'c' ) { + public function GetRelativeTime( string $text, $timezone = 'UTC', $format = 'c' ): string { $result = new DateTime( $text, new DateTimeZone( $timezone ) ); return $result->format( $format ); } diff --git a/tests/lib/DateTimeServiceClient.php b/tests/lib/DateTimeServiceClient.php deleted file mode 100644 index 470201f..0000000 --- a/tests/lib/DateTimeServiceClient.php +++ /dev/null @@ -1,66 +0,0 @@ -call( __FUNCTION__, array( 'timezone' => $timezone, 'format' => $format ), $this->getRequestId( $isNotification ) ); - } - - - /** - * Returns associative array containing dst, offset and the timezone name - * @param bool $isNotification [optional] set to true if call is notification - * @return \EazyJsonRpc\BaseJsonRpcCall (result: array) - */ - public function GetTimeZones( $isNotification = false ) { - return $this->call( __FUNCTION__, array(), $this->getRequestId( $isNotification ) ); - } - - - /** - * Get Relative time - * @param string $text a date/time string - * @param string $timezone [optional] - * @param string $format [optional] - * @param bool $isNotification [optional] set to true if call is notification - * @return \EazyJsonRpc\BaseJsonRpcCall (result: string) - */ - public function GetRelativeTime( $text, $timezone = 'UTC', $format = 'c', $isNotification = false ) { - return $this->call( __FUNCTION__, array( 'text' => $text, 'timezone' => $timezone, 'format' => $format ), $this->getRequestId( $isNotification ) ); - } - - - /** - * Implode Function - * @param string $glue - * @param array $pieces [optional] - * @param bool $isNotification [optional] set to true if call is notification - * @return \EazyJsonRpc\BaseJsonRpcCall (result: string) - */ - public function Implode( $glue, $pieces = array( 0 => '1', 1 => '2', 2 => '3' ), $isNotification = false ) { - return $this->call( __FUNCTION__, array( 'glue' => $glue, 'pieces' => $pieces ), $this->getRequestId( $isNotification ) ); - } - - - /** - * Get Instance - * @return DateTimeServiceClient - */ - public static function GetInstance() { - return new self( 'http://eazyjsonrpc/tests/example-server.php?v3' ); - } - - } \ No newline at end of file From 6655c381c6c9d9dab1dcee872ff1f33aa40d4c2f Mon Sep 17 00:00:00 2001 From: Alexander Korelskiy Date: Tue, 6 Apr 2021 16:30:45 +0300 Subject: [PATCH 2/2] Migrate to guzzle --- README.md | 6 ++- composer.json | 3 +- generator/JsonRpcClientGenerator.php | 4 +- src/EazyJsonRpc/BaseJsonRpcClient.php | 53 ++++++++++--------- src/EazyJsonRpc/HttpException.php | 7 --- tests/JsonRpcClient/DatePingServiceClient.php | 28 +++++----- tests/JsonRpcClient/DateTimeServiceClient.php | 16 +++--- 7 files changed, 59 insertions(+), 58 deletions(-) delete mode 100644 src/EazyJsonRpc/HttpException.php diff --git a/README.md b/README.md index 3430804..ed84ef9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ eazy-jsonrpc ============ +[![Latest Version](https://img.shields.io/github/release/sergeyfast/eazy-jsonrpc.svg?style=flat-square)](https://github.com/sergeyfast/eazy-jsonrpc/releases) +[![Total Downloads](https://img.shields.io/packagist/dt/sergeyfast/eazy-jsonrpc.svg?style=flat-square)](https://packagist.org/packages/sergeyfast/eazy-jsonrpc) PHP JSON-RPC 2.0 Server/Client Implementation with Automatic Client Class Generation via SMD @@ -29,7 +31,7 @@ Client $client = ::GetInstance(); try { -$result = $client->Method(); + $result = $client->Method(); } catch (BaseJsonRpcException $e) { // work with exception } @@ -44,7 +46,7 @@ Client with typed returns by rpcgen $client = RpcClient::GetInstance(); try { -$result = $client->Method(); + $result = $client->Method(); } catch (BaseJsonRpcException $e) { // work with exception } diff --git a/composer.json b/composer.json index b4583d3..0caa298 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "php":">=7.2.0", "ext-curl": "*", "ext-json": "*", - "netresearch/jsonmapper": "^4.0" + "netresearch/jsonmapper": "^4.0", + "guzzlehttp/guzzle": "^7.3" }, "require-dev":{ "phpunit/phpunit": "4.8.*" diff --git a/generator/JsonRpcClientGenerator.php b/generator/JsonRpcClientGenerator.php index bc98836..6a72065 100644 --- a/generator/JsonRpcClientGenerator.php +++ b/generator/JsonRpcClientGenerator.php @@ -65,7 +65,7 @@ private function getHeader() { use EazyJsonRpc\BaseJsonRpcClient; use EazyJsonRpc\BaseJsonRpcException; - use EazyJsonRpc\HttpException; + use GuzzleHttp\Exception\GuzzleException; use JsonMapper_Exception; /** @@ -174,7 +174,7 @@ public function getMethod( $methodName, $methodData ): string { /** * {$description}{$strDocParams} * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function {$methodName}({$strParams})$strReturnType { diff --git a/src/EazyJsonRpc/BaseJsonRpcClient.php b/src/EazyJsonRpc/BaseJsonRpcClient.php index 31a88ab..773608b 100644 --- a/src/EazyJsonRpc/BaseJsonRpcClient.php +++ b/src/EazyJsonRpc/BaseJsonRpcClient.php @@ -2,6 +2,9 @@ namespace EazyJsonRpc; + use GuzzleHttp\Client; + use GuzzleHttp\Exception\GuzzleException; + use GuzzleHttp\RequestOptions; use JsonMapper; /** @@ -20,14 +23,10 @@ class BaseJsonRpcClient { public $UseObjectsInResults = false; /** - * Curl Options + * Guzzle Client Options * @var array */ - public $CurlOptions = [ - CURLOPT_POST => 1, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_HTTPHEADER => [ 'Content-Type: application/json' ], - ]; + private $ClientOptions = []; /** * Current Request id @@ -41,6 +40,17 @@ class BaseJsonRpcClient { */ private $isBatchCall = false; + /** + * Guzzle HTTP Client + * @var Client + */ + private $client; + + /** + * @var string + */ + private $serverUrl; + /** * Batch Calls * @var BaseJsonRpcCall[] @@ -59,7 +69,10 @@ class BaseJsonRpcClient { * @param string $serverUrl */ public function __construct( string $serverUrl ) { - $this->CurlOptions[CURLOPT_URL] = $serverUrl; + $opts = [ RequestOptions::TIMEOUT => 2.0 ]; + $opts = array_merge( $opts, $this->ClientOptions ); + $this->client = new Client( $opts ); + $this->serverUrl = $serverUrl; } @@ -92,6 +105,7 @@ public function BeginBatch(): bool { /** * Commit Batch * @return array + * @throws GuzzleException */ public function CommitBatch(): array { if ( !$this->isBatchCall || ( !$this->batchCalls && !$this->batchNotifications ) ) { @@ -134,6 +148,7 @@ public function RollbackBatch(): bool { * @param string $returnType * @return mixed * @throws BaseJsonRpcException + * @throws GuzzleException * @throws \JsonMapper_Exception */ protected function call( string $method, string $returnType, array $parameters = [], $id = null ) { @@ -163,6 +178,7 @@ protected function call( string $method, string $returnType, array $parameters = * @param array $parameters * @return BaseJsonRpcCall * @throws BaseJsonRpcException + * @throws GuzzleException * @throws \JsonMapper_Exception */ public function __call( string $method, array $parameters = [] ) { @@ -174,31 +190,20 @@ public function __call( string $method, array $parameters = [] ) { * Process Calls * @param BaseJsonRpcCall[] $calls * @return mixed - * @throws HttpException + * @throws GuzzleException */ protected function processCalls( array $calls ): bool { // Prepare Data $singleCall = !$this->isBatchCall ? reset( $calls ) : null; $result = $this->batchCalls ? array_values( array_map( '\EazyJsonRpc\BaseJsonRpcCall::GetCallData', $calls ) ) : BaseJsonRpcCall::GetCallData( $singleCall ); - // Send Curl Request - $options = $this->CurlOptions + [ CURLOPT_POSTFIELDS => json_encode( $result ) ]; - $ch = curl_init(); - curl_setopt_array( $ch, $options ); - - $data = curl_exec( $ch ); - - if ( curl_errno( $ch ) ) { - throw new HttpException( 'Error with curl response: ' . curl_error( $ch ) ); - } - - $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); - if ( $httpCode != 200 ) { - throw new HttpException( 'Error with http response, got http status: ' . $httpCode ); + try { + $resp = $this->client->post( $this->serverUrl, [ RequestOptions::JSON => $result ] ); + } catch ( GuzzleException $e ) { + throw $e; } - $data = json_decode( $data, !$this->UseObjectsInResults ); - curl_close( $ch ); + $data = json_decode( $resp->getBody()->getContents(), !$this->UseObjectsInResults ); if ( $data === null ) { return false; } diff --git a/src/EazyJsonRpc/HttpException.php b/src/EazyJsonRpc/HttpException.php deleted file mode 100644 index 08ac564..0000000 --- a/src/EazyJsonRpc/HttpException.php +++ /dev/null @@ -1,7 +0,0 @@ - '1', 1 => '2', 2 => '3'), $isNotification = false ): string { @@ -89,7 +89,7 @@ public function date_Implode( string $glue, array $pieces = array( 0 => '1', 1 * @param bool $isNotification [optional] set to true if call is notification * @return string pong * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function ping_Ping( string $message = 'pong', $isNotification = false ): string { @@ -103,7 +103,7 @@ public function ping_Ping( string $message = 'pong', $isNotification = false ): * @param bool $isNotification [optional] set to true if call is notification * @return array News * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function news_GetList( $isNotification = false ): array { @@ -118,7 +118,7 @@ public function news_GetList( $isNotification = false ): array { * @param bool $isNotification [optional] set to true if call is notification * @return array News * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function news_GetById( int $id, $isNotification = false ): array { @@ -132,7 +132,7 @@ public function news_GetById( int $id, $isNotification = false ): array { * @param bool $isNotification [optional] set to true if call is notification * @return array Category Tag * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function news_GetTags( $isNotification = false ): array { @@ -146,7 +146,7 @@ public function news_GetTags( $isNotification = false ): array { * @param bool $isNotification [optional] set to true if call is notification * @return array * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function news_Categories( $isNotification = false ): array { @@ -163,7 +163,7 @@ public function news_Categories( $isNotification = false ): array { * @param bool $isNotification [optional] set to true if call is notification * @return array News * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function news_Search( array $s, int $page, int $count = 50, $isNotification = false ): array { @@ -179,7 +179,7 @@ public function news_Search( array $s, int $page, int $count = 50, $isNotificati * @param bool $isNotification [optional] set to true if call is notification * @return array * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function news_NameValue( array $nv, int $c = null, $isNotification = false ): array { @@ -192,7 +192,7 @@ public function news_NameValue( array $nv, int $c = null, $isNotification = fals * @param $url string * @return DatePingServiceClient */ - public static function GetInstance( string $url = 'http://localhost/tests/example-server.php' ): DatePingServiceClient { + public static function GetInstance( string $url ): DatePingServiceClient { return new self( $url ); } diff --git a/tests/JsonRpcClient/DateTimeServiceClient.php b/tests/JsonRpcClient/DateTimeServiceClient.php index da6c46d..4eefa27 100644 --- a/tests/JsonRpcClient/DateTimeServiceClient.php +++ b/tests/JsonRpcClient/DateTimeServiceClient.php @@ -1,14 +1,14 @@ '1', 1 => '2', 2 => '3'), $isNotification = false ): string { @@ -86,7 +86,7 @@ public function Implode( string $glue, array $pieces = array( 0 => '1', 1 => ' * @param bool $isNotification [optional] set to true if call is notification * @return array * @throws BaseJsonRpcException - * @throws HttpException + * @throws GuzzleException * @throws JsonMapper_Exception */ public function ComplexResult( $isNotification = false ): array { @@ -99,7 +99,7 @@ public function ComplexResult( $isNotification = false ): array { * @param $url string * @return DateTimeServiceClient */ - public static function GetInstance( string $url = 'http://localhost/tests/example-server.php' ): DateTimeServiceClient { + public static function GetInstance( string $url ): DateTimeServiceClient { return new self( $url ); }