Skip to content

Refactor ApiFactory to Support Any HTTP Client #17

@maagdeveloper

Description

@maagdeveloper

❌ Current Issue: Hardcoded Guzzle
Right now, ApiFactory forces Guzzle usage:

use GuzzleHttp\Client;

public function run(): Client
{
    return new Client([...]); // ❌ Forces Guzzle
}

✅ Updated: Use php-http/discovery to Auto-Detect the HTTP Client
Modify ApiFactory to support both Guzzle and Symfony's HTTP client dynamically:

use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Client\ClientInterface;

final class ApiFactory implements ApiFactoryContract
{
    protected string $apiKey;
    protected string $baseUrl;
    protected int $timeout;

    public static function build(): self
    {
        return new self;
    }

    public function setBaseUri(?string $baseUrl = null): self
    {
        $this->baseUrl = $baseUrl ?: DefaultConfigs::BASE_URL->value;
        return $this;
    }

    public function setKey(string $apiKey): self
    {
        $this->apiKey = trim($apiKey);
        return $this;
    }

    public function setTimeout(?int $timeout = null): self
    {
        $this->timeout = $timeout ?: (int)DefaultConfigs::TIMEOUT->value;
        return $this;
    }

    public function run(): ClientInterface
    {
        return HttpClientDiscovery::find(); // Auto-detects installed HTTP client (Guzzle or Symfony)
    }
}

So the package will automatically work with either Guzzle or Symfony HTTP client!

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions