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

Make auth middleware inherit "parent" handler ? #2345

Closed
geoffrey-brier opened this issue Dec 16, 2022 · 3 comments
Closed

Make auth middleware inherit "parent" handler ? #2345

geoffrey-brier opened this issue Dec 16, 2022 · 3 comments

Comments

@geoffrey-brier
Copy link
Contributor

Hello,

I have been trying to mock google API calls via the recommended way in Guzzle and I could'nt understand why it would not work until I stumbled upon this part of the code:

https://github.com/googleapis/google-api-php-client/blob/main/src/AuthHandler/Guzzle6AuthHandler.php#L106

And that's when I realized that if you have a custom stack handler which is the case when mocking, then the stack handler is not shared at all with the auth middleware stack handler.

As of today, to make it work I overrode the GoogleClient::getAuthHandler method to return a custom GuzzleAuthHandler which is basically a copy of the Guzzle6AuthHandler with one line updated in the createAuthHttp method:

    private function createAuthHttp(ClientInterface $http)
    {
        return new Client([
            'base_uri' => $http->getConfig('base_uri'),
            'http_errors' => true,
            'verify' => $http->getConfig('verify'),
            'proxy' => $http->getConfig('proxy'),
            // That's the line we're talking about
            'handler' => $http->getConfig('handler')
        ]);
    }

That works but that's a lot of code which is (let's be honest) not that clean 😅

Is there any alternatives ? What would you think about adding this line during the http client creation ?

NB: My problematic concerns mocking but I guess that would also be true if you depend on another specific handler.

@bshaffer
Copy link
Contributor

This seems like a fine addition to me. The only reason this exists is because we want to ensure http_errors is set to true for auth calls. So if there's a better way to do that in Guzzle, we could do that also. Something like this might work:

    private function createAuthHttp(ClientInterface $http)
    {
        return new Client(['http_errors' => true] + $http->getConfig());
    }

@geoffrey-brier
Copy link
Contributor Author

I don't think there's another way of doing so in guzzle, but I like your idea! Submitting a patch 🚀

@bshaffer
Copy link
Contributor

added!

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

No branches or pull requests

2 participants