Skip to content

Commit

Permalink
Argument validation in the constructor
Browse files Browse the repository at this point in the history
aka fail early
  • Loading branch information
OskarStark committed Oct 8, 2024
1 parent aa61f81 commit 627adfa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/OpenAI/Platform/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Webmozart\Assert\Assert;

final readonly class Azure extends AbstractPlatform implements Platform
{
Expand All @@ -21,6 +22,11 @@ public function __construct(
#[\SensitiveParameter] private string $apiKey,
) {
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
Assert::notStartsWith($baseUrl, 'http://', 'The base URL must not contain the protocol.');
Assert::notStartsWith($baseUrl, 'https://', 'The base URL must not contain the protocol.');
Assert::stringNotEmpty($deployment, 'The deployment must not be empty.');
Assert::stringNotEmpty($apiVersion, 'The API version must not be empty.');
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
}

protected function rawRequest(string $endpoint, array $body): ResponseInterface
Expand Down
3 changes: 3 additions & 0 deletions src/OpenAI/Platform/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Webmozart\Assert\Assert;

final readonly class OpenAI extends AbstractPlatform implements Platform
{
Expand All @@ -18,6 +19,8 @@ public function __construct(
#[\SensitiveParameter] private string $apiKey,
) {
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
Assert::startsWith($apiKey, 'sk-', 'The API key must start with "sk-".');
}

protected function rawRequest(string $endpoint, array $body): ResponseInterface
Expand Down

0 comments on commit 627adfa

Please sign in to comment.