From 4f8aff8ebf06fd6ef071ee5ad68ade0286cffa5b Mon Sep 17 00:00:00 2001 From: Bernhard Schmitt Date: Mon, 5 Feb 2024 17:41:34 +0100 Subject: [PATCH] Refactor from single to multiple accounts --- Classes/AccountRecord.php | 30 ++++++++++++++++++++++++++++ Classes/AccountRepository.php | 35 +++++++++++++++++++++++++++++++++ Classes/OpenAiClientFactory.php | 17 ++++++---------- Configuration/Objects.yaml | 7 ------- Configuration/Settings.yaml | 1 - 5 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 Classes/AccountRecord.php create mode 100644 Classes/AccountRepository.php delete mode 100644 Configuration/Objects.yaml diff --git a/Classes/AccountRecord.php b/Classes/AccountRecord.php new file mode 100644 index 0000000..37afd09 --- /dev/null +++ b/Classes/AccountRecord.php @@ -0,0 +1,30 @@ + $configuration + */ + public static function fromConfiguration(string $name, array $configuration): self + { + return new self( + $name, + $configuration['apiKey'], + $configuration['organisation'], + ); + } +} diff --git a/Classes/AccountRepository.php b/Classes/AccountRepository.php new file mode 100644 index 0000000..0ebab16 --- /dev/null +++ b/Classes/AccountRepository.php @@ -0,0 +1,35 @@ + + */ + public function findAll(): array + { + return array_map( + fn (array $data, string $name): AccountRecord => AccountRecord::fromConfiguration($name, $data), + $this->accounts, + array_keys($this->accounts) + ); + } + + public function findById(string $id): AccountRecord + { + if (array_key_exists($id, $this->accounts)) { + return AccountRecord::fromConfiguration($id, $this->accounts[$id]); + } + + throw new \Exception('Unknown account "' . $id . '"', 1707149688); + } +} diff --git a/Classes/OpenAiClientFactory.php b/Classes/OpenAiClientFactory.php index 7202dea..9987ae7 100644 --- a/Classes/OpenAiClientFactory.php +++ b/Classes/OpenAiClientFactory.php @@ -1,4 +1,5 @@ withHttpClient($this->client) - ->withApiKey($this->apiKey) + ->withHttpClient($this->httpClient) + ->withApiKey($record->apiKey) ->withHttpHeader('OpenAI-Beta', 'assistants=v1') - ->withOrganization($this->organisation); + ->withOrganization($record->organisation); return $factory->make(); } } diff --git a/Configuration/Objects.yaml b/Configuration/Objects.yaml deleted file mode 100644 index 668a552..0000000 --- a/Configuration/Objects.yaml +++ /dev/null @@ -1,7 +0,0 @@ -OpenAI\Client: - factoryObjectName: Sitegeist\Flow\OpenAiClientFactory\OpenAiClientFactory - factoryMethodName: createClient - -OpenAI\Contracts\ClientContract: - factoryObjectName: Sitegeist\Flow\OpenAiClientFactory\OpenAiClientFactory - factoryMethodName: createClient diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 2dd2796..89aa5d0 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -3,6 +3,5 @@ Sitegeist: OpenAiClientFactory: accounts: [] # name: - # discriminator: 'aja-%ENV:ENVIRONMENT_NAME%' # apiKey: ~ # organisation: ~