Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 24, 2020
Merged

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

merged 1 commit into from
Jun 24, 2020

Conversation

hannesvdvreken
Copy link
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
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
Copy link

MrRio commented Jul 7, 2020

This looks pretty familiar 😬 #32170

@driesvints
Copy link
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