Skip to content

Commit

Permalink
Merge pull request #377 from ManukMinasyan/main
Browse files Browse the repository at this point in the history
Added `withProject` method to set project for requests
  • Loading branch information
gehrisandro committed Jun 7, 2024
2 parents a57cfc3 + 8eddcc9 commit 724e4b2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ final class Factory
*/
private ?string $organization = null;

/**
* The project for the requests.
*/
private ?string $project = null;

/**
* The HTTP client for the requests.
*/
Expand Down Expand Up @@ -74,6 +79,16 @@ public function withOrganization(?string $organization): self
return $this;
}

/**
* Sets the project for the requests.
*/
public function withProject(?string $project): self
{
$this->project = $project;

return $this;
}

/**
* Sets the HTTP client for the requests.
* If no client is provided the factory will try to find one using PSR-18 HTTP Client Discovery.
Expand Down Expand Up @@ -141,6 +156,10 @@ public function make(): Client
$headers = $headers->withOrganization($this->organization);
}

if ($this->project !== null) {
$headers = $headers->withProject($this->project);
}

foreach ($this->headers as $name => $value) {
$headers = $headers->withCustomHeader($name, $value);
}
Expand Down
3 changes: 2 additions & 1 deletion src/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ final class OpenAI
/**
* Creates a new Open AI Client with the given API token.
*/
public static function client(string $apiKey, ?string $organization = null): Client
public static function client(string $apiKey, ?string $organization = null, ?string $project = null): Client
{
return self::factory()
->withApiKey($apiKey)
->withOrganization($organization)
->withProject($project)
->withHttpHeader('OpenAI-Beta', 'assistants=v2')
->make();
}
Expand Down
11 changes: 11 additions & 0 deletions src/ValueObjects/Transporter/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ public function withOrganization(string $organization): self
]);
}

/**
* Creates a new Headers value object, with the given project, and the existing headers.
*/
public function withProject(string $project): self
{
return new self([
...$this->headers,
'OpenAI-Project' => $project,
]);
}

/**
* Creates a new Headers value object, with the newly added header, and the existing headers.
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
expect($openAI)->toBeInstanceOf(Client::class);
});

it('sets project when provided', function () {
$openAI = OpenAI::client('foo', 'nunomaduro', 'openai_proj');

expect($openAI)->toBeInstanceOf(Client::class);
});

it('may create a client via factory', function () {
$openAI = OpenAI::factory()
->withApiKey('foo')
Expand All @@ -33,6 +39,15 @@
expect($openAI)->toBeInstanceOf(Client::class);
});

it('sets an project via factory', function () {
$openAI = OpenAI::factory()
->withOrganization('nunomaduro')
->withProject('openai_proj')
->make();

expect($openAI)->toBeInstanceOf(Client::class);
});

it('sets a custom client via factory', function () {
$openAI = OpenAI::factory()
->withHttpClient(new GuzzleClient())
Expand Down
12 changes: 12 additions & 0 deletions tests/ValueObjects/Transporter/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
]);
});

it('can have project', function () {
$headers = Headers::withAuthorization(ApiKey::from('foo'))
->withContentType(ContentType::JSON)
->withProject('openai_proj');

expect($headers->toArray())->toBe([
'Authorization' => 'Bearer foo',
'Content-Type' => 'application/json',
'OpenAI-Project' => 'openai_proj',
]);
});

it('can have custom header', function () {
$headers = Headers::withAuthorization(ApiKey::from('foo'))
->withContentType(ContentType::JSON)
Expand Down

0 comments on commit 724e4b2

Please sign in to comment.