Skip to content

Instantly share code, notes, and snippets.

@barryvdh
Created September 7, 2021 12:52
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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

jameshulse commented Mar 21, 2024

Exactly what I was looking for! Barry strikes again with a real gem 👍

We have to pass a Guzzle instance to an external library but we don't get tracking with the Laravel Sentry integration. This should fix that for us. Thanks.

Edit: There is a caveat to be aware of. If an event handler reads the body of the response then your actual code that was using the Guzzle client in the first place will get an empty response body. This is due to the body being a stream that can only be read once. I'm not sure of the best solution, but I ended up introducing this change:

protected function onSuccess(Request $request)
{
    return function ($response) use ($request) {

        event(new ResponseReceived($request, new Response($response)));

+       $response->getBody()->rewind(); // Rewind the body so that the caller can read it again

        return $response;
    };
}

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