Skip to content

Commit

Permalink
Refactor to an abstract Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Dec 6, 2023
1 parent 8d5f752 commit 614d9c9
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 148 deletions.
39 changes: 13 additions & 26 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
8 changes: 2 additions & 6 deletions src/Resources/Assistants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
}
5 changes: 1 addition & 4 deletions src/Resources/AssistantsFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 1 addition & 3 deletions src/Resources/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/Resources/Completions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions src/Resources/Concerns/Dispatchable.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Resources/Concerns/Transportable.php

This file was deleted.

5 changes: 1 addition & 4 deletions src/Resources/Edits.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class Edits implements EditsContract
final class Edits extends Resource implements EditsContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Creates a new edit for the provided input, instruction, and parameters.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class Embeddings implements EmbeddingsContract
final class Embeddings extends Resource implements EmbeddingsContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Creates an embedding vector representing the input text.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class Files implements FilesContract
final class Files extends Resource implements FilesContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Returns a list of files that belong to the user's organization.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/FineTunes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class FineTunes implements FineTunesContract
final class FineTunes extends Resource implements FineTunesContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Creates a job that fine-tunes a specified model from a given dataset.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/FineTuning.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class FineTuning implements FineTuningContract
final class FineTuning extends Resource implements FineTuningContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Creates a job that fine-tunes a specified model from a given dataset.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class Images implements ImagesContract
final class Images extends Resource implements ImagesContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Creates an image given a prompt.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class Models implements ModelsContract
final class Models extends Resource implements ModelsContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Lists the currently available models, and provides basic information about each one such as the owner and availability.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/Moderations.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class Moderations implements ModerationsContract
final class Moderations extends Resource implements ModerationsContract
{
use Concerns\Dispatchable;
use Concerns\Transportable;

/**
* Classifies if text violates OpenAI's Content Policy.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace OpenAI\Resources;

use OpenAI\Contracts\DispatcherContract;
use OpenAI\Contracts\TransporterContract;

abstract class Resource
{
public function __construct(
protected readonly TransporterContract $transporter,
protected readonly DispatcherContract $events,
) {
// ..
}

public function event(object $event): void
{
$this->events->dispatch($event);
}
}
Loading

0 comments on commit 614d9c9

Please sign in to comment.