Skip to content

Commit 6655c38

Browse files
committed
Migrate to guzzle
1 parent 8f7b5ce commit 6655c38

File tree

7 files changed

+59
-58
lines changed

7 files changed

+59
-58
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
eazy-jsonrpc
22
============
3+
[![Latest Version](https://img.shields.io/github/release/sergeyfast/eazy-jsonrpc.svg?style=flat-square)](https://github.com/sergeyfast/eazy-jsonrpc/releases)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/sergeyfast/eazy-jsonrpc.svg?style=flat-square)](https://packagist.org/packages/sergeyfast/eazy-jsonrpc)
35

46
PHP JSON-RPC 2.0 Server/Client Implementation with Automatic Client Class Generation via SMD
57

@@ -29,7 +31,7 @@ Client
2931
$client = <class-name>::GetInstance(<url>);
3032
3133
try {
32-
$result = $client->Method();
34+
$result = $client->Method();
3335
} catch (BaseJsonRpcException $e) {
3436
// work with exception
3537
}
@@ -44,7 +46,7 @@ Client with typed returns by rpcgen
4446
$client = RpcClient::GetInstance(<url>);
4547
4648
try {
47-
$result = $client->Method();
49+
$result = $client->Method();
4850
} catch (BaseJsonRpcException $e) {
4951
// work with exception
5052
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"php":">=7.2.0",
3333
"ext-curl": "*",
3434
"ext-json": "*",
35-
"netresearch/jsonmapper": "^4.0"
35+
"netresearch/jsonmapper": "^4.0",
36+
"guzzlehttp/guzzle": "^7.3"
3637
},
3738
"require-dev":{
3839
"phpunit/phpunit": "4.8.*"

generator/JsonRpcClientGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function getHeader() {
6565
6666
use EazyJsonRpc\BaseJsonRpcClient;
6767
use EazyJsonRpc\BaseJsonRpcException;
68-
use EazyJsonRpc\HttpException;
68+
use GuzzleHttp\Exception\GuzzleException;
6969
use JsonMapper_Exception;
7070
7171
/**
@@ -174,7 +174,7 @@ public function getMethod( $methodName, $methodData ): string {
174174
/**
175175
* {$description}{$strDocParams}
176176
* @throws BaseJsonRpcException
177-
* @throws HttpException
177+
* @throws GuzzleException
178178
* @throws JsonMapper_Exception
179179
*/
180180
public function {$methodName}({$strParams})$strReturnType {

src/EazyJsonRpc/BaseJsonRpcClient.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace EazyJsonRpc;
44

5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use GuzzleHttp\RequestOptions;
58
use JsonMapper;
69

710
/**
@@ -20,14 +23,10 @@ class BaseJsonRpcClient {
2023
public $UseObjectsInResults = false;
2124

2225
/**
23-
* Curl Options
26+
* Guzzle Client Options
2427
* @var array
2528
*/
26-
public $CurlOptions = [
27-
CURLOPT_POST => 1,
28-
CURLOPT_RETURNTRANSFER => 1,
29-
CURLOPT_HTTPHEADER => [ 'Content-Type: application/json' ],
30-
];
29+
private $ClientOptions = [];
3130

3231
/**
3332
* Current Request id
@@ -41,6 +40,17 @@ class BaseJsonRpcClient {
4140
*/
4241
private $isBatchCall = false;
4342

43+
/**
44+
* Guzzle HTTP Client
45+
* @var Client
46+
*/
47+
private $client;
48+
49+
/**
50+
* @var string
51+
*/
52+
private $serverUrl;
53+
4454
/**
4555
* Batch Calls
4656
* @var BaseJsonRpcCall[]
@@ -59,7 +69,10 @@ class BaseJsonRpcClient {
5969
* @param string $serverUrl
6070
*/
6171
public function __construct( string $serverUrl ) {
62-
$this->CurlOptions[CURLOPT_URL] = $serverUrl;
72+
$opts = [ RequestOptions::TIMEOUT => 2.0 ];
73+
$opts = array_merge( $opts, $this->ClientOptions );
74+
$this->client = new Client( $opts );
75+
$this->serverUrl = $serverUrl;
6376
}
6477

6578

@@ -92,6 +105,7 @@ public function BeginBatch(): bool {
92105
/**
93106
* Commit Batch
94107
* @return array
108+
* @throws GuzzleException
95109
*/
96110
public function CommitBatch(): array {
97111
if ( !$this->isBatchCall || ( !$this->batchCalls && !$this->batchNotifications ) ) {
@@ -134,6 +148,7 @@ public function RollbackBatch(): bool {
134148
* @param string $returnType
135149
* @return mixed
136150
* @throws BaseJsonRpcException
151+
* @throws GuzzleException
137152
* @throws \JsonMapper_Exception
138153
*/
139154
protected function call( string $method, string $returnType, array $parameters = [], $id = null ) {
@@ -163,6 +178,7 @@ protected function call( string $method, string $returnType, array $parameters =
163178
* @param array $parameters
164179
* @return BaseJsonRpcCall
165180
* @throws BaseJsonRpcException
181+
* @throws GuzzleException
166182
* @throws \JsonMapper_Exception
167183
*/
168184
public function __call( string $method, array $parameters = [] ) {
@@ -174,31 +190,20 @@ public function __call( string $method, array $parameters = [] ) {
174190
* Process Calls
175191
* @param BaseJsonRpcCall[] $calls
176192
* @return mixed
177-
* @throws HttpException
193+
* @throws GuzzleException
178194
*/
179195
protected function processCalls( array $calls ): bool {
180196
// Prepare Data
181197
$singleCall = !$this->isBatchCall ? reset( $calls ) : null;
182198
$result = $this->batchCalls ? array_values( array_map( '\EazyJsonRpc\BaseJsonRpcCall::GetCallData', $calls ) ) : BaseJsonRpcCall::GetCallData( $singleCall );
183199

184-
// Send Curl Request
185-
$options = $this->CurlOptions + [ CURLOPT_POSTFIELDS => json_encode( $result ) ];
186-
$ch = curl_init();
187-
curl_setopt_array( $ch, $options );
188-
189-
$data = curl_exec( $ch );
190-
191-
if ( curl_errno( $ch ) ) {
192-
throw new HttpException( 'Error with curl response: ' . curl_error( $ch ) );
193-
}
194-
195-
$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
196-
if ( $httpCode != 200 ) {
197-
throw new HttpException( 'Error with http response, got http status: ' . $httpCode );
200+
try {
201+
$resp = $this->client->post( $this->serverUrl, [ RequestOptions::JSON => $result ] );
202+
} catch ( GuzzleException $e ) {
203+
throw $e;
198204
}
199205

200-
$data = json_decode( $data, !$this->UseObjectsInResults );
201-
curl_close( $ch );
206+
$data = json_decode( $resp->getBody()->getContents(), !$this->UseObjectsInResults );
202207
if ( $data === null ) {
203208
return false;
204209
}

src/EazyJsonRpc/HttpException.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/JsonRpcClient/DatePingServiceClient.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22
/**
33
* PHP RPC Client by JsonRpcClientGenerator
4-
* @date 06.04.2021 12:02
4+
* @date 06.04.2021 13:23
55
*/
66

77
namespace JsonRpcClient;
88

99
use EazyJsonRpc\BaseJsonRpcClient;
1010
use EazyJsonRpc\BaseJsonRpcException;
11-
use EazyJsonRpc\HttpException;
11+
use GuzzleHttp\Exception\GuzzleException;
1212
use JsonMapper_Exception;
1313

1414
/**
@@ -27,7 +27,7 @@ class DatePingServiceClient extends BaseJsonRpcClient {
2727
* @param bool $isNotification [optional] set to true if call is notification
2828
* @return string
2929
* @throws BaseJsonRpcException
30-
* @throws HttpException
30+
* @throws GuzzleException
3131
* @throws JsonMapper_Exception
3232
*/
3333
public function date_GetTime( string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string {
@@ -41,7 +41,7 @@ public function date_GetTime( string $timezone = 'UTC', string $format = 'c', $i
4141
* @param bool $isNotification [optional] set to true if call is notification
4242
* @return array
4343
* @throws BaseJsonRpcException
44-
* @throws HttpException
44+
* @throws GuzzleException
4545
* @throws JsonMapper_Exception
4646
*/
4747
public function date_GetTimeZones( $isNotification = false ): array {
@@ -58,7 +58,7 @@ public function date_GetTimeZones( $isNotification = false ): array {
5858
* @param bool $isNotification [optional] set to true if call is notification
5959
* @return string
6060
* @throws BaseJsonRpcException
61-
* @throws HttpException
61+
* @throws GuzzleException
6262
* @throws JsonMapper_Exception
6363
*/
6464
public function date_GetRelativeTime( string $text, string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string {
@@ -74,7 +74,7 @@ public function date_GetRelativeTime( string $text, string $timezone = 'UTC', st
7474
* @param bool $isNotification [optional] set to true if call is notification
7575
* @return string string
7676
* @throws BaseJsonRpcException
77-
* @throws HttpException
77+
* @throws GuzzleException
7878
* @throws JsonMapper_Exception
7979
*/
8080
public function date_Implode( string $glue, array $pieces = array( 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
8989
* @param bool $isNotification [optional] set to true if call is notification
9090
* @return string pong
9191
* @throws BaseJsonRpcException
92-
* @throws HttpException
92+
* @throws GuzzleException
9393
* @throws JsonMapper_Exception
9494
*/
9595
public function ping_Ping( string $message = 'pong', $isNotification = false ): string {
@@ -103,7 +103,7 @@ public function ping_Ping( string $message = 'pong', $isNotification = false ):
103103
* @param bool $isNotification [optional] set to true if call is notification
104104
* @return array News
105105
* @throws BaseJsonRpcException
106-
* @throws HttpException
106+
* @throws GuzzleException
107107
* @throws JsonMapper_Exception
108108
*/
109109
public function news_GetList( $isNotification = false ): array {
@@ -118,7 +118,7 @@ public function news_GetList( $isNotification = false ): array {
118118
* @param bool $isNotification [optional] set to true if call is notification
119119
* @return array News
120120
* @throws BaseJsonRpcException
121-
* @throws HttpException
121+
* @throws GuzzleException
122122
* @throws JsonMapper_Exception
123123
*/
124124
public function news_GetById( int $id, $isNotification = false ): array {
@@ -132,7 +132,7 @@ public function news_GetById( int $id, $isNotification = false ): array {
132132
* @param bool $isNotification [optional] set to true if call is notification
133133
* @return array Category Tag
134134
* @throws BaseJsonRpcException
135-
* @throws HttpException
135+
* @throws GuzzleException
136136
* @throws JsonMapper_Exception
137137
*/
138138
public function news_GetTags( $isNotification = false ): array {
@@ -146,7 +146,7 @@ public function news_GetTags( $isNotification = false ): array {
146146
* @param bool $isNotification [optional] set to true if call is notification
147147
* @return array
148148
* @throws BaseJsonRpcException
149-
* @throws HttpException
149+
* @throws GuzzleException
150150
* @throws JsonMapper_Exception
151151
*/
152152
public function news_Categories( $isNotification = false ): array {
@@ -163,7 +163,7 @@ public function news_Categories( $isNotification = false ): array {
163163
* @param bool $isNotification [optional] set to true if call is notification
164164
* @return array News
165165
* @throws BaseJsonRpcException
166-
* @throws HttpException
166+
* @throws GuzzleException
167167
* @throws JsonMapper_Exception
168168
*/
169169
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
179179
* @param bool $isNotification [optional] set to true if call is notification
180180
* @return array
181181
* @throws BaseJsonRpcException
182-
* @throws HttpException
182+
* @throws GuzzleException
183183
* @throws JsonMapper_Exception
184184
*/
185185
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
192192
* @param $url string
193193
* @return DatePingServiceClient
194194
*/
195-
public static function GetInstance( string $url = 'http://localhost/tests/example-server.php' ): DatePingServiceClient {
195+
public static function GetInstance( string $url ): DatePingServiceClient {
196196
return new self( $url );
197197
}
198198

tests/JsonRpcClient/DateTimeServiceClient.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22
/**
33
* PHP RPC Client by JsonRpcClientGenerator
4-
* @date 06.04.2021 12:02
4+
* @date 06.04.2021 13:23
55
*/
66

77
namespace JsonRpcClient;
88

99
use EazyJsonRpc\BaseJsonRpcClient;
1010
use EazyJsonRpc\BaseJsonRpcException;
11-
use EazyJsonRpc\HttpException;
11+
use GuzzleHttp\Exception\GuzzleException;
1212
use JsonMapper_Exception;
1313

1414
/**
@@ -25,7 +25,7 @@ class DateTimeServiceClient extends BaseJsonRpcClient {
2525
* @param bool $isNotification [optional] set to true if call is notification
2626
* @return string
2727
* @throws BaseJsonRpcException
28-
* @throws HttpException
28+
* @throws GuzzleException
2929
* @throws JsonMapper_Exception
3030
*/
3131
public function GetTime( string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string {
@@ -39,7 +39,7 @@ public function GetTime( string $timezone = 'UTC', string $format = 'c', $isNoti
3939
* @param bool $isNotification [optional] set to true if call is notification
4040
* @return array
4141
* @throws BaseJsonRpcException
42-
* @throws HttpException
42+
* @throws GuzzleException
4343
* @throws JsonMapper_Exception
4444
*/
4545
public function GetTimeZones( $isNotification = false ): array {
@@ -56,7 +56,7 @@ public function GetTimeZones( $isNotification = false ): array {
5656
* @param bool $isNotification [optional] set to true if call is notification
5757
* @return string
5858
* @throws BaseJsonRpcException
59-
* @throws HttpException
59+
* @throws GuzzleException
6060
* @throws JsonMapper_Exception
6161
*/
6262
public function GetRelativeTime( string $text, string $timezone = 'UTC', string $format = 'c', $isNotification = false ): string {
@@ -72,7 +72,7 @@ public function GetRelativeTime( string $text, string $timezone = 'UTC', string
7272
* @param bool $isNotification [optional] set to true if call is notification
7373
* @return string string
7474
* @throws BaseJsonRpcException
75-
* @throws HttpException
75+
* @throws GuzzleException
7676
* @throws JsonMapper_Exception
7777
*/
7878
public function Implode( string $glue, array $pieces = array( 0 => '1', 1 => '2', 2 => '3'), $isNotification = false ): string {
@@ -86,7 +86,7 @@ public function Implode( string $glue, array $pieces = array( 0 => '1', 1 => '
8686
* @param bool $isNotification [optional] set to true if call is notification
8787
* @return array
8888
* @throws BaseJsonRpcException
89-
* @throws HttpException
89+
* @throws GuzzleException
9090
* @throws JsonMapper_Exception
9191
*/
9292
public function ComplexResult( $isNotification = false ): array {
@@ -99,7 +99,7 @@ public function ComplexResult( $isNotification = false ): array {
9999
* @param $url string
100100
* @return DateTimeServiceClient
101101
*/
102-
public static function GetInstance( string $url = 'http://localhost/tests/example-server.php' ): DateTimeServiceClient {
102+
public static function GetInstance( string $url ): DateTimeServiceClient {
103103
return new self( $url );
104104
}
105105

0 commit comments

Comments
 (0)