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

Abstract request handler logic to Http class #4

Merged
merged 3 commits into from
Sep 24, 2024

Conversation

Thavarshan
Copy link
Owner

Purpose

The purpose of this PR is to enhance the fetch.php library by adding a new Http class that improves the handling of HTTP requests, particularly by addressing the Guzzle client instantiation issue. Previously, the Guzzle client was being instantiated on every request, leading to performance inefficiencies. This PR implements a singleton pattern for the Guzzle client to ensure it is instantiated only once and reused across requests.

Approach

This change addresses the problem by introducing the following:

  1. Http Class:
    • A new class that centralizes the logic for handling HTTP requests using Guzzle.
    • It includes methods for synchronous (fetch) and asynchronous (fetchAsync/fetch_async) HTTP requests.
  2. Guzzle Singleton Implementation:
    • The Guzzle client is now instantiated only once and reused for all subsequent HTTP requests.
    • This is done using a static getClient() method that checks if the client has already been created, preventing unnecessary re-instantiation on each request.
  3. Custom Client Support:
    • Added the ability to pass a custom Guzzle client through the options array, allowing for further customization when needed, without altering the singleton instance.

Open Questions and Pre-Merge TODOs

  • Verify that the singleton Guzzle client is correctly reused across multiple HTTP requests.
  • Ensure custom Guzzle clients can be passed via the options array and that they work without overriding the global singleton client.
  • Test both synchronous and asynchronous requests to confirm that the correct client behavior is applied in each case.

Learning

The research stage included reviewing Guzzle’s documentation to understand its client instantiation process and best practices for optimizing HTTP requests in PHP. Additionally, the solution is inspired by common patterns used to implement singletons in PHP while ensuring flexibility for users who need custom Guzzle clients.

@Thavarshan Thavarshan added the enhancement New feature or request label Sep 24, 2024
@Thavarshan Thavarshan self-assigned this Sep 24, 2024
@Thavarshan Thavarshan merged commit ba67f9b into main Sep 24, 2024
4 checks passed
@Thavarshan Thavarshan deleted the feature/request-handler branch September 25, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant