From 614d9c951c2cd9164947b80ed10e00945979a689 Mon Sep 17 00:00:00 2001 From: gehrisandro Date: Wed, 6 Dec 2023 08:30:37 +0100 Subject: [PATCH] Refactor to an abstract Resource --- src/Client.php | 39 ++++++++---------------- src/Resources/Assistants.php | 8 ++--- src/Resources/AssistantsFiles.php | 5 +-- src/Resources/Audio.php | 5 +-- src/Resources/Chat.php | 4 +-- src/Resources/Completions.php | 4 +-- src/Resources/Concerns/Dispatchable.php | 24 --------------- src/Resources/Concerns/Transportable.php | 18 ----------- src/Resources/Edits.php | 5 +-- src/Resources/Embeddings.php | 5 +-- src/Resources/Files.php | 5 +-- src/Resources/FineTunes.php | 5 +-- src/Resources/FineTuning.php | 5 +-- src/Resources/Images.php | 5 +-- src/Resources/Models.php | 5 +-- src/Resources/Moderations.php | 5 +-- src/Resources/Resource.php | 21 +++++++++++++ src/Resources/Threads.php | 11 ++----- src/Resources/ThreadsMessages.php | 8 ++--- src/Resources/ThreadsMessagesFiles.php | 5 +-- src/Resources/ThreadsRuns.php | 8 ++--- src/Resources/ThreadsRunsSteps.php | 5 +-- 22 files changed, 57 insertions(+), 148 deletions(-) delete mode 100644 src/Resources/Concerns/Dispatchable.php delete mode 100644 src/Resources/Concerns/Transportable.php create mode 100644 src/Resources/Resource.php diff --git a/src/Client.php b/src/Client.php index 2500c444..6a292d39 100644 --- a/src/Client.php +++ b/src/Client.php @@ -42,8 +42,7 @@ public function __construct( */ public function completions(): Completions { - return (new Completions($this->transporter)) - ->setEventDispatcher($this->events); + return new Completions($this->transporter, $this->events); } /** @@ -53,8 +52,7 @@ public function completions(): Completions */ public function chat(): Chat { - return (new Chat($this->transporter)) - ->setEventDispatcher($this->events); + return new Chat($this->transporter, $this->events); } /** @@ -64,8 +62,7 @@ public function chat(): Chat */ public function embeddings(): Embeddings { - return (new Embeddings($this->transporter)) - ->setEventDispatcher($this->events); + return new Embeddings($this->transporter, $this->events); } /** @@ -75,8 +72,7 @@ public function embeddings(): Embeddings */ public function audio(): Audio { - return (new Audio($this->transporter)) - ->setEventDispatcher($this->events); + return new Audio($this->transporter, $this->events); } /** @@ -86,8 +82,7 @@ public function audio(): Audio */ public function edits(): Edits { - return (new Edits($this->transporter)) - ->setEventDispatcher($this->events); + return new Edits($this->transporter, $this->events); } /** @@ -97,8 +92,7 @@ public function edits(): Edits */ public function files(): Files { - return (new Files($this->transporter)) - ->setEventDispatcher($this->events); + return new Files($this->transporter, $this->events); } /** @@ -108,8 +102,7 @@ public function files(): Files */ public function models(): Models { - return (new Models($this->transporter)) - ->setEventDispatcher($this->events); + return new Models($this->transporter, $this->events); } /** @@ -119,8 +112,7 @@ public function models(): Models */ public function fineTuning(): FineTuning { - return (new FineTuning($this->transporter)) - ->setEventDispatcher($this->events); + return new FineTuning($this->transporter, $this->events); } /** @@ -132,8 +124,7 @@ public function fineTuning(): FineTuning */ public function fineTunes(): FineTunes { - return (new FineTunes($this->transporter)) - ->setEventDispatcher($this->events); + return new FineTunes($this->transporter, $this->events); } /** @@ -143,8 +134,7 @@ public function fineTunes(): FineTunes */ public function moderations(): Moderations { - return (new Moderations($this->transporter)) - ->setEventDispatcher($this->events); + return new Moderations($this->transporter, $this->events); } /** @@ -154,8 +144,7 @@ public function moderations(): Moderations */ public function images(): Images { - return (new Images($this->transporter)) - ->setEventDispatcher($this->events); + return new Images($this->transporter, $this->events); } /** @@ -165,8 +154,7 @@ public function images(): Images */ public function assistants(): Assistants { - return (new Assistants($this->transporter)) - ->setEventDispatcher($this->events); + return new Assistants($this->transporter, $this->events); } /** @@ -176,7 +164,6 @@ public function assistants(): Assistants */ public function threads(): ThreadsContract { - return (new Threads($this->transporter)) - ->setEventDispatcher($this->events); + return new Threads($this->transporter, $this->events); } } diff --git a/src/Resources/Assistants.php b/src/Resources/Assistants.php index 489e3162..722b507a 100644 --- a/src/Resources/Assistants.php +++ b/src/Resources/Assistants.php @@ -13,11 +13,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class Assistants implements AssistantsContract +final class Assistants extends Resource implements AssistantsContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Create an assistant with a model and instructions. * @@ -126,7 +123,6 @@ public function list(array $parameters = []): AssistantListResponse */ public function files(): AssistantsFilesContract { - return (new AssistantsFiles($this->transporter)) - ->setEventDispatcher($this->events); + return new AssistantsFiles($this->transporter, $this->events); } } diff --git a/src/Resources/AssistantsFiles.php b/src/Resources/AssistantsFiles.php index 005c2c9b..d8a4ba48 100644 --- a/src/Resources/AssistantsFiles.php +++ b/src/Resources/AssistantsFiles.php @@ -12,11 +12,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class AssistantsFiles implements AssistantsFilesContract +final class AssistantsFiles extends Resource implements AssistantsFilesContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Create an assistant file by attaching a File to an assistant. * diff --git a/src/Resources/Audio.php b/src/Resources/Audio.php index dd1d51cb..cdb8a438 100644 --- a/src/Resources/Audio.php +++ b/src/Resources/Audio.php @@ -12,11 +12,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class Audio implements AudioContract +final class Audio extends Resource implements AudioContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Generates audio from the input text. * diff --git a/src/Resources/Chat.php b/src/Resources/Chat.php index c67884dc..d60a9b4b 100644 --- a/src/Resources/Chat.php +++ b/src/Resources/Chat.php @@ -12,11 +12,9 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class Chat implements ChatContract +final class Chat extends Resource implements ChatContract { - use Concerns\Dispatchable; use Concerns\Streamable; - use Concerns\Transportable; /** * Creates a completion for the chat message diff --git a/src/Resources/Completions.php b/src/Resources/Completions.php index 9d4d1c12..ffec9703 100644 --- a/src/Resources/Completions.php +++ b/src/Resources/Completions.php @@ -12,11 +12,9 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class Completions implements CompletionsContract +final class Completions extends Resource implements CompletionsContract { - use Concerns\Dispatchable; use Concerns\Streamable; - use Concerns\Transportable; /** * Creates a completion for the provided prompt and parameters diff --git a/src/Resources/Concerns/Dispatchable.php b/src/Resources/Concerns/Dispatchable.php deleted file mode 100644 index d20fb812..00000000 --- a/src/Resources/Concerns/Dispatchable.php +++ /dev/null @@ -1,24 +0,0 @@ -events = $events; - - return $this; - } - - public function event(object $event): void - { - $this->events->dispatch($event); - } -} diff --git a/src/Resources/Concerns/Transportable.php b/src/Resources/Concerns/Transportable.php deleted file mode 100644 index 091511f9..00000000 --- a/src/Resources/Concerns/Transportable.php +++ /dev/null @@ -1,18 +0,0 @@ -events->dispatch($event); + } +} diff --git a/src/Resources/Threads.php b/src/Resources/Threads.php index 0c73e2dd..21cd426b 100644 --- a/src/Resources/Threads.php +++ b/src/Resources/Threads.php @@ -14,11 +14,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class Threads implements ThreadsContract +final class Threads extends Resource implements ThreadsContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Create a thread. * @@ -127,8 +124,7 @@ public function delete(string $id): ThreadDeleteResponse */ public function messages(): ThreadsMessagesContract { - return (new ThreadsMessages($this->transporter)) - ->setEventDispatcher($this->events); + return new ThreadsMessages($this->transporter, $this->events); } /** @@ -138,7 +134,6 @@ public function messages(): ThreadsMessagesContract */ public function runs(): ThreadsRunsContract { - return (new ThreadsRuns($this->transporter)) - ->setEventDispatcher($this->events); + return new ThreadsRuns($this->transporter, $this->events); } } diff --git a/src/Resources/ThreadsMessages.php b/src/Resources/ThreadsMessages.php index 8f3b7c8c..720663b6 100644 --- a/src/Resources/ThreadsMessages.php +++ b/src/Resources/ThreadsMessages.php @@ -13,11 +13,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class ThreadsMessages implements ThreadsMessagesContract +final class ThreadsMessages extends Resource implements ThreadsMessagesContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Create a message. * @@ -126,7 +123,6 @@ public function list(string $threadId, array $parameters = []): ThreadMessageLis */ public function files(): ThreadsMessagesFilesContract { - return (new ThreadsMessagesFiles($this->transporter)) - ->setEventDispatcher($this->events); + return new ThreadsMessagesFiles($this->transporter, $this->events); } } diff --git a/src/Resources/ThreadsMessagesFiles.php b/src/Resources/ThreadsMessagesFiles.php index afb4fc18..068944a1 100644 --- a/src/Resources/ThreadsMessagesFiles.php +++ b/src/Resources/ThreadsMessagesFiles.php @@ -11,11 +11,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class ThreadsMessagesFiles implements ThreadsMessagesFilesContract +final class ThreadsMessagesFiles extends Resource implements ThreadsMessagesFilesContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Retrieves a message file. * diff --git a/src/Resources/ThreadsRuns.php b/src/Resources/ThreadsRuns.php index ebd8f2e4..eeae8047 100644 --- a/src/Resources/ThreadsRuns.php +++ b/src/Resources/ThreadsRuns.php @@ -12,11 +12,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class ThreadsRuns implements ThreadsRunsContract +final class ThreadsRuns extends Resource implements ThreadsRunsContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Create a run. * @@ -146,7 +143,6 @@ public function list(string $threadId, array $parameters = []): ThreadRunListRes */ public function steps(): ThreadsRunsStepsContract { - return (new ThreadsRunsSteps($this->transporter)) - ->setEventDispatcher($this->events); + return new ThreadsRunsSteps($this->transporter, $this->events); } } diff --git a/src/Resources/ThreadsRunsSteps.php b/src/Resources/ThreadsRunsSteps.php index d819e4e4..c85d6167 100644 --- a/src/Resources/ThreadsRunsSteps.php +++ b/src/Resources/ThreadsRunsSteps.php @@ -11,11 +11,8 @@ use OpenAI\ValueObjects\Transporter\Payload; use OpenAI\ValueObjects\Transporter\Response; -final class ThreadsRunsSteps implements ThreadsRunsStepsContract +final class ThreadsRunsSteps extends Resource implements ThreadsRunsStepsContract { - use Concerns\Dispatchable; - use Concerns\Transportable; - /** * Retrieves a run step. *