承接 mtrn/apiservice 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mtrn/apiservice

Composer 安装命令:

composer require mtrn/apiservice

包简介

api service: get api response and map it into any object of your choice.

README 文档

README

Latest Version on Packagist Total Downloads Build Status StyleCI

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 mtrn/apiservice 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-08