|
4 | 4 |
|
5 | 5 | use DeepSeek\Contracts\Factories\ApiFactoryContract; |
6 | 6 | use DeepSeek\Enums\Configs\DefaultConfigs; |
| 7 | +use DeepSeek\Enums\Requests\ClientTypes; |
7 | 8 | use DeepSeek\Enums\Requests\HeaderFlags; |
8 | 9 | use GuzzleHttp\Client; |
| 10 | +use Psr\Http\Client\ClientInterface; |
| 11 | +use Symfony\Component\HttpClient\HttpClient; |
| 12 | +use Symfony\Component\HttpClient\Psr18Client; |
| 13 | +use RuntimeException; |
| 14 | +use InvalidArgumentException; |
9 | 15 |
|
10 | 16 | final class ApiFactory implements ApiFactoryContract |
11 | 17 | { |
12 | | - /** |
13 | | - * The API key for authentication. |
14 | | - * |
15 | | - * @var string |
16 | | - */ |
17 | 18 | protected string $apiKey; |
18 | | - |
19 | | - /** |
20 | | - * The base URL for the API. |
21 | | - * |
22 | | - * @var string |
23 | | - */ |
24 | 19 | protected string $baseUrl; |
25 | | - |
26 | | - /** |
27 | | - * The timeout value for the API request in seconds. |
28 | | - * |
29 | | - * @var int |
30 | | - */ |
31 | 20 | protected int $timeout; |
| 21 | + protected array $clientConfig; |
32 | 22 |
|
33 | | - /** |
34 | | - * Returns an instance of the ApiFactory. |
35 | | - * |
36 | | - * This is a static factory method that creates a new instance of the class. |
37 | | - * |
38 | | - * @return self A new instance of the ApiFactory. |
39 | | - */ |
40 | 23 | public static function build(): self |
41 | 24 | { |
42 | | - return new self; |
| 25 | + return new self(); |
43 | 26 | } |
44 | 27 |
|
45 | | - /** |
46 | | - * Set the base URL for the API. |
47 | | - * |
48 | | - * If no URL is provided, the default base URL from the configuration is used. |
49 | | - * |
50 | | - * @param string|null $baseUrl The base URL to set (optional). |
51 | | - * @return self The instance of the self for method chaining. |
52 | | - */ |
53 | 28 | public function setBaseUri(?string $baseUrl = null): self |
54 | 29 | { |
55 | 30 | $this->baseUrl = $baseUrl ? trim($baseUrl) : DefaultConfigs::BASE_URL->value; |
56 | 31 | return $this; |
57 | 32 | } |
58 | 33 |
|
59 | | - /** |
60 | | - * Set the API key for authentication. |
61 | | - * |
62 | | - * @param string $apiKey The API key to set. |
63 | | - * @return self The instance of the self for method chaining. |
64 | | - */ |
65 | 34 | public function setKey(string $apiKey): self |
66 | 35 | { |
67 | 36 | $this->apiKey = trim($apiKey); |
68 | 37 | return $this; |
69 | 38 | } |
70 | 39 |
|
71 | | - /** |
72 | | - * Set the timeout for the API request. |
73 | | - * |
74 | | - * If no timeout is provided, the default timeout value from the configuration is used. |
75 | | - * |
76 | | - * @param int|null $timeout The timeout value in seconds (optional). |
77 | | - * @return self The instance of the self for method chaining. |
78 | | - */ |
79 | 40 | public function setTimeout(?int $timeout = null): self |
80 | 41 | { |
81 | 42 | $this->timeout = $timeout ?: (int)DefaultConfigs::TIMEOUT->value; |
82 | 43 | return $this; |
83 | 44 | } |
84 | 45 |
|
85 | | - /** |
86 | | - * Build and return the Guzzle Client instance. |
87 | | - * |
88 | | - * This method creates and configures a new Guzzle HTTP client instance |
89 | | - * using the provided base URL, timeout, and headers. |
90 | | - * |
91 | | - * @return Client A Guzzle client instance configured for the API. |
92 | | - */ |
93 | | - public function run(): Client |
| 46 | + public function initialize(): self |
94 | 47 | { |
95 | | - $clientConfig = [ |
| 48 | + if (!isset($this->baseUrl)) { |
| 49 | + $this->setBaseUri(); |
| 50 | + } |
| 51 | + |
| 52 | + if (!isset($this->apiKey)) { |
| 53 | + throw new RuntimeException('API key must be set using setKey() before initialization.'); |
| 54 | + } |
| 55 | + |
| 56 | + if (!isset($this->timeout)) { |
| 57 | + $this->setTimeout(); |
| 58 | + } |
| 59 | + |
| 60 | + $this->clientConfig = [ |
96 | 61 | HeaderFlags::BASE_URL->value => $this->baseUrl, |
97 | 62 | HeaderFlags::TIMEOUT->value => $this->timeout, |
98 | 63 | HeaderFlags::HEADERS->value => [ |
99 | 64 | HeaderFlags::AUTHORIZATION->value => 'Bearer ' . $this->apiKey, |
100 | | - HeaderFlags::CONTENT_TYPE->value => "application/json", |
| 65 | + HeaderFlags::CONTENT_TYPE->value => 'application/json', |
101 | 66 | ], |
102 | 67 | ]; |
103 | 68 |
|
104 | | - return new Client($clientConfig); |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + public function run(?string $clientType = null): ClientInterface |
| 73 | + { |
| 74 | + $clientType = $clientType ?? ClientTypes::GUZZLE->value; |
| 75 | + |
| 76 | + if (!isset($this->clientConfig)) { |
| 77 | + $this->initialize(); |
| 78 | + } |
| 79 | + |
| 80 | + return match (strtolower($clientType)) { |
| 81 | + ClientTypes::GUZZLE->value => new Client($this->clientConfig), |
| 82 | + ClientTypes::SYMFONY->value => new Psr18Client(HttpClient::create($this->clientConfig)), |
| 83 | + default => throw new InvalidArgumentException("Unsupported client type: {$clientType}") |
| 84 | + }; |
105 | 85 | } |
106 | 86 | } |
0 commit comments