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 Type Handling in API Calls #86

Merged
merged 1 commit into from
Feb 24, 2025

Conversation

tharropoulos
Copy link
Contributor

TLDR

Support both PSR-18 ClientInterface and HttpMethodsClient implementations in API calls.

Change Summary

What is this?

This change addresses a critical compatibility issue where the Typesense PHP client was not properly handling different HTTP client implementations. Users were encountering errors when trying to use standard PSR-18 compatible clients, limiting the library's flexibility and causing unexpected failures. This fix enables broader compatibility with different HTTP client implementations while maintaining existing functionality.

Changes

Code Improvements:

  1. In src/ApiCall.php:

    • Updated $client property to support both ClientInterface and HttpMethodsClient types
    • Added conditional request handling logic to support both client types
    • Implemented PSR-17 factory discovery for request and stream creation
    • Added proper request building for ClientInterface implementations
  2. In src/Lib/Configuration.php:

    • Updated return type declaration in getClient() method to support both client types
    • Fixed type documentation to reflect dual client type support

Technical Details:

  • Added support for creating requests using PSR-17 factories when using ClientInterface
  • Maintained backward compatibility with HttpMethodsClient implementation
  • Properly handled request components (headers, body, query parameters) for both client types
  • Improved type declarations to reflect the dual client support

Context

This change addresses two reported issues:

  1. Issue Typehint mismatch #82: Users experiencing errors after upgrading to version 5.0.0
  2. Issue Types missmatches for the client configuration #77: TypeError when passing PSR-18 ClientInterface instances:
    TypeError: Http\Client\Common\HttpMethodsClient::__construct(): 
    Argument #1 ($httpClient) must be of type Http\Client\HttpClient, 
    Http\Discovery\Psr18Client given
    

The fix ensures that users can now use any PSR-18 compatible client implementation without encountering type errors, while maintaining support for the existing HttpMethodsClient functionality. This improvement makes the library more flexible and robust, following PHP-FIG standards for HTTP client implementations.

PR Checklist

- update `ApiCall` class to support both `HttpMethodsClient` and `ClientInterface`
- fix type declaration in `Configuration::getClient()` return type
- add request factory handling for `ClientInterface` requests
@jasonbosco jasonbosco merged commit 513270e into typesense:master Feb 24, 2025
1 check passed
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.

2 participants