ufo-tech / json-rpc-sdk-bundle
用于简单使用Json-RPC api并具有动态SDK的Symfony包
2.2.0
2024-07-27 08:57 UTC
Requires
- php: >=8.2
- symfony/console: >=6.4
- symfony/framework-bundle: >=6.4
- symfony/http-kernel: >=6.4
- symfony/serializer: >=6.4
- ufo-tech/json-rpc-client-sdk: ^3.2
README
用于简单使用Json-RPC的Symfony包。并且可以自动从服务器生成Symfony v.6.*的SDK
阅读文档
关于此包
用于轻松创建json-rpc服务器API的SDK和DTO的包
环境要求
新功能?
版本 1.1
- 在服务器端使用ufo-tech/json-rpc-bundle库并配置服务器响应的情况下,生成api响应的DTO
入门指南
Symfony中的自动包安装
步骤 0 (推荐): 配置Composer
为了使您的Symfony Flex在添加包时自动进行所有必要的设置,您需要在您的 composer.json 中进行以下更改
// composer.json // ... "extra" : { // ... "symfony": { // ... "endpoint": [ "https://api.github.com/repos/ufo-tech/recipes/contents/index.json?ref=main", "flex://defaults" ] } // ... }, // ...
有关Symfony Flex的更多信息,请参阅 文档
步骤 1: 安装
在项目文件夹中的控制台运行以下命令以下载此包的最新版本
composer require ufo-tech/json-prc-sdk-bundle
步骤 2: 注册包
确保在项目的 `config/bundles.php` 文件中自动注册了此包
<?php // config/bundles.php return [ // ... Ufo\JsonRpcSdkBundle\JsonRpcSdkBundle::class => ['all' => true],, // ... ];
步骤 3: 添加参数
在 config/packages/json_rpc_sdk.yaml 中,您可以配置生成器,以便在服务器更改方法时自动重新生成SDK
以下是一个带说明的示例
# config/packages/json_rpc_sdk.yaml json_rpc_sdk: #Namespace of generated SDK. Files will be generated to folder which contain this namespace by PSR-4. #Folder will be created if not exists. namespace: App\Sdk #List of "domain" or vendor of API server. vendors: - name: products url: https://products.example.com/rpc - name: orders # Required: Name of vendor namespace url: https://orders-api.example.com/api-rpc # Required: Url of endpoint of JsonRPC token_key: some-key # Optional: Name of Header token if security enabled token: dsfsdfsdfsdfsdfsd32 # Optional: Value of Header token if security enabled
.ENV
您可以通过环境变量传递令牌以访问提供者API
PRODUCT_API_TOKEN_KEY=Rpc-Security-Token
PRODUCT_API_TOKEN=048ecafe6228863c444b7320e6e943d4
ORDER_API_TOKEN_KEY=Rpc-Custom-Token
ORDER_API_TOKEN=048ecafe624x22x3er4b7320e6e943d4
# config/packages/json_rpc_sdk.yaml json_rpc_sdk: #... vendors: #... - name: products url: https://products.example.com/rpc token_key: %env(resolve:PRODUCT_API_TOKEN_KEY)% token: %env(resolve:PRODUCT_API_TOKEN)% - name: orders url: https://orders-api.example.com/api-rpc token_key: %env(resolve:ORDER_API_TOKEN_KEY)% token: %env(resolve:ORDER_API_TOKEN)%
步骤 4: 生成SDK
您有两种生成SDK的选项
- 从JsonRPC服务器生成一个供应商的SDK。
- 从配置文件生成每个供应商-URL的SDK
对于单个SDK,您必须使用以下命令执行 bin/console ufo:sdk:make
- 供应商 (必需)
- RPC端点URL (必需)
- 令牌名称 (可选)
- 令牌值 (可选)
示例
bin/console ufo:sdk:make vendor http://api.endpoint/rpc -ttoken-name -stoken-value
对于批量生成,只需运行不带参数即可(参数将从配置中传递)
bin/console ufo:sdk:generate
完成!您已生成SDK。
⚠️ 注意!
RPC服务器响应将被缓存1小时。如果您需要清除它并再次请求API,请运行
bin/console cache:clear
步骤 5: SDK的使用
生成后,您就可以使用您的SDK了。
以下是一个生成的SDK示例。
App\Sdk\Test\PingProcedure
<?php /** * Auto generated SDK class for easy usage RPC API Test * @link http://nginx/rpc * Created at 23.07.2023 20:59:37 * * @category ufo-tech * @package json-rpc-client-sdk * @generated ufo-tech/json-rpc-client-sdk * @author Alex Maystrenko <ashterix69@gmail.com> * @see https://ufo-tech.github.io/json-rpc-client-sdk/ Library documentation * @license https://github.com/ufo-tech/json-rpc-client-sdk/blob/main/LICENSE */ namespace App\Sdk\Test; use Ufo\RpcSdk\Interfaces\ISdkMethodClass; use Ufo\RpcSdk\Procedures\AbstractProcedure; use Ufo\RpcSdk\Procedures\ApiMethod; use Ufo\RpcSdk\Procedures\ApiUrl; use Ufo\RpcObject\RpcResponse; use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; /** * RPC API Test * @link http://nginx/rpc */ #[ApiUrl('http://nginx/rpc')] #[AutoconfigureTag('ufo.sdk_method_class')] class PingProcedure extends AbstractProcedure implements ISdkMethodClass { /** * @method PingProcedure.ping * @return string */ #[ApiMethod('PingProcedure.ping')] public function ping(): string { return $this->requestApi()->getResult(); } }
太棒了。现在让我们来使用它。尽可能简单。在您的 Controllers/Procedures/Commands/e.t.c 中,您只需要从您的SDK调用SDK方法并传递所需的任何参数。就像这样
<?php namespace App\Controller; use App\Sdk\Test\PingProcedure; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class HomeController extends AbstractController { #[Route('/test', name: 'test')] public function indexAction(PingProcedure $procedure): Response { $response = $procedure->ping(); return new Response($response); } }
步骤 6: 利润
当您调用方法时,此包将使用配置的令牌向服务器发出RPC请求,带上所有参数,然后处理响应,在幕后返回您可以使用的仅数据。此库就像您在本地使用它一样,在您的代码和远程RPC代码之间充当桥梁。