ufo-tech / json-rpc-client-sdk
Simple clientSDK builder for any json-RPC servers
3.2.1
2024-10-24 09:35 UTC
Requires
- php: >=8.3
- ext-readline: *
- symfony/amqp-messenger: ^7.0
- symfony/cache: >=6.4
- symfony/filesystem: >=6.4
- symfony/http-client: >=6.4
- symfony/maker-bundle: ^1.48
- symfony/messenger: ^6.0|^7.0
- symfony/serializer: ^7.0
- symfony/validator: >=6.4
- symfony/yaml: ^7.1
- ufo-cms/colored_cli: ^1.0
- ufo-tech/rpc-exceptions: >=1.0.0
- ufo-tech/rpc-objects: >=2.6.0
README
Simple clientSDK builder for any json-RPC servers
See the Documentations
Generate SDK
Run cli command php bin/make.php
$ php bin/make.php > Enter API vendor name: some_vendor > Enter the API url: http://some.url/api
Use SDK
This example shows working with the generated SDK. IMPORTANT: You may have other procedure classes. The example only shows the concept of interaction.
<?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; } // ...
Debug request and response
<?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 // ...