Skip to content

FIX: Renamed all $apiToken variable and ApiToken class with $apiKey a… #27

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use GuzzleHttp\Client as GuzzleClient;
use OpenAI\Client;
use OpenAI\Transporters\HttpTransporter;
use OpenAI\ValueObjects\ApiToken;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\BaseUri;
use OpenAI\ValueObjects\Transporter\Headers;

Expand All @@ -14,13 +14,13 @@ final class OpenAI
/**
* Creates a new Open AI Client with the given API token.
*/
public static function client(string $apiToken, string $organization = null): Client
public static function client(string $apiKey, string $organization = null): Client
{
$apiToken = ApiToken::from($apiToken);
$apiKey = ApiKey::from($apiKey);

$baseUri = BaseUri::from('api.openai.com/v1');

$headers = Headers::withAuthorization($apiToken);
$headers = Headers::withAuthorization($apiKey);

if ($organization !== null) {
$headers = $headers->withOrganization($organization);
Expand Down
10 changes: 5 additions & 5 deletions src/ValueObjects/ApiToken.php → src/ValueObjects/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
/**
* @internal
*/
final class ApiToken implements Stringable
final class ApiKey implements Stringable
{
/**
* Creates a new API token value object.
*/
private function __construct(public readonly string $apiToken)
private function __construct(public readonly string $apiKey)
{
// ..
}

public static function from(string $apiToken): self
public static function from(string $apiKey): self
{
return new self($apiToken);
return new self($apiKey);
}

/**
* {@inheritdoc}
*/
public function toString(): string
{
return $this->apiToken;
return $this->apiKey;
}
}
6 changes: 3 additions & 3 deletions src/ValueObjects/Transporter/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace OpenAI\ValueObjects\Transporter;

use OpenAI\Enums\Transporter\ContentType;
use OpenAI\ValueObjects\ApiToken;
use OpenAI\ValueObjects\ApiKey;

/**
* @internal
Expand All @@ -25,10 +25,10 @@ private function __construct(private readonly array $headers)
/**
* Creates a new Headers value object with the given API token.
*/
public static function withAuthorization(ApiToken $apiToken): self
public static function withAuthorization(ApiKey $apiKey): self
{
return new self([
'Authorization' => "Bearer {$apiToken->toString()}",
'Authorization' => "Bearer {$apiKey->toString()}",
]);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use OpenAI\Client;
use OpenAI\Contracts\Transporter;
use OpenAI\ValueObjects\ApiToken;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\BaseUri;
use OpenAI\ValueObjects\Transporter\Headers;
use OpenAI\ValueObjects\Transporter\Payload;
Expand All @@ -16,7 +16,7 @@ function mockClient(string $method, string $resource, array $params, array|strin
->once()
->withArgs(function (Payload $payload) use ($method, $resource) {
$baseUri = BaseUri::from('api.openai.com/v1');
$headers = Headers::withAuthorization(ApiToken::from('foo'));
$headers = Headers::withAuthorization(ApiKey::from('foo'));

$request = $payload->toRequest($baseUri, $headers);

Expand Down
10 changes: 5 additions & 5 deletions tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use OpenAI\Exceptions\TransporterException;
use OpenAI\Exceptions\UnserializableResponse;
use OpenAI\Transporters\HttpTransporter;
use OpenAI\ValueObjects\ApiToken;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\BaseUri;
use OpenAI\ValueObjects\Transporter\Headers;
use OpenAI\ValueObjects\Transporter\Payload;
Expand All @@ -17,12 +17,12 @@
beforeEach(function () {
$this->client = Mockery::mock(ClientInterface::class);

$apiToken = ApiToken::from('foo');
$apiKey = ApiKey::from('foo');

$this->http = new HttpTransporter(
$this->client,
BaseUri::from('api.openai.com/v1'),
Headers::withAuthorization($apiToken)->withContentType(ContentType::JSON),
Headers::withAuthorization($apiKey)->withContentType(ContentType::JSON),
);
});

Expand Down Expand Up @@ -108,7 +108,7 @@
$payload = Payload::list('models');

$baseUri = BaseUri::from('api.openai.com');
$headers = Headers::withAuthorization(ApiToken::from('foo'));
$headers = Headers::withAuthorization(ApiKey::from('foo'));

$this->client
->shouldReceive('sendRequest')
Expand Down Expand Up @@ -177,7 +177,7 @@
$payload = Payload::list('models');

$baseUri = BaseUri::from('api.openai.com');
$headers = Headers::withAuthorization(ApiToken::from('foo'));
$headers = Headers::withAuthorization(ApiKey::from('foo'));

$this->client
->shouldReceive('sendRequest')
Expand Down
6 changes: 3 additions & 3 deletions tests/ValueObjects/ApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

// Generate test for API token value object...

use OpenAI\ValueObjects\ApiToken;
use OpenAI\ValueObjects\ApiKey;

it('can be created from a string', function () {
$apiToken = ApiToken::from('foo');
$apiKey = ApiKey::from('foo');

expect($apiToken->toString())->toBe('foo');
expect($apiKey->toString())->toBe('foo');
});
8 changes: 4 additions & 4 deletions tests/ValueObjects/Transporter/Headers.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

use OpenAI\Enums\Transporter\ContentType;
use OpenAI\ValueObjects\ApiToken;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\Headers;

it('can be created from an API Token', function () {
$headers = Headers::withAuthorization(ApiToken::from('foo'));
$headers = Headers::withAuthorization(ApiKey::from('foo'));

expect($headers->toArray())->toBe([
'Authorization' => 'Bearer foo',
]);
});

it('can have content/type', function () {
$headers = Headers::withAuthorization(ApiToken::from('foo'))
$headers = Headers::withAuthorization(ApiKey::from('foo'))
->withContentType(ContentType::JSON);

expect($headers->toArray())->toBe([
Expand All @@ -23,7 +23,7 @@
});

it('can have organization', function () {
$headers = Headers::withAuthorization(ApiToken::from('foo'))
$headers = Headers::withAuthorization(ApiKey::from('foo'))
->withContentType(ContentType::JSON)
->withOrganization('nunomaduro');

Expand Down
12 changes: 6 additions & 6 deletions tests/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use OpenAI\Enums\Transporter\ContentType;
use OpenAI\ValueObjects\ApiToken;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\BaseUri;
use OpenAI\ValueObjects\Transporter\Headers;
use OpenAI\ValueObjects\Transporter\Payload;
Expand All @@ -10,7 +10,7 @@
$payload = Payload::create('models', []);

$baseUri = BaseUri::from('api.openai.com/v1');
$headers = Headers::withAuthorization(ApiToken::from('foo'))->withContentType(ContentType::JSON);
$headers = Headers::withAuthorization(ApiKey::from('foo'))->withContentType(ContentType::JSON);

expect($payload->toRequest($baseUri, $headers)->getMethod())->toBe('POST');
});
Expand All @@ -19,7 +19,7 @@
$payload = Payload::list('models');

$baseUri = BaseUri::from('api.openai.com/v1');
$headers = Headers::withAuthorization(ApiToken::from('foo'))->withContentType(ContentType::JSON);
$headers = Headers::withAuthorization(ApiKey::from('foo'))->withContentType(ContentType::JSON);

$uri = $payload->toRequest($baseUri, $headers)->getUri();

Expand All @@ -32,7 +32,7 @@
$payload = Payload::list('models');

$baseUri = BaseUri::from('api.openai.com/v1');
$headers = Headers::withAuthorization(ApiToken::from('foo'))->withContentType(ContentType::JSON);
$headers = Headers::withAuthorization(ApiKey::from('foo'))->withContentType(ContentType::JSON);

expect($payload->toRequest($baseUri, $headers)->getBody()->getContents())->toBe('');
});
Expand All @@ -43,7 +43,7 @@
]);

$baseUri = BaseUri::from('api.openai.com/v1');
$headers = Headers::withAuthorization(ApiToken::from('foo'))->withContentType(ContentType::JSON);
$headers = Headers::withAuthorization(ApiKey::from('foo'))->withContentType(ContentType::JSON);

expect($payload->toRequest($baseUri, $headers)->getBody()->getContents())->toBe(json_encode([
'name' => 'test',
Expand All @@ -57,7 +57,7 @@
]);

$baseUri = BaseUri::from('api.openai.com/v1');
$headers = Headers::withAuthorization(ApiToken::from('foo'));
$headers = Headers::withAuthorization(ApiKey::from('foo'));

$request = $payload->toRequest($baseUri, $headers);

Expand Down