marcus-campos/maestro

v1.8.0 2017-11-01 18:27 UTC

This package is auto-updated.

Last update: 2024-09-15 10:48:59 UTC


README

Build Status Maintainability MIT licensed

Maestro - PHP 的 Http 客户端

基于 Guzzle 的轻量级客户端,简化了与微服务交互的方式。它基于方法定义和 URL 参数。

要求

  • PHP 7.0 或更高版本
  • Composer

安装

Composer 方式

composer require marcus-campos/maestro

或手动添加到您的 composer.json 文件中

"marcus-campos/maestro": "dev-master"

如果您使用 Laravel

服务提供者(在 Laravel 5.5 中可选)

一旦 Composer 安装或更新了您的包,您需要将别名或注册您的包添加到 Laravel 中。打开 config/app.php 并找到 aliases 键,然后添加

'Maestro' => Maestro\Laravel\MaestroLaravelFacade::class,

运行测试套件

使用 Docker

docker build -t maestro .
docker run maestro

基本用法

<?php

namespace App\Platform\V1\Domains\App;


use Maestro\Rest;

class Products extends Rest
{
    protected $url = 'https://mydomain.com/api/v1/'; // http://mydomain.com:9000/api/v1

    /**
     * @return array
     */
    public function getProducts()
    {
        return $this
            ->get()
            ->setEndPoint('products')
            ->headers([
                'Content-Type' => 'application/json'
            ])
            ->body( [
                'ids' => [1,2,3,4,5]
            ])
            ->send()
            ->parse();
    }

    /**
     * @return array
     */
    public function getProduct()
    {
        return $this
            ->get()
            ->setEndPoint('product')
            ->send()
            ->parse();
    }

    /**
     * @return array
     */
    public function postNotification()
    {
        return $this
            ->get()
            ->setEndPoint('package/')
            ->headers([
                'Content-Type' => 'application/json'
            ])
            ->body([
                'message' => 'Tanks for all.',
                'id' => [1]
            ])
            ->sendAsync()
            ->wait()
            ->parse()
            ->name;
    }
}

响应数据

默认情况下,返回的是 StdClass,这使您能够根据需要处理数据。请参阅示例

 public function getProducts()
 {
     return
         $this
         ->get()
         ->setEndPoint('products')
         ->headers([
             'Content-Type' => 'application/json'
         ])
         ->body([
             'ids' => [1,2,3,4,5]
         ])
         ->send()
         ->parse();
 }

您可以选择关联返回。返回将是数组。

 public function postNotification()
 {
     return $this
         ->get()
         ->setEndPoint('package/')
         ->headers([
             'Content-Type' => 'application/json'
         ])
         ->body([
             'message' => 'Tanks for all.',
             'id' => [1]
         ])
         ->send()
         ->assoc()
         ->parse()['name'];
 }

获取响应状态

 public function postNotification()
 {
     $response = $this
          ->get()
          ->setEndPoint('package/')
          ->headers([
              'Content-Type' => 'application/json'
          ])
          ->body([
              'message' => 'Tanks for all.',
              'id' => [1]
          ])
          ->send();

          if($response->status() === 200) {
                $db->save($response->parse()); // any code to work with response body
          }
 }

其他方式

 public function postNotification()
 {
     return $this
         ->get()
         ->setEndPoint('package/')
         ->headers([
             'Content-Type' => 'application/json'
         ])
         ->body([
             'message' => 'Tanks for all.',
             'id' => [1]
         ])
         ->sendAsync()
         ->wait()
         ->parse()
         ->name;
 }

响应缓存

如果微服务的响应在一段时间内很可能是相同的,您可能希望通过缓存响应来礼貌地减少对服务的请求。如果服务施加了请求限制,并且您不想因为不必要的调用而违反它,这也很有用。

您可以使用接受一个时间(以秒为单位)作为参数的 ->cachable() 方法来启用缓存。以下示例将在 60 秒后再次请求之前保持响应

$request = new \Maestro\Rest();

$result = $request
    ->get()
    ->setUrl('http://api.example.com')
    ->setEndPoint('/horses')
    ->cachable(60)
    ->send()
    ->parse();

缓存功能取决于 PHP 包,APCu 已在环境中安装并启用。

注意:如果您在开发过程中意外缓存了长时间的不良响应,只需使用 ->cachable(0) 发送请求以覆盖以前的缓存。

发送者

您可以通过两种方式发送:同步或异步。请参阅示例

同步: ->send()

 public function getProducts()
 {
     return collect($this
         ->get()
         ->setEndPoint('products')
         ->headers([
             'Content-Type' => 'application/json'
         ])
         ->body([
             'ids' => [1,2,3,4,5]
         ])
         ->send()
         ->parse());
 }

异步: ->sendAsync()

 public function postNotification()
 {
     return $this
         ->get()
         ->setEndPoint('package/')
         ->headers([
             'Content-Type' => 'application/json'
         ])
         ->body([
             'message' => 'Tanks for all.',
             'id' => [1]
         ])
         ->sendAsync()
         ->wait()
         ->parse()
         ->name;
 }

支持的方法

  • GET ->get()
  • POST ->post()
  • PUT ->put()
  • PATCH ->patch()
  • DELETE ->delete()
  • COPY ->copy()
  • HEAD ->head()
  • OPTIONS ->options()
  • LINK ->link()
  • UNLINK ->unlink()
  • PURGE ->purge()
  • LOCK ->lock()
  • UNLOCK ->unlock()
  • PROPFIND ->propfind()
  • VIEW ->view()