ufo-tech/json-rpc-sdk-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ufo-tech/json-rpc-sdk-bundle

Composer 安装命令:

composer require ufo-tech/json-rpc-sdk-bundle

包简介

The Symfony bundle for simple usage Json-RPC api with dynamic SDK

README 文档

README

Ukraine

The Symfony bundle for simple usage Json-RPC. And automatically generation SDK from server for Symfony v.6.*

Read the Documentations

About this package

Package for easy api creation SDK and DTO for json-rpc server

License Size package_version fork

Environmental requirements

php_version symfony_version symfony_version

What's new?

Version 1.1

  • Generation of DTO for the api response under the condition of using the ufo-tech/json-rpc-bundle library on the server side and configuring the server response

Getting Started

Automatic package installation in Symfony

Step 0 (RECOMMENDED): Configure Composer

In order for your Symfony Flex to automatically make all the necessary settings when you add a package, you need to make the following changes to your composer.json

// composer.json    

// ...  
  
    "extra" : {
  
        // ...  
  
        "symfony": {
  
            // ...  
  
            "endpoint": [
                "https://api.github.com/repos/ufo-tech/recipes/contents/index.json?ref=main",
                "flex://defaults"
            ]
        }
  
        // ...  
  
    },

// ...  
  

More about Symfony Flex in doc

Step 1: Installation

From the console in the project folder, run this command to download the latest version of this package:

composer require ufo-tech/json-prc-sdk-bundle

Step 2: Register the package

Make sure that the bundle is automatically registered in your project's `config/bundles.php' file:

<?php
// config/bundles.php

return [
    // ...
    Ufo\JsonRpcSdkBundle\JsonRpcSdkBundle::class => ['all' => true],,
    // ...
];

Step 3: Adding parameters

In config/packages/json_rpc_sdk.yaml you can configure generator, to automatically regenerate SDK when server change methods

Here is example with explanation:

# 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
          ignore_methods: # Optional: List of methods to ignore in generation SDK
            - '*test'

🔍 Filtering methods during SDK generation

The SDK supports skipping RPC methods during generation.
This is done using the ignore_methods option, which accepts a list of masks.

📌 Mask Rules

  • * — any sequence of characters
  • ! at the beginning — inversion (always generate)
  • & at the beginning or after ! — indicates a sync request
  • ~ at the beginning or after ! — indicates an async request
  • Other characters are literals, meaning they represent themselves

✔️ Example masks

Mask Description
AdminApi.* ignores all methods of AdminApi class
Command.run ignores only Command.run
#Command.run ignores only Command.run in sync API
*.delete ignores all delete() methods in any class
!~Comment.delete always generates Comment.delete for async API even if *.delete blocks it
*.*Test ignores all methods ending with Test

🚫 Prohibited

Mask Reason
User.?pdate ? is not supported
~&User.update ~ and & in one mask is not supported
[A-Z]*.create regex is not supported

📎 Usage Example

# config/packages/json_rpc_sdk.yaml
json_rpc_sdk:
  #...
  vendors:
    - name: orders # Required: Name of vendor namespace
      #...
      ignore_methods: # Optional: List of methods to ignore in generation SDK
        - 'AdminApi.*'       # Exclude all methods of AdminApi class
        - 'Command.run'      # Exclude method Command.run
        - '*.delete'         # Exclude all delete() methods in any class
        - '!Comment.delete'  # Always generate Comment.delete API even if *.delete blocks it
        - '*.*Test'          # Exclude all methods ending with Test

Masks allow you to easily remove service procedures, test methods, and unwanted CRUD operations from SDK generation.

.ENV

You can pass tokens to access the provider api via environment variables

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)% 

Step 4: Generate SDK

You have two options for generating SDK:

  1. Make SDK for one vendor from JsonRPC server.
  2. Generate SDK for every vendor-URL from config file

For single SDK You have to execute bin/console ufo:sdk:make with

  • vendor (required)
  • RPC endpoint url (required)
  • Token name (optional)
  • Token value (optional)

Example:

bin/console ufo:sdk:make vendor http://api.endpoint/rpc -ttoken-name -stoken-value

For batch generating just run without arguments (arguments will be passed from config)

bin/console ufo:sdk:generate

Done! You have generated SDK.

⚠️ NOTE!

RPC server answer will be cached for 1 HOUR. If you need to clear it and request api again, run:

bin/console cache:clear

Step 5: Usage of SDK

After generating you are ready to use your SDK.

Here is example of generated 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();
    }

}

Brilliant. Now let's use it. It is as simple as possible. In your Controllers/Procedures/Commands/e.t.c You just have to call your SDK method from your SDK and pass any required parameters. Like that

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

Step 6: Profit

When you call your method, this package will make RPC request with configured token, to your server with all parameters, than handle response, under the hood and return you just data which you can use. This lib working like a bridge between your code and remote RPC code just like you use it local.

ufo-tech/json-rpc-sdk-bundle 适用场景与选型建议

ufo-tech/json-rpc-sdk-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.84k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 07 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「generator」 「api」 「bundle」 「sdk」 「json-rpc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ufo-tech/json-rpc-sdk-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ufo-tech/json-rpc-sdk-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 ufo-tech/json-rpc-sdk-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-20