mtrn/apiservice
Composer 安装命令:
composer require mtrn/apiservice
包简介
api service: get api response and map it into any object of your choice.
README 文档
README
This package pdrovides an api-service to ease working with different apis. It maps api-responses to objects of your choice by using a trait named IsApiClient. In this way you are free to define different clients for a single api. You'r also able to use diffrent apis for a single client.
let's get a look at structur:
├── configs
│ └── apiservice.php
├── app
│ └── Models
│ └── Client.php
│ └── Services
│ └── ApiService
│ └── ApiProviders
│ └── GoogleApiProvider.php
│ └── Decorators
│ └──GoogleClientDecorator.php
Configs
The package's config-file is apiservice.php. Its options are:
apis:
This is an associative array where keys are the name of apis and values are array of configs for the api.
Each api has three attributes:
url: the api's url
response_type: the type of api response (currently supported type is json)
data_access_keys: it is an associative array where a key is client-name and its value is access-key to the client-related-data.
- default access-key would be the name of the client; like 'user' => 'user'.
- 'user' => ' ' indicates that the whole response-body is related to the client user.
defaults:
this option controls the default. You're free to change them as you want. Its only value it response_type which is json.
path_to_decorators:
This value is used to access decorators.
path_to_apiproviders:
This value is used to access to api-providers.
Client.php
This is an example client object which get use of trait IsApiClient.
It must implement function getMappedArray() to present its data in array format.
ApiProviders
All your api-provider classes should be placed in this directory.
An api-provider is used to handle api related functionalities; like sending a request to the api, or choosing a way to extract data from response based on its type.
For each one of your apis you should create an api-provider and name it in format ApinameApiProvider.
GoogleApiProvider is an example of api-provider.
Note: They must extend ApiProvider abstract and implement method requestFromApi().
Decorators
All your decorator classes should be placed in this directory.
A decorator is used to decorate your clients. The nameing format is ApinameClientnameDecorator.
Note: They must extend Decorator abstract and implement method mapApiData();
Installation
Via Composer
$ composer require mtrn/apiservice
Usage
lets take a look at the provided example.
GoogleApiProvider
use Mtrn\ApiService\Services\ApiService\ApiProviders\ApiProvider;
class GoogleApiProvider extends ApiProvider
{
public function requestFromApi()
{
return Http::get($this->url);
}
}
Client
use Mtrn\ApiService\Traits\IsApiClient;
class Client
{
use IsApiClient
public function getMappedArray()
{
return $this->toArray();
}
}
Decorator
use Mtrn\ApiService\Services\ApiService\Decorators\Decorator;
class GoogleClientDecorator extends Decorator
{
/**
* Define your map rules here.
*
* @param array $data
* @return object
*/
public function mapApiData(array $data): object
{
$this->client->setAttribute('name', $data['first_name'].' '.$data['last_name']);
return $this->client;
}
}
request to google and get response object:
$client = new Client();
$response = $client->requestFromApi($apiname='google', $map=false);
request to google and get extracted data
$client = new Client();
$client->requestFromApi($amiName='google', $map=false);
$extractedData = $client->getApiBody();
request to google and get mapped data object
$client = new Client();
$mappedData = $client->requestFromApi($apiname='google', $map=true);
request to google and get mapped array
$client = new Client();
$client->requestFromApi('google', true);
$mappedArray = $client->getMappedArray();
mtrn/apiservice 适用场景与选型建议
mtrn/apiservice 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「apiservice」 「ApiResponseMapper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mtrn/apiservice 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mtrn/apiservice 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mtrn/apiservice 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
API Service to work with DotKernel
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-08