Wiki source code of JsonRpcClientSdk
Last modified by Ashterix on 2024/05/27 10:36
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{box cssClass="floatinginfobox"}} | ||
| 2 | {{html}} | ||
| 3 | <div style='text-align: center;margin:0 0 10px 0;'><a class="button-link packagist" data-cke-saved-href="https://packagist.org/packages/ufo-tech/json-rpc-client-sdk" href="https://packagist.org/packages/ufo-tech/json-rpc-client-sdk"><img data-cke-saved-src="https://packagist.org/img/logo-small.png" src="https://packagist.org/img/logo-small.png">Packagist</a> <a class="button-link" data-cke-saved-href="https://github.com/UFO-Tech/json-rpc-client-sdk" href="https://github.com/UFO-Tech/json-rpc-client-sdk"><img data-cke-saved-src="https://camo.githubusercontent.com/dfe7e80288901f8d5e8de7562d6f94491e2a7f8042316fd544fe3b6364b63783/68747470733a2f2f69636f6e2d6c6962726172792e636f6d2f696d616765732f6769746875622d69636f6e2d77686974652f6769746875622d69636f6e2d77686974652d362e6a7067" src="https://camo.githubusercontent.com/dfe7e80288901f8d5e8de7562d6f94491e2a7f8042316fd544fe3b6364b63783/68747470733a2f2f69636f6e2d6c6962726172792e636f6d2f696d616765732f6769746875622d69636f6e2d77686974652f6769746875622d69636f6e2d77686974652d362e6a7067">GitHub</a></div> | ||
| 4 | {{/html}} | ||
| 5 | |||
| 6 | |||
| 7 | |**Актуальна версія** | [[image:https://img.shields.io/github/v/tag/ufo-tech/json-rpc-client-sdk?color=blue&label=&logo=alienware&logoColor=white&labelColor=7b8185]] | ||
| 8 | | **Категорія** | Api | ||
| 9 | | **Тип** | Library | ||
| 10 | | **Залежності** |[[image:https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/php?logo=PHP&logoColor=white]] | ||
| 11 | [[image:https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/symfony/maker-bundle?label=SymfonyMaker&logo=Symfony&logoColor=white]] | ||
| 12 | [[image:https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/symfony/http-client?label=SymfonyHttp&logo=Symfony&logoColor=white]] | ||
| 13 | [[image:https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/symfony/cache?label=SymfonyCache&logo=Symfony&logoColor=white]] | ||
| 14 | [[image:https://img.shields.io/packagist/dependency-v/ufo-tech/json-rpc-client-sdk/ufo-tech/rpc-objects?label=UfoRpcObject&logo=alienware&logoColor=white]] | ||
| 15 | | **Розмір** | [[image:https://img.shields.io/github/repo-size/ufo-tech/json-rpc-client-sdk?label=Size%20of%20the%20repository]] | ||
| 16 | | **Ліцензія** | [[image:https://img.shields.io/badge/license-MIT-green?labelColor=7b8185]] | ||
| 17 | {{/box}} | ||
| 18 | |||
| 19 | A simple library that generates an SDK for interacting with third-party services using the Json-RPC API protocol with the [[JsonRpcBundle>>doc:docs.JsonRpcBundle.WebHome]] library. The generator reads the Json API documentation and creates convenient service classes and typed DTO response classes. | ||
| 20 | |||
| 21 | == Generating the SDK == | ||
| 22 | |||
| 23 | Run the interactive CLI command {{code language="none"}}php bin/make.php{{/code}}, and enter the service name and the URL where the documentation is available. | ||
| 24 | |||
| 25 | {{code language="bash"}} | ||
| 26 | php bin/make.php | ||
| 27 | > Enter API vendor name: some_vendor | ||
| 28 | > Enter the API url: http://some.url/api | ||
| 29 | {{/code}} | ||
| 30 | |||
| 31 | (% class="box warningmessage" %) | ||
| 32 | ((( | ||
| 33 | IMPORTANT: The generator uses documentation caching for 30 minutes. | ||
| 34 | ))) | ||
| 35 | |||
| 36 | == Example of Using the SDK == | ||
| 37 | |||
| 38 | This example demonstrates working with the generated SDK. | ||
| 39 | |||
| 40 | (% class="box warningmessage" %) | ||
| 41 | ((( | ||
| 42 | IMPORTANT: You may have other procedure classes. The example only shows the concept of interaction. | ||
| 43 | ))) | ||
| 44 | |||
| 45 | {{code language="php" layout="LINENUMBERS"}} | ||
| 46 | <?php | ||
| 47 | |||
| 48 | use Symfony\Component\HttpClient\CurlHttpClient; | ||
| 49 | use Ufo\RpcSdk\Client\Shortener\UserProcedure; | ||
| 50 | use Ufo\RpcSdk\Client\Shortener\PingProcedure; | ||
| 51 | use Ufo\RpcSdk\Procedures\AbstractProcedure; | ||
| 52 | |||
| 53 | require_once __DIR__ . '/../vendor/autoload.php'; | ||
| 54 | |||
| 55 | $headers = [ | ||
| 56 | 'Ufo-RPC-Token'=>'some_security_token' | ||
| 57 | ]; | ||
| 58 | |||
| 59 | try { | ||
| 60 | $pingService = new PingProcedure( | ||
| 61 | headers: $headers | ||
| 62 | ); | ||
| 63 | echo $pingService->ping(); // print "PONG" | ||
| 64 | |||
| 65 | // ... | ||
| 66 | |||
| 67 | $userService = new UserProcedure( | ||
| 68 | headers: $headers, | ||
| 69 | requestId: uniqid(), | ||
| 70 | rpcVersion: AbstractProcedure::DEFAULT_RPC_VERSION, | ||
| 71 | httpClient: new CurlHttpClient(), | ||
| 72 | httpRequestOptions: [] | ||
| 73 | ); | ||
| 74 | $user = $userService->createUser( | ||
| 75 | login: 'some_login', | ||
| 76 | password: 'some_password' | ||
| 77 | ); | ||
| 78 | var_dump($user); | ||
| 79 | // array(3) { | ||
| 80 | // ["id"]=> int(279232969) | ||
| 81 | // ["login"]=> string(3) "some_login" | ||
| 82 | // ["status"]=> int(0) | ||
| 83 | |||
| 84 | } catch (\Throwable $e) { | ||
| 85 | echo $e->getMessage() . PHP_EOL; | ||
| 86 | } | ||
| 87 | // ... | ||
| 88 | {{/code}} | ||
| 89 | |||
| 90 | == Debugging Requests and Responses == | ||
| 91 | |||
| 92 | {{code language="php" layout="LINENUMBERS"}} | ||
| 93 | <?php | ||
| 94 | |||
| 95 | // ... | ||
| 96 | use Ufo\RpcSdk\Procedures\RequestResponseStack; | ||
| 97 | // ... | ||
| 98 | |||
| 99 | $fullStack = RequestResponseStack::getAll(); // get all previous requests and responses | ||
| 100 | $lastStack = RequestResponseStack::getLastStack(); // get last requests and responses | ||
| 101 | |||
| 102 | $lastRequest = RequestResponseStack::getLastRequest(); // get last request | ||
| 103 | $lastResponse = RequestResponseStack::getLastResponse(); // get last response | ||
| 104 | // ... | ||
| 105 | {{/code}} | ||
| 106 | |||
| 107 | == Custom Generator Settings == | ||
| 108 | |||
| 109 | You can also generate the SDK in a custom way. | ||
| 110 | |||
| 111 | {{code language="php" layout="LINENUMBERS"}} | ||
| 112 | <?php | ||
| 113 | use Ufo\RpcSdk\Maker\Maker; | ||
| 114 | |||
| 115 | require_once __DIR__ . '/../vendor/autoload.php'; | ||
| 116 | |||
| 117 | $maker = new Maker( | ||
| 118 | apiUrl: $apiUrl, | ||
| 119 | apiVendorAlias: $vendorName, | ||
| 120 | namespace: Maker::DEFAULT_NAMESPACE, // 'Ufo\RpcSdk\Client' | ||
| 121 | projectRootDir: getcwd(), // project_dir | ||
| 122 | cacheLifeTimeSecond: Maker::DEFAULT_CACHE_LIFETIME // 3600 | ||
| 123 | ); | ||
| 124 | |||
| 125 | $maker->make(); | ||
| 126 | {{/code}} | ||
| 127 | |||
| 128 | (% class="wikigeneratedid" %) | ||
| 129 | You can also pass a callback to the make method for possible additional processing after generation. | ||
| 130 | |||
| 131 | {{code language="php" layout="LINENUMBERS"}} | ||
| 132 | <?php | ||
| 133 | |||
| 134 | //... | ||
| 135 | |||
| 136 | $maker->make(function (ClassDefinition $classDefinition) { | ||
| 137 | // additional traversal of generated classes after generation | ||
| 138 | }); | ||
| 139 | {{/code}} | ||
| 140 | |||
| 141 | == Advantages == | ||
| 142 | |||
| 143 | {{info}} | ||
| 144 | This SDK ensures ease of use and efficient interaction with json-RPC servers. | ||
| 145 | {{/info}} |