Skip to content

[8.x] Add Http::configure() method #40332

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

Closed
wants to merge 5 commits into from
Closed

[8.x] Add Http::configure() method #40332

wants to merge 5 commits into from

Conversation

ryangjchandler
Copy link
Contributor

This pull request introduces a new Http::configure() method that can be used to modify all PendingRequest objects globally.

class AppServiceProvider extends ServiceProvider
{
    public function boot() {
        Http::configure(function (PendingRequest $request) {
            $request->withHeaders(['custom' => 'yes!']);
        });
    }
}

In this scenario, all requests made from the app will have this custom header applied.

@taylorotwell
Copy link
Member

Does it make sense for this to be invoked right before the request is actually sent? That way you could inspect other information about the request before configuring it further - for example, what if you only wanted to add some headers if the URI matched a given value?

@ryangjchandler
Copy link
Contributor Author

ryangjchandler commented Jan 10, 2022

@taylorotwell Yeah, fair point. My personal use case was for all requests, regardless of URL but you're right, that would make a lot more sense. It's getting late here so I'll update this tomorrow if that's okay?

@taylorotwell
Copy link
Member

OK

@taylorotwell taylorotwell marked this pull request as draft January 10, 2022 22:33
@ryangjchandler ryangjchandler marked this pull request as ready for review January 11, 2022 15:03
@ryangjchandler
Copy link
Contributor Author

@taylorotwell I've updated the logic to configure the request at the top of the PendingRequest::send method. Wasn't sure whether it should be at the start of that method or the PendingRequest::sendRequest method.

@taylorotwell
Copy link
Member

taylorotwell commented Jan 11, 2022

Hmm - I dunno, something about this still seems kinda clunky to use. How do I inspect the request for a header or look at its final URI? How can I see what data it has?

@ryangjchandler
Copy link
Contributor Author

Happy to close this and leave it be if you think it's not worth it. There are user-land workarounds like constructing a base client that has the correct options already.

Without dedicated methods on the request object to inspect the Guzzle options, there's no easy way to do the things you mentioned.

@taylorotwell
Copy link
Member

taylorotwell commented Jan 12, 2022

Yeah - here's how I would do this:

In service provider:

Http::macro('github', function () {
    return Http::withHeaders([
        'X-First' => 'foo',
    ])->baseUrl('https://github.com');
});

In code:

Http::github()->get('/');

May document this 🤔

@ryangjchandler
Copy link
Contributor Author

ryangjchandler commented Jan 12, 2022

@taylorotwell Yeah, this is an alternative approach. Thanks for taking a look though! :)

I guess the only drawback here is that lack of autocomplete/intellisense, but easily fixed with ide-helper or a stubs file.

@taylorotwell
Copy link
Member

Embrace the dynamic language ❤️

@dammy001
Copy link
Contributor

🔥🔥🔥

@ryangjchandler ryangjchandler deleted the feature/http-client-configure-callback branch January 12, 2022 16:32
@frknasir
Copy link

Yeah - here's how I would do this:

In service provider:

Http::macro('github', function () {
    return Http::withHeaders([
        'X-First' => 'foo',
    ])->baseUrl('https://github.com');
});

In code:

Http::github()->get('/');

May document this 🤔

Rad!

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.

4 participants