Skip to content

Commit 724e4b2

Browse files
authored
Merge pull request #377 from ManukMinasyan/main
Added `withProject` method to set project for requests
2 parents a57cfc3 + 8eddcc9 commit 724e4b2

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

src/Factory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ final class Factory
2828
*/
2929
private ?string $organization = null;
3030

31+
/**
32+
* The project for the requests.
33+
*/
34+
private ?string $project = null;
35+
3136
/**
3237
* The HTTP client for the requests.
3338
*/
@@ -74,6 +79,16 @@ public function withOrganization(?string $organization): self
7479
return $this;
7580
}
7681

82+
/**
83+
* Sets the project for the requests.
84+
*/
85+
public function withProject(?string $project): self
86+
{
87+
$this->project = $project;
88+
89+
return $this;
90+
}
91+
7792
/**
7893
* Sets the HTTP client for the requests.
7994
* If no client is provided the factory will try to find one using PSR-18 HTTP Client Discovery.
@@ -141,6 +156,10 @@ public function make(): Client
141156
$headers = $headers->withOrganization($this->organization);
142157
}
143158

159+
if ($this->project !== null) {
160+
$headers = $headers->withProject($this->project);
161+
}
162+
144163
foreach ($this->headers as $name => $value) {
145164
$headers = $headers->withCustomHeader($name, $value);
146165
}

src/OpenAI.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ final class OpenAI
1010
/**
1111
* Creates a new Open AI Client with the given API token.
1212
*/
13-
public static function client(string $apiKey, ?string $organization = null): Client
13+
public static function client(string $apiKey, ?string $organization = null, ?string $project = null): Client
1414
{
1515
return self::factory()
1616
->withApiKey($apiKey)
1717
->withOrganization($organization)
18+
->withProject($project)
1819
->withHttpHeader('OpenAI-Beta', 'assistants=v2')
1920
->make();
2021
}

src/ValueObjects/Transporter/Headers.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ public function withOrganization(string $organization): self
6262
]);
6363
}
6464

65+
/**
66+
* Creates a new Headers value object, with the given project, and the existing headers.
67+
*/
68+
public function withProject(string $project): self
69+
{
70+
return new self([
71+
...$this->headers,
72+
'OpenAI-Project' => $project,
73+
]);
74+
}
75+
6576
/**
6677
* Creates a new Headers value object, with the newly added header, and the existing headers.
6778
*/

tests/OpenAI.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
expect($openAI)->toBeInstanceOf(Client::class);
1818
});
1919

20+
it('sets project when provided', function () {
21+
$openAI = OpenAI::client('foo', 'nunomaduro', 'openai_proj');
22+
23+
expect($openAI)->toBeInstanceOf(Client::class);
24+
});
25+
2026
it('may create a client via factory', function () {
2127
$openAI = OpenAI::factory()
2228
->withApiKey('foo')
@@ -33,6 +39,15 @@
3339
expect($openAI)->toBeInstanceOf(Client::class);
3440
});
3541

42+
it('sets an project via factory', function () {
43+
$openAI = OpenAI::factory()
44+
->withOrganization('nunomaduro')
45+
->withProject('openai_proj')
46+
->make();
47+
48+
expect($openAI)->toBeInstanceOf(Client::class);
49+
});
50+
3651
it('sets a custom client via factory', function () {
3752
$openAI = OpenAI::factory()
3853
->withHttpClient(new GuzzleClient())

tests/ValueObjects/Transporter/Headers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@
4444
]);
4545
});
4646

47+
it('can have project', function () {
48+
$headers = Headers::withAuthorization(ApiKey::from('foo'))
49+
->withContentType(ContentType::JSON)
50+
->withProject('openai_proj');
51+
52+
expect($headers->toArray())->toBe([
53+
'Authorization' => 'Bearer foo',
54+
'Content-Type' => 'application/json',
55+
'OpenAI-Project' => 'openai_proj',
56+
]);
57+
});
58+
4759
it('can have custom header', function () {
4860
$headers = Headers::withAuthorization(ApiKey::from('foo'))
4961
->withContentType(ContentType::JSON)

0 commit comments

Comments
 (0)