Skip to content

[7.x] Tap into Http::get() even better#33315

Merged
taylorotwell merged 1 commit into
laravel:7.xfrom
hannesvdvreken:7.x
Jun 24, 2020
Merged

[7.x] Tap into Http::get() even better#33315
taylorotwell merged 1 commit into
laravel:7.xfrom
hannesvdvreken:7.x

Conversation

@hannesvdvreken

Copy link
Copy Markdown
Contributor

Long time no see, friends. 👋

The Laravel HTTP client (accessible through the Http facade) uses an underlying Guzzle Client (and allows to change some of its settings). But Guzzle has a feature called middleware which provides infinite flexibility to add/inspect requests and responses going to or coming from the HTTP client. Why not allow to tap into that?

This method allows to wrap additional user-created middleware to that HandlerStack already initiated.

Example: profiling HTTP requests to a profiler like DebugBar or Clockwork

$client = new \Illuminate\Http\Client\PendingRequest();

$debugBar = new \DebugBar\StandardDebugBar();

// Get data collector.
$timeline = $debugBar->getCollector('time');

// Wrap the timeline.
$profiler = new \GuzzleHttp\Profiling\Debugbar\Profiler($timeline);

$client->withMiddleware(new \GuzzleHttp\Profiling\Middleware($profiler));

$client->send('GET', 'https://httpbin.org/status/200');

var_dump($timeline->collect());

Bonus: it's future proof (Guzzle v7 will be out soon) but middleware will remain the same.


cc @barryvdh @chrisrhymes @driesvints

ref https://twitter.com/chrisrhymes/status/1275378980137426950

@GrahamCampbell GrahamCampbell changed the title Tap into Http::get() even better [7.x] Tap into Http::get() even better Jun 24, 2020
public function withMiddleware(callable $middleware)
{
return tap($this, function ($request) use ($middleware) {
return $this->middleware = collect($this->middleware)->push($middleware);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return $this->middleware ... wouldn't have any effect for a tap() callback since the return value is ignored.

Either way, this method looks much simpler by skipping tap():

$this->middleware = collect($this->middleware)->push($middleware);

return $this;

@taylorotwell taylorotwell merged commit 04694bb into laravel:7.x Jun 24, 2020
@MrRio

MrRio commented Jul 7, 2020

Copy link
Copy Markdown

This looks pretty familiar 😬 #32170

@driesvints

Copy link
Copy Markdown
Member

@MrRio sometimes we reconsider prs if there's enough demand for it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants