Created
September 7, 2021 12:52
-
-
Save barryvdh/a04ab1628558d6ac41a036a238ee090b to your computer and use it in GitHub Desktop.
Middleware to log Guzzle requests with Telescope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$stack = \GuzzleHttp\HandlerStack::create(); | |
$stack->push(new \App\Http\Client\HttpClientEventsMiddleware()); | |
$client = new \GuzzleHttp\Client(['handler' => $stack]); | |
$client->get('http://example.com'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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; }; }