Skip to content

Commit fc0e5b6

Browse files
committed
Refactor to an abstract Resource
1 parent 8d5f752 commit fc0e5b6

20 files changed

+57
-106
lines changed

src/Client.php

+13-26
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public function __construct(
4242
*/
4343
public function completions(): Completions
4444
{
45-
return (new Completions($this->transporter))
46-
->setEventDispatcher($this->events);
45+
return new Completions($this->transporter, $this->events);
4746
}
4847

4948
/**
@@ -53,8 +52,7 @@ public function completions(): Completions
5352
*/
5453
public function chat(): Chat
5554
{
56-
return (new Chat($this->transporter))
57-
->setEventDispatcher($this->events);
55+
return new Chat($this->transporter, $this->events);
5856
}
5957

6058
/**
@@ -64,8 +62,7 @@ public function chat(): Chat
6462
*/
6563
public function embeddings(): Embeddings
6664
{
67-
return (new Embeddings($this->transporter))
68-
->setEventDispatcher($this->events);
65+
return new Embeddings($this->transporter, $this->events);
6966
}
7067

7168
/**
@@ -75,8 +72,7 @@ public function embeddings(): Embeddings
7572
*/
7673
public function audio(): Audio
7774
{
78-
return (new Audio($this->transporter))
79-
->setEventDispatcher($this->events);
75+
return new Audio($this->transporter, $this->events);
8076
}
8177

8278
/**
@@ -86,8 +82,7 @@ public function audio(): Audio
8682
*/
8783
public function edits(): Edits
8884
{
89-
return (new Edits($this->transporter))
90-
->setEventDispatcher($this->events);
85+
return new Edits($this->transporter, $this->events);
9186
}
9287

9388
/**
@@ -97,8 +92,7 @@ public function edits(): Edits
9792
*/
9893
public function files(): Files
9994
{
100-
return (new Files($this->transporter))
101-
->setEventDispatcher($this->events);
95+
return new Files($this->transporter, $this->events);
10296
}
10397

10498
/**
@@ -108,8 +102,7 @@ public function files(): Files
108102
*/
109103
public function models(): Models
110104
{
111-
return (new Models($this->transporter))
112-
->setEventDispatcher($this->events);
105+
return new Models($this->transporter, $this->events);
113106
}
114107

115108
/**
@@ -119,8 +112,7 @@ public function models(): Models
119112
*/
120113
public function fineTuning(): FineTuning
121114
{
122-
return (new FineTuning($this->transporter))
123-
->setEventDispatcher($this->events);
115+
return new FineTuning($this->transporter, $this->events);
124116
}
125117

126118
/**
@@ -132,8 +124,7 @@ public function fineTuning(): FineTuning
132124
*/
133125
public function fineTunes(): FineTunes
134126
{
135-
return (new FineTunes($this->transporter))
136-
->setEventDispatcher($this->events);
127+
return new FineTunes($this->transporter, $this->events);
137128
}
138129

139130
/**
@@ -143,8 +134,7 @@ public function fineTunes(): FineTunes
143134
*/
144135
public function moderations(): Moderations
145136
{
146-
return (new Moderations($this->transporter))
147-
->setEventDispatcher($this->events);
137+
return new Moderations($this->transporter, $this->events);
148138
}
149139

150140
/**
@@ -154,8 +144,7 @@ public function moderations(): Moderations
154144
*/
155145
public function images(): Images
156146
{
157-
return (new Images($this->transporter))
158-
->setEventDispatcher($this->events);
147+
return new Images($this->transporter, $this->events);
159148
}
160149

161150
/**
@@ -165,8 +154,7 @@ public function images(): Images
165154
*/
166155
public function assistants(): Assistants
167156
{
168-
return (new Assistants($this->transporter))
169-
->setEventDispatcher($this->events);
157+
return new Assistants($this->transporter, $this->events);
170158
}
171159

172160
/**
@@ -176,7 +164,6 @@ public function assistants(): Assistants
176164
*/
177165
public function threads(): ThreadsContract
178166
{
179-
return (new Threads($this->transporter))
180-
->setEventDispatcher($this->events);
167+
return new Threads($this->transporter, $this->events);
181168
}
182169
}

src/Resources/Assistants.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
use OpenAI\ValueObjects\Transporter\Payload;
1414
use OpenAI\ValueObjects\Transporter\Response;
1515

16-
final class Assistants implements AssistantsContract
16+
final class Assistants extends Resource implements AssistantsContract
1717
{
18-
use Concerns\Dispatchable;
19-
use Concerns\Transportable;
20-
2118
/**
2219
* Create an assistant with a model and instructions.
2320
*
@@ -126,7 +123,6 @@ public function list(array $parameters = []): AssistantListResponse
126123
*/
127124
public function files(): AssistantsFilesContract
128125
{
129-
return (new AssistantsFiles($this->transporter))
130-
->setEventDispatcher($this->events);
126+
return new AssistantsFiles($this->transporter, $this->events);
131127
}
132128
}

src/Resources/AssistantsFiles.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
use OpenAI\ValueObjects\Transporter\Payload;
1313
use OpenAI\ValueObjects\Transporter\Response;
1414

15-
final class AssistantsFiles implements AssistantsFilesContract
15+
final class AssistantsFiles extends Resource implements AssistantsFilesContract
1616
{
17-
use Concerns\Dispatchable;
18-
use Concerns\Transportable;
19-
2017
/**
2118
* Create an assistant file by attaching a File to an assistant.
2219
*

src/Resources/Audio.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
use OpenAI\ValueObjects\Transporter\Payload;
1313
use OpenAI\ValueObjects\Transporter\Response;
1414

15-
final class Audio implements AudioContract
15+
final class Audio extends Resource implements AudioContract
1616
{
17-
use Concerns\Dispatchable;
18-
use Concerns\Transportable;
19-
2017
/**
2118
* Generates audio from the input text.
2219
*

src/Resources/Chat.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
use OpenAI\ValueObjects\Transporter\Payload;
1313
use OpenAI\ValueObjects\Transporter\Response;
1414

15-
final class Chat implements ChatContract
15+
final class Chat extends Resource implements ChatContract
1616
{
17-
use Concerns\Dispatchable;
1817
use Concerns\Streamable;
19-
use Concerns\Transportable;
2018

2119
/**
2220
* Creates a completion for the chat message

src/Resources/Completions.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
use OpenAI\ValueObjects\Transporter\Payload;
1313
use OpenAI\ValueObjects\Transporter\Response;
1414

15-
final class Completions implements CompletionsContract
15+
final class Completions extends Resource implements CompletionsContract
1616
{
17-
use Concerns\Dispatchable;
1817
use Concerns\Streamable;
19-
use Concerns\Transportable;
2018

2119
/**
2220
* Creates a completion for the provided prompt and parameters

src/Resources/Edits.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
use OpenAI\ValueObjects\Transporter\Payload;
1111
use OpenAI\ValueObjects\Transporter\Response;
1212

13-
final class Edits implements EditsContract
13+
final class Edits extends Resource implements EditsContract
1414
{
15-
use Concerns\Dispatchable;
16-
use Concerns\Transportable;
17-
1815
/**
1916
* Creates a new edit for the provided input, instruction, and parameters.
2017
*

src/Resources/Embeddings.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
use OpenAI\ValueObjects\Transporter\Payload;
1111
use OpenAI\ValueObjects\Transporter\Response;
1212

13-
final class Embeddings implements EmbeddingsContract
13+
final class Embeddings extends Resource implements EmbeddingsContract
1414
{
15-
use Concerns\Dispatchable;
16-
use Concerns\Transportable;
17-
1815
/**
1916
* Creates an embedding vector representing the input text.
2017
*

src/Resources/Files.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
use OpenAI\ValueObjects\Transporter\Payload;
1414
use OpenAI\ValueObjects\Transporter\Response;
1515

16-
final class Files implements FilesContract
16+
final class Files extends Resource implements FilesContract
1717
{
18-
use Concerns\Dispatchable;
19-
use Concerns\Transportable;
20-
2118
/**
2219
* Returns a list of files that belong to the user's organization.
2320
*

src/Resources/FineTunes.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
use OpenAI\ValueObjects\Transporter\Payload;
1515
use OpenAI\ValueObjects\Transporter\Response;
1616

17-
final class FineTunes implements FineTunesContract
17+
final class FineTunes extends Resource implements FineTunesContract
1818
{
19-
use Concerns\Dispatchable;
20-
use Concerns\Transportable;
21-
2219
/**
2320
* Creates a job that fine-tunes a specified model from a given dataset.
2421
*

src/Resources/FineTuning.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
use OpenAI\ValueObjects\Transporter\Payload;
1313
use OpenAI\ValueObjects\Transporter\Response;
1414

15-
final class FineTuning implements FineTuningContract
15+
final class FineTuning extends Resource implements FineTuningContract
1616
{
17-
use Concerns\Dispatchable;
18-
use Concerns\Transportable;
19-
2017
/**
2118
* Creates a job that fine-tunes a specified model from a given dataset.
2219
*

src/Resources/Images.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
use OpenAI\ValueObjects\Transporter\Payload;
1313
use OpenAI\ValueObjects\Transporter\Response;
1414

15-
final class Images implements ImagesContract
15+
final class Images extends Resource implements ImagesContract
1616
{
17-
use Concerns\Dispatchable;
18-
use Concerns\Transportable;
19-
2017
/**
2118
* Creates an image given a prompt.
2219
*

src/Resources/Models.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
use OpenAI\ValueObjects\Transporter\Payload;
1313
use OpenAI\ValueObjects\Transporter\Response;
1414

15-
final class Models implements ModelsContract
15+
final class Models extends Resource implements ModelsContract
1616
{
17-
use Concerns\Dispatchable;
18-
use Concerns\Transportable;
19-
2017
/**
2118
* Lists the currently available models, and provides basic information about each one such as the owner and availability.
2219
*

src/Resources/Moderations.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
use OpenAI\ValueObjects\Transporter\Payload;
1111
use OpenAI\ValueObjects\Transporter\Response;
1212

13-
final class Moderations implements ModerationsContract
13+
final class Moderations extends Resource implements ModerationsContract
1414
{
15-
use Concerns\Dispatchable;
16-
use Concerns\Transportable;
17-
1815
/**
1916
* Classifies if text violates OpenAI's Content Policy.
2017
*

src/Resources/Resource.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace OpenAI\Resources;
4+
5+
use OpenAI\Contracts\DispatcherContract;
6+
use OpenAI\Contracts\TransporterContract;
7+
8+
abstract class Resource
9+
{
10+
public function __construct(
11+
protected readonly TransporterContract $transporter,
12+
protected readonly DispatcherContract $events,
13+
) {
14+
// ..
15+
}
16+
17+
public function event(object $event): void
18+
{
19+
$this->events->dispatch($event);
20+
}
21+
}

src/Resources/Threads.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
use OpenAI\ValueObjects\Transporter\Payload;
1515
use OpenAI\ValueObjects\Transporter\Response;
1616

17-
final class Threads implements ThreadsContract
17+
final class Threads extends Resource implements ThreadsContract
1818
{
19-
use Concerns\Dispatchable;
20-
use Concerns\Transportable;
21-
2219
/**
2320
* Create a thread.
2421
*
@@ -127,8 +124,7 @@ public function delete(string $id): ThreadDeleteResponse
127124
*/
128125
public function messages(): ThreadsMessagesContract
129126
{
130-
return (new ThreadsMessages($this->transporter))
131-
->setEventDispatcher($this->events);
127+
return new ThreadsMessages($this->transporter, $this->events);
132128
}
133129

134130
/**
@@ -138,7 +134,6 @@ public function messages(): ThreadsMessagesContract
138134
*/
139135
public function runs(): ThreadsRunsContract
140136
{
141-
return (new ThreadsRuns($this->transporter))
142-
->setEventDispatcher($this->events);
137+
return new ThreadsRuns($this->transporter, $this->events);
143138
}
144139
}

src/Resources/ThreadsMessages.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
use OpenAI\ValueObjects\Transporter\Payload;
1414
use OpenAI\ValueObjects\Transporter\Response;
1515

16-
final class ThreadsMessages implements ThreadsMessagesContract
16+
final class ThreadsMessages extends Resource implements ThreadsMessagesContract
1717
{
18-
use Concerns\Dispatchable;
19-
use Concerns\Transportable;
20-
2118
/**
2219
* Create a message.
2320
*
@@ -126,7 +123,6 @@ public function list(string $threadId, array $parameters = []): ThreadMessageLis
126123
*/
127124
public function files(): ThreadsMessagesFilesContract
128125
{
129-
return (new ThreadsMessagesFiles($this->transporter))
130-
->setEventDispatcher($this->events);
126+
return new ThreadsMessagesFiles($this->transporter, $this->events);
131127
}
132128
}

src/Resources/ThreadsMessagesFiles.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
use OpenAI\ValueObjects\Transporter\Payload;
1212
use OpenAI\ValueObjects\Transporter\Response;
1313

14-
final class ThreadsMessagesFiles implements ThreadsMessagesFilesContract
14+
final class ThreadsMessagesFiles extends Resource implements ThreadsMessagesFilesContract
1515
{
16-
use Concerns\Dispatchable;
17-
use Concerns\Transportable;
18-
1916
/**
2017
* Retrieves a message file.
2118
*

0 commit comments

Comments
 (0)