translator icon indicating copy to clipboard operation
translator copied to clipboard

Question about HttpClient

Open tacman opened this issue 10 months ago • 3 comments

I'm fixing the signatures so that PHP 8.4 doesn't throw warnings about implicit nullables.

    /**
     * @param string $key Google API key
     */
    public function __construct($key, ?HttpClient $httpClient = null, RequestFactory $requestFactory = null)

Of course, I'd love to put ?string in front of $key, but I can't remember when that was allowed. It's the main reason I want to drop old versions of PHP.

Static analysis is complaining about HttpClient though. Can it be replaced with ClientInterface? Or just keep this until we get psr-3 working?

Image

Image

tacman avatar Feb 05 '25 11:02 tacman

OK, I think I've figured it out, I need to replace those calls with ClientInterface and RequestFactoryInterface, right?

tacman avatar Feb 05 '25 11:02 tacman

ack. That's deprecated.


/**
 * Finds an HTTP Client.
 *
 * @author Márk Sági-Kazár <[email protected]>
 *
 * @deprecated This will be removed in 2.0. Consider using Psr18ClientDiscovery.
 */
final class HttpClientDiscovery extends ClassDiscovery
{
    /**
     * Finds an HTTP Client.
     *
     * @return HttpClient
     *
     * @throws Exception\NotFoundException
     */
    public static function find()
    {
        try {
            $client = static::findOneByType(HttpClient::class);
        } catch (DiscoveryFailedException $e) {
            throw new NotFoundException('No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter".', 0, $e);
        }

        return static::instantiateClass($client);
    }
}

tacman avatar Feb 05 '25 11:02 tacman

Sorry for so many questions! How do I add the body to the client call?

        $body = json_encode([['Text' => $string]]);
        $url = $this->getUrl($from, $to);
        $request = $this->getRequestFactory()->createRequest('POST', $url, [], $body);

Thoughts on PHP 8.1+? Then we could use named parameters, property promotion in the constructor, etc.

tacman avatar Feb 05 '25 11:02 tacman