JsonRpcClientSdk
Last modified by Ashterix on 2024/05/27 10:36
A simple library that generates an SDK for interacting with third-party services using the Json-RPC API protocol with the JsonRpcBundle library. The generator reads the Json API documentation and creates convenient service classes and typed DTO response classes.
Generating the SDK
Run the interactive CLI command php bin/make.php, and enter the service name and the URL where the documentation is available.
php bin/make.php
> Enter API vendor name: some_vendor
> Enter the API url: http://some.url/api
> Enter API vendor name: some_vendor
> Enter the API url: http://some.url/api
Example of Using the SDK
This example demonstrates working with the generated SDK.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
use Symfony\Component\HttpClient\CurlHttpClient;
use Ufo\RpcSdk\Client\Shortener\UserProcedure;
use Ufo\RpcSdk\Client\Shortener\PingProcedure;
use Ufo\RpcSdk\Procedures\AbstractProcedure;
require_once __DIR__ . '/../vendor/autoload.php';
$headers = [
'Ufo-RPC-Token'=>'some_security_token'
];
try {
$pingService = new PingProcedure(
headers: $headers
);
echo $pingService->ping(); // print "PONG"
// ...
$userService = new UserProcedure(
headers: $headers,
requestId: uniqid(),
rpcVersion: AbstractProcedure::DEFAULT_RPC_VERSION,
httpClient: new CurlHttpClient(),
httpRequestOptions: []
);
$user = $userService->createUser(
login: 'some_login',
password: 'some_password'
);
var_dump($user);
// array(3) {
// ["id"]=> int(279232969)
// ["login"]=> string(3) "some_login"
// ["status"]=> int(0)
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
// ...
use Symfony\Component\HttpClient\CurlHttpClient;
use Ufo\RpcSdk\Client\Shortener\UserProcedure;
use Ufo\RpcSdk\Client\Shortener\PingProcedure;
use Ufo\RpcSdk\Procedures\AbstractProcedure;
require_once __DIR__ . '/../vendor/autoload.php';
$headers = [
'Ufo-RPC-Token'=>'some_security_token'
];
try {
$pingService = new PingProcedure(
headers: $headers
);
echo $pingService->ping(); // print "PONG"
// ...
$userService = new UserProcedure(
headers: $headers,
requestId: uniqid(),
rpcVersion: AbstractProcedure::DEFAULT_RPC_VERSION,
httpClient: new CurlHttpClient(),
httpRequestOptions: []
);
$user = $userService->createUser(
login: 'some_login',
password: 'some_password'
);
var_dump($user);
// array(3) {
// ["id"]=> int(279232969)
// ["login"]=> string(3) "some_login"
// ["status"]=> int(0)
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
// ...
Debugging Requests and Responses
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
// ...
use Ufo\RpcSdk\Procedures\RequestResponseStack;
// ...
$fullStack = RequestResponseStack::getAll(); // get all previous requests and responses
$lastStack = RequestResponseStack::getLastStack(); // get last requests and responses
$lastRequest = RequestResponseStack::getLastRequest(); // get last request
$lastResponse = RequestResponseStack::getLastResponse(); // get last response
// ...
// ...
use Ufo\RpcSdk\Procedures\RequestResponseStack;
// ...
$fullStack = RequestResponseStack::getAll(); // get all previous requests and responses
$lastStack = RequestResponseStack::getLastStack(); // get last requests and responses
$lastRequest = RequestResponseStack::getLastRequest(); // get last request
$lastResponse = RequestResponseStack::getLastResponse(); // get last response
// ...
Custom Generator Settings
You can also generate the SDK in a custom way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
use Ufo\RpcSdk\Maker\Maker;
require_once __DIR__ . '/../vendor/autoload.php';
$maker = new Maker(
apiUrl: $apiUrl,
apiVendorAlias: $vendorName,
namespace: Maker::DEFAULT_NAMESPACE, // 'Ufo\RpcSdk\Client'
projectRootDir: getcwd(), // project_dir
cacheLifeTimeSecond: Maker::DEFAULT_CACHE_LIFETIME // 3600
);
$maker->make();
use Ufo\RpcSdk\Maker\Maker;
require_once __DIR__ . '/../vendor/autoload.php';
$maker = new Maker(
apiUrl: $apiUrl,
apiVendorAlias: $vendorName,
namespace: Maker::DEFAULT_NAMESPACE, // 'Ufo\RpcSdk\Client'
projectRootDir: getcwd(), // project_dir
cacheLifeTimeSecond: Maker::DEFAULT_CACHE_LIFETIME // 3600
);
$maker->make();
You can also pass a callback to the make method for possible additional processing after generation.
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
//...
$maker->make(function (ClassDefinition $classDefinition) {
// additional traversal of generated classes after generation
});
//...
$maker->make(function (ClassDefinition $classDefinition) {
// additional traversal of generated classes after generation
});