JsonRpcClientSdk

Last modified by Ashterix on 2024/05/27 10:36

Актуальна версія  https://img.shields.io/github/v/tag/ufo-tech/json-rpc-client-sdk?color=blue&label=&logo=alienware&logoColor=white&labelColor=7b8185
 Категорія  Api
 Тип  Library
 Залежності https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/php?logo=PHP&logoColor=white
https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/symfony/maker-bundle?label=SymfonyMaker&logo=Symfony&logoColor=white
https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/symfony/http-client?label=SymfonyHttp&logo=Symfony&logoColor=white
https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/symfony/cache?label=SymfonyCache&logo=Symfony&logoColor=white
https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/ufo-tech/rpc-objects?label=UfoRpcObject&logo=alienware&logoColor=white
 Розмір  https://img.shields.io/github/repo-size/ufo-tech/json-rpc-client-sdk?label=Size%20of%20the%20repository
 Ліцензія  https://img.shields.io/badge/license-MIT-green?labelColor=7b8185

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

IMPORTANT: The generator uses documentation caching for 30 minutes.

Example of Using the SDK

This example demonstrates working with the generated SDK.

IMPORTANT: You may have other procedure classes. The example only shows the concept of interaction.

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
<?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;
}
// ...

Debugging Requests and Responses

1
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
// ...

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
<?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();

You can also pass a callback to the make method for possible additional processing after generation.

1
2
3
4
5
6
7
<?php

//...

$maker->make(function (ClassDefinition $classDefinition) {
   // additional traversal of generated classes after generation
});

Advantages

This SDK ensures ease of use and efficient interaction with json-RPC servers.