承接 exeque/guzzle-spy 相关项目开发

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

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

exeque/guzzle-spy

最新稳定版本:0.2.0

Composer 安装命令:

composer require exeque/guzzle-spy

包简介

Spy middleware for Guzzle

README 文档

README

This middleware is designed to be used with Guzzle. It allows you to spy on requests and responses made by Guzzle. This is useful for testing and logging.

It is a simple middleware that reports request and response information to a spy class.

Installation

You can install Guzzle Spy Middleware using Composer:

composer require exeque/guzzle-spy

Usage

Basic Usage

Guzzle Spy Middleware can be used by adding it to a Guzzle client.

use ExeQue\Guzzle\Spy\Middleware;
use ExeQue\Guzzle\Spy\Spy;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

$middleware = new Middleware(
    $spy = new Spy(
        fn(
            string $id, 
            RequestInterface $request, 
            array $options,
        ) =>  ..., // Before the request is sent
        fn(
            string $id, 
            ResponseInterface $response, 
            RequestInterface $request, 
            array $options,
        ) => ..., // After the response is received
    ),
);
// or
$middleware = Spy::middleware(
    fn(
        string $id, 
        RequestInterface $request, 
        array $options,
    ) =>  ..., // Before the request is sent
    fn(
        string $id, 
        ResponseInterface $response, 
        RequestInterface $request, 
        array $options,
    ) => ..., // After the response is received
)

$handler = HandlerStack::create();

// It's recommended to use the spy
// as the last middleware in the stack.
// It will work regardless of where it is placed, 
// but any other middleware that modifies the request or response
// will affect the data that is reported to the spy.
$handler->push($middleware, 'spy');

$client = new Client(['handler' => $handler]);

Custom Spy

You can create a custom spy by implementing the ExeQue\Guzzle\Spy\Contracts\Spy interface.

use ExeQue\Guzzle\Spy\Contracts\Spy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class TimeLoggingSpy implements Spy
{
    private array $timers = [];

    public function __construct(
        private \Psr\Log\LoggerInterface $logger,
    ) {}

    public function before(string $id, RequestInterface $request, array $options): void {
        $this->timers[$id] = microtime(true);
    }
    
    public function after(string $id, ResponseInterface $response, RequestInterface $request, array $options): void {
        $time = round(microtime(true) - $this->timers[$id], 4);
        unset($this->timers[$id]);
        
        $uri = "{$request->getUri()->getHost()}/{$request->getUri()->getPath()}";
        
        $message = "{$request->getMethod()}@{$uri} Request took {$time} seconds";
        
        $this->logger->info($message);
    }
}

If you need more fine-grained control over the request id, you can implement the ExeQue\Guzzle\Spy\Contracts\CanCreateRequestIds interface.

The createRequestId method is called before the request is sent. It is passed the request and options array, and it should return a unique identifier for the request.

use ExeQue\Guzzle\Spy\Contracts\CanCreateRequestIds;
use ExeQue\Guzzle\Spy\Contracts\Spy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Ramsey\Uuid\Uuid;

class CustomSpy implements Spy, CanCreateRequestIds
{
    public function before(string $id, RequestInterface $request, array $options): void {
        ...
    }
    
    public function after(string $id, ResponseInterface $response, RequestInterface $request, array $options): void {
        ...
    }
    
    public function createRequestId(RequestInterface $request, array $options): string {
        return (string)Uuid::uuid4();
    }
}

Testing

You can run the tests using Pest:

composer test

License

Dedent is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-12