Wiki source code of JsonRpcClientSdk
Last modified by Ashterix on 2024/05/27 10:36
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
2.1 | 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 | |||
![]() |
4.1 | 7 | |**Актуальна версія** | [[image:https://img.shields.io/github/v/tag/ufo-tech/json-rpc-client-sdk?color=blue&label=&logo=alienware&logoColor=white&labelColor=7b8185]] |
![]() |
2.1 | 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]] | ||
![]() |
3.1 | 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]] |
![]() |
2.1 | 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}} | ||
![]() |
4.2 | 18 | |
![]() |
7.1 | 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. |
![]() |
4.2 | 20 | |
![]() |
7.1 | 21 | == Generating the SDK == |
![]() |
4.2 | 22 | |
![]() |
7.1 | 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. |
![]() |
4.2 | 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 | |||
![]() |
6.1 | 31 | (% class="box warningmessage" %) |
32 | ((( | ||
![]() |
7.1 | 33 | IMPORTANT: The generator uses documentation caching for 30 minutes. |
![]() |
6.1 | 34 | ))) |
35 | |||
![]() |
7.1 | 36 | == Example of Using the SDK == |
![]() |
4.2 | 37 | |
![]() |
7.1 | 38 | This example demonstrates working with the generated SDK. |
![]() |
4.2 | 39 | |
![]() |
5.1 | 40 | (% class="box warningmessage" %) |
41 | ((( | ||
![]() |
7.1 | 42 | IMPORTANT: You may have other procedure classes. The example only shows the concept of interaction. |
![]() |
5.1 | 43 | ))) |
44 | |||
![]() |
4.2 | 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 | |||
![]() |
7.1 | 90 | == Debugging Requests and Responses == |
![]() |
4.2 | 91 | |
92 | {{code language="php" layout="LINENUMBERS"}} | ||
93 | <?php | ||
94 | |||
95 | // ... | ||
96 | use Ufo\RpcSdk\Procedures\RequestResponseStack; | ||
97 | // ... | ||
98 | |||
![]() |
7.1 | 99 | $fullStack = RequestResponseStack::getAll(); // get all previous requests and responses |
100 | $lastStack = RequestResponseStack::getLastStack(); // get last requests and responses | ||
![]() |
4.2 | 101 | |
![]() |
7.1 | 102 | $lastRequest = RequestResponseStack::getLastRequest(); // get last request |
103 | $lastResponse = RequestResponseStack::getLastResponse(); // get last response | ||
![]() |
4.2 | 104 | // ... |
![]() |
5.1 | 105 | {{/code}} |
![]() |
4.2 | 106 | |
![]() |
7.1 | 107 | == Custom Generator Settings == |
![]() |
5.1 | 108 | |
![]() |
7.1 | 109 | You can also generate the SDK in a custom way. |
![]() |
5.1 | 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 | ); | ||
![]() |
6.1 | 124 | |
125 | $maker->make(); | ||
![]() |
4.2 | 126 | {{/code}} |
127 | |||
![]() |
6.1 | 128 | (% class="wikigeneratedid" %) |
![]() |
7.1 | 129 | You can also pass a callback to the make method for possible additional processing after generation. |
![]() |
6.1 | 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 | |||
![]() |
7.1 | 141 | == Advantages == |
![]() |
4.2 | 142 | |
143 | {{info}} | ||
![]() |
7.1 | 144 | This SDK ensures ease of use and efficient interaction with json-RPC servers. |
![]() |
4.2 | 145 | {{/info}} |