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

Fix HTTP client handling and configuration in Typesense PHP client #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tharropoulos
Copy link

Change Summary

What is this?

This PR fixes an issue with HTTP client handling in the Typesense PHP client library where
PSR-18 compatible clients were being unnecessarily wrapped and recreated on every request.
This improves performance and provides better compatibility with different HTTP client
implementations.

Changes

Code Changes:

  1. In src/Lib/Configuration.php:

    • Fixed client instance caching to avoid creating new instances on every request
    • Added proper handling for different HTTP client types:
      • Direct use of HttpMethodsClient instances without wrapping
      • Smart wrapping of PSR-18 and HttpClient implementations
    • Improved type hints and error messaging for invalid client configurations
    • Added validation for client implementations
  2. In tests/Feature/HttpClientsTest.php (New):

    • Added tests for client configuration scenarios:
      • Default client discovery behavior
      • PSR-18 client compatibility
      • Direct HttpMethodsClient usage
      • Invalid client validation

Documentation Updates:

  1. In .gitignore:
    • Added .env and typesense-data to ignore local development files

Context

This fixes issue #77 reported by @ragusa87 where passing a PSR-18 client instance
to the configuration would fail with:

TypeError: Http\Client\Common\HttpMethodsClient::__construct(): Argument #1 
($httpClient) must be of type Http\Client\HttpClient, Http\Discovery\Psr18Client given

The fix ensures proper handling of different client types while maintaining backward
compatibility and improving performance by avoiding unnecessary client wrapping and
recreation.

PR Checklist

* add support for HttpMethodsClient to be used directly without wrapping
* handle different client types (PSR-18, HttpClient) properly by wrapping only
  when needed
* fix client initialization to avoid creating new instances on every call
* add tests for client configuration scenarios
* improve type hints and error messages for invalid client configurations
- modify client instance checks in `Configuration.php` to handle psr-18
  clients correctly
- update `testWorksWithPsr18Client` to wrap client with required factories
- add new test `testWorksWithLegacyPsr18Client` for backward compatibility
- simplify array key check using `isset` instead of `array_key_exists`
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.

1 participant