sudiptochoudhury/php-api-client-forge
Composer 安装命令:
composer require sudiptochoudhury/php-api-client-forge
包简介
Create restful API client in PHP
README 文档
README
Create restful API client from Postman Collection data (or other sources) in PHP with least code.
php-api-client-forge
- Create restful API client class from Postman Collection data (or other sources)
- Auto generate class
@methoddocumentation for phpDoc - Auto generate Markdown API Documentation
Import API definitions from Postman exported JSON to a JSON compatible with Guzzle\Description format
or pass a GuzzleHttp\Command\Guzzle\Description object and
your API class is ready to roll.
As a bonus, you will also get an auto generated API documentation in markdown format. Moreover, you will get a php file with
@methoddeclarations for phpDoc class documentation.
This package needs to be used as parent. Extend the Client class to make you own API class.
This is specially designed to import Postman collection exported as JSON (Collection v2.1 - Schema).
Basics
<?php namespace My\Namespace\ApiProvider; use SudiptoChoudhury\Support\Forge\Api\Client as ApiForge; class Api extends ApiForge { protected $DEFAULT_API_JSON_PATH = './config/GuzzleDescription.json'; protected $DEFAULTS = [ 'AuthToken' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx', 'ClientID' => 'xxxxxxxxxxxxxxx', 'client' => [ 'base_uri' => 'https://api.provider.com/api/v1/', 'verify' => false, 'headers' => [ 'Authorization' => 'authtoken {{AuthToken}}', 'X-clientID' => "{{ClientID}}", ], ], ]; }
That should be it. Nothing else is required. All the API endpoint will be intelligently converted to functions of this
class. For example, if you have an endpoint [GET]/products, you will get a getProducts function. Here are some more
examples:
| HTTP Method | Endpoint | Converted function name | Parameters | Example call |
|---|---|---|---|---|
| GET | /products | getProducts |
none | $api->getProducts() |
| GET | /products/{product_id} | getProduct |
product_id |
$api->getProduct(['product_id' => 101]) |
| POST | /products | addProduct |
a list of params | $api->addProduct($data) |
| PUT | /products/{product_id} | updateProduct | product_id and a list of other parameters |
$api->updateProduct(['product_id' => 101])... |
| DELETE | /products/{product_id} | deleteProduct | product_id |
$api->deleteProduct(['product_id' => 101]) |
Tried best to normalize the singulars and plurals.
There can be cases when the API endpoint structure is same as the another but the data passed to it determines a different action altogether. Ideally, this should not happen but not all API enpoints are ideally created. So, an endpoint like
[get]/products?filter_by=activealong with another endpoint[get]/productswill have the same name. To keep both, the default behaviour is to add an_before the duplicate name. However, you can add filters or hooks to completely change the duplicate name to something else.
Installation
Requirements
- Any flavour of PHP 7.0+ should do
Install With Composer
You can install the library via Composer by adding the following line to the require block of your composer.json file (replace dev-master with latest stable version):
"sudiptochoudhury/php-api-client-forge": "dev-master"
or run the following command:
composer require sudiptochoudhury/php-api-client-forge
Importing Postman JSON
Export your Postman Collection using COllecton v2.1 format. It will be exported as JSON. Give the file a
name, say postman.json and keep it inside a folder in your project.
Next, extend Import class (recommended) or may use it directly.
By Extending the Import class
<?php namespace My\Namespace\ApiProvider; use SudiptoChoudhury\Support\Forge\Api\Import as ParentImport; /** * Class Import * * @package Pour\Package\Name */ class Import extends ParentImport { protected $DEFAULT_API_JSON_PATH = './config/GuzzleDescription.json'; protected $DEFAULT_SOURCE_JSON_PATH = './config/postman.json'; }
You are ready to import. That's pretty much it (though there are tons of configurable options and hooks... we will come to that soon).
Now, let's import in some other part of your codebase.
<?php use My\Namespace\ApiProvider\Import; ... new (Import())->writeDefinition(); ...
Done!
Directly using parent class
It's even simpler (though)
<?php use SudiptoChoudhury\Support\Forge\Api\Import; ... new (Import([ './config/postman.json' ]))->writeDefinition( './config/GuzzleDescription.json'); ...
Simpler, eh!
You will find a new .json, a new .md and a new .php file to server you well in your API venture forward.
The .json file is obviously based on data structure required to create a GuzzleHttp\Command\Guzzle\Description
object.
The .php file has phpDoc @method definitions and may look something similar to
<?php /** * @method array getHostedpage(array $parameters) Getting the details of a particular hosted page * @method array getHostedpages() Listing all the hostedpages
You can copy the method definitions parts to yor API class extended from the Client class.
The .md file is a markdown file with a table containing details of the API functions, equivalent endpoints,
parameters, default values for parameters (if defined) and description - all derived from the Postman json file.
However, I would recommend to use the extend option as you will get a plethora of options to hook into various sections of parent code while the conversion of the Postman data to Guzzle Description is undergoing. You may want to add default values or change a name or change description or skip some items. All can be done by overriding functions of Import parent class and defining custom filter functions.
What are custom filter function?!!!
Well, let's pick an example. Let's say you see you want to change the duplicate function name for [get]/products as
I stated an example in previous section. By default, the duplicate name will be set to _getProducts
(remember? by default adding a _ before the name).
Now, in your extended Import class just define this
<?php /** This is a filter * @param $apiFunctionName * @param array $helperData * @return string */ public function filterFinalName($apiFunctionName, $helperData = []) { if ($apiFunctionName === '_getProducts') { $apiFunctionName = 'getActiveProducts'; } return $apiFunctionName; }
This is a filter function. The name must start with filter followed by the filter name and can then optionally
add an _ and anything you want after that, just in case you want to define multiple functions for the same filter
(like - filterFinalName_xxx or filterFinalName_001).
Each filter function will receive the value to be modified in the first parameter. The optional second parameter will be an associative array of helper data items that may help in determining logic of how the filter can be applied. At times it may also be the source of data to build documentation layouts. For example, the markdown API table is generated via a default filter. You can override it to generate the API table in your own way. May be you do not like table structure, you may devise you own structure. Sky is the limit. Additionally, there are a lot of filters dedicated to create a complete markdown documentation with sections like title, menu, description, table, header, footer, install, even donate and contact. Just tap into the desired layout filters and fill in your own content.
The table below gives you details of all the filters.
But before that, you can also do similar things by totally overriding the parent's functions. For example,
readApiNamecan be overridden to extend or replace existing logic to detect a function name with your own logic. However, filters help in changing things in a granular way. There can be 10 filters applied to a single function you wan to override and you may need to just use one filter out of them instead of writing a big bunch of overriding code. Of course, choice is yours.
| Filter Name | Description |
|---|---|
| Name | API function name of a single Endpoint |
| Slug | A slug derived from API's name defined in Postmam |
| FinalName | API function name after all logic has been applied |
| Params | Api function parameters |
| Uri | Endpoint |
| Operation | Opetaion data of a single endpoint for Guzzle Description |
| Title | Title of the Postman collection |
| DocMethodFinalText | phpDoc method as joined text |
| DocMDItem | Markdown API for single endpoint details as array |
| DocMDApiExternalLink | Markdown's placeholder for link to external (API service provider's) API Documentation |
| DocMDEndpoint | Endpoint of API item in Markdown documentation |
| DocMDDescription | Description of API item in Markdown documentation |
| DocMDParams | Parameters of API Item Markdown documentation |
| DocMDParamDefaults | Default values for parameters of API item Markdown documentation |
| DocMDLayoutTitle | Markdown documentation title |
| DocMDLayoutSubtitle | Markdown documentation subtitle |
| DocMDLayoutHeader | Markdown documentation header |
| DocMDLayoutMenu | Markdown documentation menu |
| DocMDLayoutDescription | Markdown documentation description |
| DocMDLayoutRequirements | Markdown documentation requirements section |
| DocMDLayoutInstall | Markdown documentation install section |
| DocMDLayoutGetStarted | Markdown documentation gettting started section |
| DocMDLayoutSetup | Markdown documentation setup section |
| DocMDLayoutExamples | Markdown documentation examples section |
| DocMDLayoutTable | Markdown documentation API table section - helper data can be iterated to get data in this structure -[ 'method' => '\<function name\>', 'endpoint' => '\<endpoint uri\>', 'parameters' => [<indexed array>], 'defaults' => ['<associative array>'], 'description' => '<api description>'] |
| DocMDLayoutFooter | Markdown documentation footer section |
| DocMDLayoutNotes | Markdown documentation notes section |
| DocMDLayoutReferences | Markdown documentation references section |
| DocMDLayoutContact | Markdown documentation contact us section |
| DocMDLayoutDonate | Markdown documentation donate section |
| DocMDFinalLayoutArray | All Markdown documentation layouts as array |
| DocMDFinalText | Full Markdown documentation as text |
| APIDefinitionFinalJson | The full Guzzle Description |
| DocMethodItem | phpDoc method for single endpoint |
| DocMethodDescription | Markdown documentation |
| DocMethodFinalArray | All phpDoc methods as array |
| DocMethodParams | Parameters for phpDoc |
| DocMethodData | What needs to shown in the parenthesis of the method signature in phpDoc |
| DocMethodSignature | Full single method signature for phpDoc |
| GroupName | Postman Collection's Group Name |
| GroupDescription | Post Collection's Description |
sudiptochoudhury/php-api-client-forge 适用场景与选型建议
sudiptochoudhury/php-api-client-forge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.44k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「rest」 「api」 「client」 「create」 「forge」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sudiptochoudhury/php-api-client-forge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sudiptochoudhury/php-api-client-forge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sudiptochoudhury/php-api-client-forge 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Alfabank REST API integration
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
BlockCypher's PHP SDK for REST API
Helper classes for creating cookie headers
统计信息
- 总下载量: 16.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 5
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-14