fnayou/zeppelin

此包已被废弃且不再维护。未建议替代包。

使用客户端配置和API描述文件简单配置Guzzle的包。

1.0.3 2016-09-07 09:10 UTC

This package is auto-updated.

Last update: 2024-06-12 03:52:53 UTC


README

[已废弃且不再维护]

简单 Guzzle 配置器,使用 客户端配置API描述 文件。

  1. 您只需要创建两个文件
    • guzzle客户端配置(API URL、版本、用户代理等)
    • API描述结构
  2. 使用ZeppelinFactory工厂或创建自己的
  3. 消费API

Zeppelin旨在通过提供简单易用的方式来消费API和Web服务,让您能够专注于主要项目。

安装

您可以使用composer安装zeppelin

$ composer require fnayou/zeppelin

用法

  • 根据配置示例文件创建您的Guzzle客户端配置文件
  • 根据API描述示例文件创建您的API描述文件
  • 使用您喜欢的LoaderZeppelinFactory工厂(即将推出更多加载器)
<?php

use Fnayou\Zeppelin\Factory\ZeppelinFactory;
use Fnayou\Zeppelin\Loader\YamlLoader;

$yamlLoader = YamlLoader();
$configurationFilePath = '/path/to/api_configuration.yml';
$customGuzzleConfiguration = [];

$client = ZeppelinFactory::build($yamlLoader, $configurationFilePath, $customGuzzleConfiguration);
  • 消费您的API
<?php

// you can access api as method (according to the api description file)
try {
    $response = $client->user([
        'X-APPID' => '26041986',
        'user_id' => 26,
    ]);
} catch (\GuzzleHttp\Exception\RequestException $e) {
    $response = json_decode($e->getResponse()->getBody()->getContents());
}

dump($response);
  • 您也可以创建自己的带有自定义逻辑的factory,您只需要实现FactoryInterface
<?php

namespace Vendor\App\Factory;

use Fnayou\Zeppelin\ApiClient;
use Fnayou\Zeppelin\Api\ApiDescription;
use Fnayou\Zeppelin\Factory\FactoryInterface;
use Fnayou\Zeppelin\Loader\LoaderInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;

class CustomFactory implements FactoryInterface
{
    public static function build(LoaderInterface $loader, $filePath, array $config)
    {
        // load the guzzle configuration file
        $apiClientConfiguration = new ApiClient($loader, $filePath);

        // load the api description file 
        $apiDescription = new ApiDescription(
            $loader,
            $apiClientConfiguration->getDescriptionFilePath()
        );

        $client = new Client($apiClientConfiguration->getClientConfiguration());

        // your guzzle instance using api description, and custom configuration 
        $zeppelin = new GuzzleClient(
            $client,
            $apiDescription->getDescription(),
            $config
        );

        return $zeppelin;
    }
}

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

致谢

许可

MIT许可(MIT)。请参阅许可文件获取更多信息。