saloonphp / saloon

🤠 Build beautiful API integrations and SDKs with Saloon
https://docs.saloon.dev
MIT License
2.03k stars 105 forks source link

Override Connector defaultHeaders() in request #430

Closed jlevers closed 2 months ago

jlevers commented 2 months ago

Hi @Sammyjo20,

Thanks for all your awesome work on this package! I've switched over jlevers/selling-partner-api to be fully Saloon-based.

I'm trying to remove some headers from my OAuth requests, but I can't figure out how to prevent the connector's defaultHeaders() from being merged with my request's defaultHeaders(). I see that the merge happens in MergeRequestProperties, but since that's only accessed by PendingRequest, I haven't found a great way to get around it.

Is there some way to force the headers to be a certain value from inside a request class?

Thanks!

Sammyjo20 commented 2 months ago

Hey @jlevers thats awesome news, nice work on migrating it all over!

Would you be able to show me your defaultHeaders on your request, please? The way it's meant to work is the request's properties will always be merged after the connectors, so that they will always take priority.

I'm wondering if there's either a bug, or an opportunity to introduce a new interface that prevents merging headers, body etc for these use cases.

juse-less commented 2 months ago

@jlevers It sounds like your issue might be the same as #398. Although that issue mentions plugins rather than the Request itself.

Not the most convenient way, but I wonder if you could maybe try adding a middleware as late as possible and modify the PendingRequest that way.

I know @Sammyjo20 already mentions this not working in #398, but maybe try to use a plugin anyway.

Worst case you could maybe add the plugin/middleware on the Connector instead, and check the type of the Request the PendingRequest is for, and, if the correct Request, modify the headers on the PendingRequest.

Edit: I wonder if it's not enough to just use the boot() method on the Request, as shown in the middleware link, but just reference the headers directly. Then unset headers you don't want. If you're certain that only the specific Requests defaultHeaders() should be used, you could reset the headers object on the PendingRequest and grab the headers from the Requests defaultHeaders() method.

jlevers commented 2 months ago

@Sammyjo20 thanks for the response :)

I don't have any defaultHeaders() method defined on the request, because I'm not really trying to overwrite the connector's headers – I just want the connector's headers removed entirely. I tried making the request's default headers return an empty array, but that didn't work.

@juse-less good call, middleware should work. I'd like to avoid that and set the headers in the request class if possible, but if that isn't doable then I'll go the middleware route.

juse-less commented 2 months ago

@jlevers typing this on my phone, so bear with me.

I think you could do something like this:

class MyRequest extends Request
{
    public function boot(PendingRequest $request): void
    {
        $defaultHeaders = array_keys($request->getConnector()->headers()->all());

        foreach ($defaultHeaders as $defaultHeader) {
            $request->headers()->remove($defaultHeader);
        }
    }
}
jlevers commented 2 months ago

Ah perfect, that worked! Thanks so much, appreciate it.