Skip to content

Commit

Permalink
Include organization and project in URI
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Nov 2, 2024
1 parent 9543b46 commit de051ca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/main/php/com/openai/rest/OpenAIEndpoint.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace com\openai\rest;

use util\URI;
use webservices\rest\Endpoint;

/**
Expand All @@ -18,7 +19,14 @@ class OpenAIEndpoint extends RestEndpoint {
* @param ?string $project
*/
public function __construct($arg, $organization= null, $project= null) {
parent::__construct($arg instanceof Endpoint ? $arg : new Endpoint($arg));
if ($arg instanceof Endpoint) {
parent::__construct($arg);
} else {
$uri= $arg instanceof URI ? $arg : new URI($arg);
$organization??= $uri->param('organization');
$project??= $uri->param('project');
parent::__construct(new Endpoint($uri));
}

// Pass optional organization and project IDs
$headers= [];
Expand All @@ -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) : '').')';
}
}
16 changes: 16 additions & 0 deletions src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,27 @@ 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(
'com.openai.rest.OpenAIEndpoint(->https://api.openai.example.com/v1/)',
$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()
);
}
}

0 comments on commit de051ca

Please sign in to comment.