smartbox / api-bundle
Smartbox Api Bundle
Requires
- php: >=7.0
- friendsofsymfony/rest-bundle: ^1.7
- jms/serializer-bundle: ^2.4
- nelmio/api-doc-bundle: ^2.11.2
- nikic/php-parser: ^2.0@dev
- noxlogic/ratelimit-bundle: ^1.5
- predis/predis: ^1.0
- psr/cache: ^1.0
- sensio/distribution-bundle: ^4.0 || ^5.0
- sensio/framework-extra-bundle: ^3.0
- smartbox/besimple-soap: ^1.3
- smartbox/core-bundle: ~1.4
- snc/redis-bundle: ~1.1 || ~2.1
- symfony/assetic-bundle: ~2.3
- symfony/monolog-bundle: ~2.4
- symfony/symfony: ~2.8 || ~3.4
Requires (Dev)
- digitalnature/php-ref: dev-master
- guzzlehttp/guzzle: ^6.3
- phpspec/prophecy: ^1.7
- sensio/generator-bundle: ~3.1
- symfony/cache: ~3.0 || ~4.0
- symfony/phpunit-bridge: *
This package is auto-updated.
Last update: 2024-09-26 13:59:24 UTC
README
该API包允许您无缝地创建REST和SOAP API。它还为这两个API自动生成文档。
安装和使用
要安装该包,您只需执行以下操作:
- 将存储库添加到composer中
"require": {
"smartbox/api-bundle": "dev-master"
}
"repositories": [
{
"type": "vcs",
"url": "git@gitlab.production.smartbox.com/:smartesb/api-bundle.git"
}
],
-
将其添加到您的AppKernel.php文件中
-
将以下内容添加到您的config.yml文件中:导入
- { resource: "@SmartboxApiBundle/Resources/config/config.yml" }
-
修改之前的config.yml文件以确定您的端点。您可以通过运行以下命令查看输出:
php console.php config:dump-reference smartbox-api
# Default configuration for extension with alias: "smartbox_api"
smartbox_api:
# Id of user provider service which implements Symfony\Component\Security\Core\User\UserProviderInterface
#
# f.e.: security.user.provider.concrete.in_memory
#
userProvider: ~ # Required
default_controller: 'SmartboxApiBundle:API:handleCall'
# Enable/Disable throttling (dafault: false).
#
# If throttling is enabled:
# - register bundles in AppKernel.php:
# new Noxlogic\RateLimitBundle\NoxlogicRateLimitBundle(),
# new Snc\RedisBundle\SncRedisBundle(),
# - configure bundles:
# noxlogic_rate_limit:
# storage_engine: redis
# redis_client: default_client
# rate_response_message: 'You exceeded the rate limit'
# display_headers: true
#
# snc_redis:
# clients:
# default:
# type: predis
# alias: default
# dsn: redis://
#
throttling: false
# List of error codes, e.g.::
#
# 400: Bad Request, the request could not be understood by the server due to malformed syntax
# 401: Unauthorized, the request requires user authentication
# 403: Forbidden, the server understood the request, but is refusing to fulfill it
# 404: Not Found, the server has not found anything matching the Request-URI
# **The success codes can be extended and changed
errorCodes: # Required
# Prototype
id: ~
# List of success codes, e.g.::
#
# 200: Success, the information returned with the response is dependent on the method used in the request
# 201: Created, the request has been fulfilled and resulted in a new resource being created
# 202: Accepted, the request has been accepted for processing, but the processing has not been completed
# 204: No content, the server has fulfilled the request but does not need to return an entity-body
# **The success codes can be extended and changed
successCodes: # Required
# Prototype
id: ~
# List of response codes were the APIBundle should enforce an empty body, e.g.: [301,202]
restEmptyBodyResponseCodes: [] # Required
services: # Required
# Prototype
id:
parent: ~
name: ~ # Required
version: ~ # Required
# The SOAP headers namespace need to be defined.
soapHeadersNamespace: ~ # Required
# List of http headers to propagate as SOAP envelope headers in SOAP calls, e.g.::
#
# X-RateLimit-Limit: rateLimitLimit
#
# The headers must be present in order to be propagated. In this example the header is present when activating throttling
propagateHttpHeadersToSoap:
# Prototype
id: ~
removed: []
# Endpoint definitions.
# Example:
# services:
# demo_v1:
# name: demo
# version: v1
# methods:
#
# ## BOXES
# createBox:
# description: Creates a box with the given information and returns its id
# successCode: 201
# input:
# box: { type: Smartbox\ApiBundle\Tests\Fixtures\Entity\Box, group: update, mode: body }
# rest:
# route: /box
# httpMethod: POST
# throttling:
# limit: 10
# period: 60
#
methods: # Required
# Prototype
name:
# Success code to be returned
successCode: 200
# Description of the method, it will be used in the documentation
description: ~ # Required
# Controller to handle the requests to this method
controller: default
roles:
# Prototype
role: ~
defaults:
# Prototype
key: ~
# Section where the input parameters are specified.
input:
# Prototype
name:
# The description of the parameter, it will be used in the documentation.
description: ''
# The type of the input, it accepts scalar types (integer, double, string), entities (e.g.: MyNamespace\MyEntity) and arrays of them (integer[], MyNamespace\MyEntity[])
type: ~ # Required
# The group of the entity to be used, acts as a view of the entity model, determines the set of attributes to be used.
group: public
# Defines if the parameter is a requirement, filter or the body.\nBody: There can be only one input as the body,\n and it must be an Entity or array of entities.\nRequirement: Requirements are scalar parameters which are required.\nFilter: Filters are scalar parameters which are optional.
mode: requirement
# Regex with the format for the parameter, e.g.: d+
format: '[a-zA-Z0-9]+'
# Section where the output parameters are specified.
output:
# The type of the output, it accepts only entities (e.g.: MyNamespace\MyEntity) and arrays of them (MyNamespace\MyEntity[])
type: ~ # Required
# The group of the entity to be used, acts as a view of the entity model, determines the set of attributes to be used.
group: public
rest: # Required
# Route for the this API method
route: ~ # Required
# HTTP verb for this API method
httpMethod: ~ # Required
# Throttling works only when smartbox_api.throttling is set to true.
#
# Response headers:
# X-RateLimit-Limit: Limit of requests in a time window
# X-RateLimit-Remaining: Remaining requests in a time window
# X-RateLimit-Reset: The remaining window before the rate limit resets in UTC epoch seconds
# Response status code when limit is exceeded: 429 Too Many Requests
#
throttling:
# Set limit of requests in specified period.
limit: ~ # Required
# Set period to limit requests.
period: ~ # Required
- 将以下内容添加到您的routing.yml文件中
smartbox_api:
resource: "@SmartboxApiBundle/Resources/config/routing.yml"
prefix: /api
-
创建您自己的包
-
创建一个继承自APIController的控制器类,例如
class APIController extends \Smartbox\ApiBundle\Controller\APIController
{
public function handleCallAction($serviceId, $serviceName, $methodConfig, $version, $methodName, $input)
{
// Checks authorization
$this->checkAuthorization();
// Check input
$inputsConfig = $methodConfig['input'];
$this->checkInput($version, $inputsConfig, $input);
return $this->respond($response);
}
}
工具
smartbox:api:generateSDK
为特定API生成SDK。
用法: php app/console smartbox:api:generateSDK --help
smartbox:api:export-list
将API和流程列表导出到csv文件。默认情况下,将导出到/tmp
用法: php app/console smartbox:api:export-list --help"
示例
php app/console smartbox:api:export-list --export-dir "/tmp"
php app/console smartbox:api:export-list --async false --http-method GET
php app/console smartbox:api:export-list --async false --role ROLE_USER
smartbox:api:generate-soapui
使用在YAML测试文件中定义的API生成一个示例SoapUI项目。
用法: php app/console smartbox:api:generate-soapui --help
示例
php app/console smartbox:api:generate-soapui -f \"Product/sendProductInformation\" -p ganesh-tt-one17.smartbox-test.local"
smartbox:api:dumpPrintable
将API文档作为可打印的HTML文件输出。
用法: php app/console smartbox:api:dumpPrintable
贡献
- 分支它!
- 创建您的功能分支:
git checkout -b my-new-feature
- 提交您的更改:
git commit -am '添加一些功能'
- 将更改推送到分支:
git push origin my-new-feature
- 提交pull request :D
测试
查看Tests/Fixtures/中的小型测试应用
历史
v0.1.0:修复了ApiConfigurator中的小错误 v0.1.1:修复了模板文件夹名称中的小错误 v0.1.2:改进了SOAP调用文档模板 v0.1.3:添加了缺少的实体Ok v0.1.4:改进了对另一个实体属性为数组的支持 v0.1.5:创建Readme.md文件 v0.1.6:添加了WSI合规性 v0.1.7:删除了对非rest调用的异常处理 v0.1.8:添加了节流 v0.1.9:将头传播到SOAP
贡献者
Jose Rufino, Marcin Skurski, Luciano Mammino, Alberto Rodrigo