allansun/openapi-runtime
Composer 安装命令:
composer require allansun/openapi-runtime
包简介
Runtime library to be used with other SDK generated from OpenAPI docs
README 文档
README
Runtime library to be used with other SDK generated from OpenAPI docs.
Installation
composer require allansun/openapi-runtime
You will also need a PSR-18 compatible client see https://docs.php-http.org/en/latest/clients.html
So either use Guzzle (or any other PSR-18 compatible clients)
composer require php-http/guzzle7-adapter
Basic concepts
OpenAPI (formally Swagger) defines how API should be interacted with by giving
- API endpoint URI
- input payload and output response (normally in JSON format)
Code generators can generate valid code by parsing the OpenAPI doc.
We try to provide a guidance on how you should organize your code generation.
ResponseHandlers
One key function this lib provides is to transform response JSON into predefined PHP objects.
By calling Client::configure() you can customize your own ResponseHanderStack , which basically is a stack of transformers to parse the response JSON.
You can create your own ResponseHandler by implementing the ResponseHandlerInterface. It is simply an invokable class either returns a Model or throws an UndefinedResponseException so the ResponseHandlerStack can try next handler.
By default, we provide a simple JSON response handler( JsonPsrResponseHandler. It will try to parse the response into a model by looking into reference, which is defined in ReponseTypes. Be aware you should set up your response references by calling ResponseTypes::setTypes().
Usage
First your generated code should provide a way to parse response references into ResponseTypes, or you can create your own ResponseTypes class and inject into a Handler then into a HandlerStack
<?php namespace App; use OpenAPI\Runtime\ResponseTypes; use OpenAPI\Runtime\ResponseHandler\JsonResponseHandler; use OpenAPI\Runtime\ResponseHandlerStack\ResponseHandlerStack; ResponseTypes::setTypes([ 'operation_id' =>[ // This should be unique to $ref as defined in the OpenAPI doc '200.' => 'YourGeneratedModelClass::class', // We add a dot after there HTTP status code to enforce string type '404.' => 'ErrorModel::class' ] ]); class MyResponseHandlerStack extends ResponseHandlerStack { public function __construct(?ResponseTypesInterface $responseTypes = null) { $handler = new JsonResponseHandler(); if ($responseTypes) { $handler->setResponseTypes($responseTypes); } parent::__construct([$handler]); } }
Then in the generated code you API class should set have the $responseHandlerStack class name ready (a instance will be created on API instantiation)
<?php // Should be generated code here namespace App\GeneratedCode; use OpenAPI\Runtime\AbstractAPI; class Customer extends AbstractAPI{ protected static $responseHandlerStack = MyResponseHandlerStack::class; public function get($id) { return $this->request('operation_id', 'GET',"/customer/${id}"); } public function post(array $payload) { return $this->request('post_operation_id','POST','/customer/',$payload); } }
Projects using this lib
allansun/openapi-runtime 适用场景与选型建议
allansun/openapi-runtime 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.63k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「swagger」 「runtime」 「docker」 「openapi」 「kubernetes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 allansun/openapi-runtime 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 allansun/openapi-runtime 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 allansun/openapi-runtime 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Docker based testing environment for shopware projects.
The Laragoon utility package
Docker compose for Robo Task Runner
Make your software programmable. Run WebAssembly extensions in your app using the first off-the-shelf, universal plug-in system.
Deployment command
Swagger integration to Laravel 5
统计信息
- 总下载量: 5.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 119
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-09