ghiyam/apix

支持对外部API进行通用客户端-服务器请求的Yii2模块

维护者

详细信息

github.com/Ghiya/apix

源代码

安装: 273

依赖者: 0

建议者: 0

安全性: 0

星星: 2

关注者: 1

分支: 1

类型:yii2-extension

2.0.6 2024-02-08 08:43 UTC

README

2.0.6

这是一个适用于Yii2框架的应用程序中的动态客户端-服务器请求到外部API的通用插件。

它是如何工作的?

APIx 插件是一个 模块,负责处理请求时的API服务配置和相应的路由。每个服务都由一个 控制器 表示,其 动作 定义了对该服务API的请求。插件包含用于cURL/SOAP/SMPP连接到API的集成客户端对象,这些对象将在具体实现中继承。

服务配置

每个API服务都由相应的控制器表示,因此所有使用的服务都在插件模块的 controllerMap 参数中配置。控制器动作应返回以下格式的请求/请求数组。

注意:为了正确工作,API服务的控制器需要从 [\ghiyam\apix\controllers\ServiceController] 继承。

[
    // ...
    'modules'    =>
        [
            'apix' => [
                'class' => 'ghiyam\apix\APIx',
                'controllerMap' => [
                    // API service with REST client example
                    'some_vendor'    =>
                        [
                            // default controller class is abstract, use inheritance instead
                            'class'      => 'ghiyam\apix\controller\ServiceController',
                            'service' =>
                                [
                                    'client' => [
                                        // default client class is abstract, use inheritance instead
                                        'class'         => '\ghiyam\apix\clients\CurlApiClient',
                                        'credentials'   => [],
                                        'clientOptions' => [
                                            'host'    => 'someHost',
                                            'port'    => 443,
                                            'uri'     => 'path/to/api/uri',
                                            'timeout' => 3,
                                        ]
                                    ],
                                ],
                        ],
                    // API service with SOAP client example
                    'another_vendor' =>
                        [
                            // default controller class is abstract, use implementation instead
                            'class'      => 'ghiyam\apix\controller\ServiceController',
                            'service' =>
                                [
                                    'client' => [
                                        // default client class is abstract, use inheritance instead
                                        'class'       => '\ghiyam\apix\clients\SoapApiClient',
                                        'credentials' => [],
                                        'namespaces'  =>
                                            [
                                                'header'   => '',
                                                'envelope' => '',
                                            ],
                                        'clientOptions' => [
                                            'location'     => '',
                                            'uri'          => '',
                                            'trace'        => true,
                                            'compression'  => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
                                            'exceptions'   => false,
                                            'soap_version' => SOAP_1_1,
                                            'encoding'     => 'UTF-8',
                                        ],
                                    ],
                                ],
                        ]
                    // ... any other API clients implementations...
                ],
            ],
        ],
    //...
]

构建请求

每个请求都必须包含一个必填的 method 参数和一个可选的 params 参数。第一个包含对服务API调用的方法名称,第二个包含其参数。

请求构建示例


[
    'method' => '<api_method_name>',
    'params' =>
        [
            '<param_name>' => '<param_value>'
            // ... API method params here ...
        ]
]