Skip to content

Instantly share code, notes, and snippets.

@barryvdh
Created September 7, 2021 12:52
Show Gist options
  • Save barryvdh/a04ab1628558d6ac41a036a238ee090b to your computer and use it in GitHub Desktop.
Save barryvdh/a04ab1628558d6ac41a036a238ee090b to your computer and use it in GitHub Desktop.
Middleware to log Guzzle requests with Telescope
<?php
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(new \App\Http\Client\HttpClientEventsMiddleware());
$client = new \GuzzleHttp\Client(['handler' => $stack]);
$client->get('http://example.com');
<?php
namespace App\Http\Client;
use GuzzleHttp\Promise;
use Illuminate\Http\Client\Events\ConnectionFailed;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\Response;
class HttpClientEventsMiddleware {
public function __invoke(callable $handler)
{
return function ($request, array $options) use ($handler) {
$laravelRequest = new Request($request);
event(new RequestSending($laravelRequest));
return $handler($request, $options)->then(
$this->onSuccess($laravelRequest),
$this->onFailure($laravelRequest)
);
};
}
protected function onSuccess(Request $request)
{
return function ($response) use ($request) {
event(new ResponseReceived($request, new Response($response)));
return $response;
};
}
protected function onFailure(Request $request)
{
return function ($reason) use ($request) {
event(new ConnectionFailed($request));
return Promise\Create::rejectionFor($reason);
};
}
}
@jameshulse
Copy link

@emergingdzns, if they don't allow you to pass in a guzzle instance then it may not be possible. There is a default middleware stack that you could mess with - but sometimes it is just easier to take the fork of the package. We do this with a few smaller packages to take control of the network stack.

@akrez
Copy link

akrez commented Feb 19, 2025

Since I'm working with Laravel 11, ConnectionFailed requires two parameters, so I changed the code to this:

<?php

namespace App\Http\Client;

use GuzzleHttp\Promise;
use Illuminate\Http\Client\Events\ConnectionFailed;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\Response;

class HttpClientEventsMiddleware {
{
    public function __invoke(callable $handler)
    {
        return function ($request, array $options) use ($handler) {

            $laravelRequest = new Request($request);

            event(new RequestSending($laravelRequest));

            return $handler($request, $options)->then(
                $this->onSuccess($laravelRequest),
                $this->onFailure($laravelRequest)
            );
        };
    }

    protected function onSuccess(Request $request)
    {
        return function ($response) use ($request) {
            event(new ResponseReceived($request, new Response($response)));

            return $response;
        };
    }

    protected function onFailure(Request $request)
    {
        return function ($e) use ($request) {
            event(new ConnectionFailed($request, new ConnectionException($e->getMessage(), $e->getCode(), $e)));

            return Promise\Create::rejectionFor($e);
        };
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment