diff --git a/ChangeLog.md b/ChangeLog.md index cd6880e..5b008ea 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,8 @@ OpenAI APIs for XP ChangeLog ## ?.?.? / ????-??-?? +* Made it possible to supply organization and project in OpenAI API URI + (@thekid) * Added `RealtimeApi::socket()` to access the underlying network socket (@thekid) * Merged PR #17: Move the `Tools` class to the com.openai.tools package diff --git a/src/main/php/com/openai/rest/OpenAIEndpoint.class.php b/src/main/php/com/openai/rest/OpenAIEndpoint.class.php index a897def..9b071be 100644 --- a/src/main/php/com/openai/rest/OpenAIEndpoint.class.php +++ b/src/main/php/com/openai/rest/OpenAIEndpoint.class.php @@ -1,5 +1,6 @@ param('organization'); + $project??= $uri->param('project'); + parent::__construct(new Endpoint($uri)); + } // Pass optional organization and project IDs $headers= []; @@ -33,5 +41,12 @@ public function api(string $path, array $segments= []): Api { } /** @return string */ - public function toString() { return nameof($this).'(->'.$this->endpoint->base().')'; } + public function toString() { + $headers= $this->endpoint->headers(); + $query= ''; + if ($value= $headers['OpenAI-Organization'] ?? null) $query.= '&organization='.$value; + if ($value= $headers['OpenAI-Project'] ?? null) $query.= '&project='.$value; + + return nameof($this).'(->'.$this->endpoint->base().($query ? '?'.substr($query, 1) : '').')'; + } } \ No newline at end of file diff --git a/src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php b/src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php index 2f9741a..d068526 100644 --- a/src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php +++ b/src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php @@ -38,6 +38,14 @@ public function optional_project_header() { ); } + #[Test] + public function org_and_project_via_uri() { + $headers= $this->fixture(self::URI.'?organization=org-test&project=prj-test')->headers(); + + Assert::equals('org-test',$headers['OpenAI-Organization']); + Assert::equals('prj-test',$headers['OpenAI-Project']); + } + #[Test] public function string_representation() { Assert::equals( @@ -45,4 +53,12 @@ public function string_representation() { $this->fixture(self::URI)->toString() ); } + + #[Test] + public function string_representation_with_organization_and_project() { + Assert::equals( + 'com.openai.rest.OpenAIEndpoint(->https://api.openai.example.com/v1/?organization=org-test&project=prj-test)', + $this->fixture(self::URI, 'org-test', 'prj-test')->toString() + ); + } } \ No newline at end of file