承接 webignition/http-history-container 相关项目开发

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

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

webignition/http-history-container

Composer 安装命令:

composer require webignition/http-history-container

包简介

A container for Guzzle history middleware

README 文档

README

Helping you more easily test what your Guzzle HTTP client has been up to.

A container for Guzzle history middleware offering smashingly-nice access to:

  • collections of HTTP transactions (requests plus responses)
  • requests made
  • responses received
  • request URLs

Oh also logging. Really useful when your Guzzle client under test is not executing within the same thread as your tests.

Usage

Basic Usage

<?php

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use webignition\HttpHistoryContainer\Container;

$historyContainer = new Container();
$historyHandler = Middleware::history($historyContainer);
$handlerStack = HandlerStack::create($historyHandler);
$httpClient = new HttpClient(['handler' => $handlerStack]);

/// ... things happen ...

$historyContainer->getTransactions();
// an array of HttpTransaction

$historyContainer->getRequests();
// a Collection\RequestCollection

$historyContainer->getResponses();
// a Collection\ResponseCollection

foreach ($historyContainer as $transaction) {
    // $transaction is a HttpTransaction

    $request = $transaction->getRequest();
    // $request is a RequestInterface

    $response = $transaction->getResponse();
    // $response is a ResponseInterface
}

Details on Requests Sent

<?php

use webignition\HttpHistoryContainer\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

$historyContainer = new Container();

// ... create and use Guzzle client ...

$transactions = $historyContainer->getTransactions();
// $transactions is a Collection\TransactionCollection

$requests = $transactions->getRequests();
// $requests is a Collection\RequestCollection

// RequestCollection is iterable
foreach ($requests as $request) {
    // $request is a RequestInterface
}

$urls = $requests->getUrls();
// $urls is a Collection\UrlCollection

// UrlCollection is iterable
foreach ($urls as $url) {
    // $url is a UriInterface
}

foreach ($urls->getAsStrings() as $urlString) {
    // convenient access to requested URLs as strings
}

Details on Responses Received

<?php

use webignition\HttpHistoryContainer\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

$historyContainer = new Container();

// ... create and use Guzzle client ...

$transactions = $historyContainer->getTransactions();
// $transactions is a Collection\TransactionCollection

$responses = $transactions->getResponses();
// $responses is a Collection\ResponseCollection

// ResponseCollection is iterable
foreach ($responses as $response) {
    // $response is a ResponseInterface
}

Logging!

You may not be able to access your Guzzle history if the client under test is executing in a different thread from your tests.

A LoggableContainer allows you to record transactions as they happen to whatever Psr\Log\LoggerInterface instance you like.

The logging container wraps each HttpTransaction in a LoggableTransaction object which is serialized to JSON and output as a debug message.

LoggableTransactionFactory::createFromJson() lets you (in a somewhat slightly lossy manner) re-create transactions object from logged messages.`

Logging Example

<?php

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use webignition\HttpHistoryContainer\LoggableContainer;
use webignition\HttpHistoryContainer\Factory\LoggableTransactionFactory;

// Create the PSR logger
$path = '/path/to/log';
$stream = fopen($path, 'w+');

$logger = new Logger('');
$logHandler = new StreamHandler($stream);
$logHandler
    ->setFormatter(new LineFormatter('%message%' . "\n"));

$logger->pushHandler($logHandler);

// Create LoggableContainer
$container = new LoggableContainer($logger);

// ... create and use Guzzle client ...

$logContent = file_get_contents($path);
$logLines = array_filter(explode("\n", $logContent));

$loggedTransactions = [];
foreach ($logLines as $logLine) {
    $loggedTransactions[] = LoggableTransactionFactory::createFromJson($logLine);
}

// $loggedTransactions is now an array of LoggableTransaction
foreach ($loggedTransactions as $loggedTransaction) {
    $transaction = $loggedTransaction->getTransaction();    
    // $transaction is a HttpTransaction

    $request = $transaction->getRequest();
    // $request is a RequestInterface

    $response = $transaction->getResponse();
    // $response is a ResponseInterface
}

webignition/http-history-container 适用场景与选型建议

webignition/http-history-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 90.93k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「container」 「Guzzle」 「middleware」 「history」 「psr-7」 「guzzlehttp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 webignition/http-history-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 webignition/http-history-container 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 90.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 31
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-02