Skip to content

Update to Guzzle 6+ #1146

@swissspidy

Description

@swissspidy

Feature Description

As mentioned a few times, most recently again in #223.

Perhaps it could just be bumped? google/apiclient supports it, so there shouldn't be a conflict in that regard.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • guzzlehttp/guzzle should be upgraded to the latest v6 version (but not v7)
    • All existing Guzzle client customizations must continue to work (see Client_Factory::create_client)

Implementation Brief

  • A working branch has been started in https://github.com/google/site-kit-wp/tree/enhance/1146-guzzle6 with a draft PR in Upgrade Guzzle to v6 #6581
  • Most of the exploration to understand what is needed here should be done, the main part remaining is refactoring the use of FakeHttpClient with Guzzle's MockHandler
    • The reason for this is because staring with Guzzle v6, the Client is designed to be immutable, so in order to make changes, a new instance must be set/returned. Because of this, an alternate client instance is lost as soon as such a modification is needed, and since this happens within the Google API client as part of every request when providing the access_token, it's no longer a viable strategy.
    • The MockHandler for overriding requests in tests will persist across Client instances via the handler request option.
  • Replace the use of FakeHttpClient with a MockHandler via the Guzzle client's handler configuration. See https://docs.guzzlephp.org/en/6.5/testing.html for details
    • This is not a 1-1 replacement as a MockHandler serves requests in order from a given queue whereas FakeHttpClient has full control over the response returned so we may want to look into extending the MockHandler to allow for conditional responses rather than a simple queue (a quick search yielded a package that provides something similar so it should at least be possible, or we can maybe even just use this one)
    • The flow to mock one or more responses generally looks like this
      • Get the Guzzle config from the Google API's http client (Google_Client->getHttpClient()->getConfig())
      • Modify the config to include the configured handler
      • Create a new Client using the new/modified config array
      • Set the new Client back in the Google API client (Google_Client->setHttpClient(newClientInstance))
  • Remove FakeHttpClient

QA Brief

  • This issue changes the main underlying library used for making API requests with Google APIs, including oAuth token requests
  • Setup flows and dashboard views should be thoroughly checked for potential regressions
  • We should test that things work on both oldest (5.6) and latest (8.0/8.1) supported versions of PHP, as well as PHP 7.4 (being the most common)

QA:Eng

  • Ensure HTTP proxy functionality still works
  • Ensure user-agent and other customizations to the client still work

Changelog entry

  • Upgrade Guzzle, with guzzlehttp/guzzle updated to v6.5.8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Exp: SPP1Medium priorityPHPQA: EngRequires specialized QA by an engineerType: InfrastructureEngineering infrastructure & tooling

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions