Skip to content

Commit 118acfb

Browse files
authored
Merge pull request #1 from gehrisandro/extend-files-api
Files api: add "receive", "receive content" and "delete"
2 parents 092759b + e6b8e90 commit 118acfb

File tree

8 files changed

+157
-2
lines changed

8 files changed

+157
-2
lines changed

src/Contracts/Transporter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ interface Transporter
2222
* @throws ErrorException|UnserializableResponse|TransporterException
2323
*/
2424
public function request(Payload $payload): array;
25+
26+
/**
27+
* Sends a content request to a server.
28+
**
29+
*
30+
* @throws ErrorException|UnserializableResponse|TransporterException
31+
*/
32+
public function requestContent(Payload $payload): string;
2533
}

src/Resources/Files.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,46 @@ public function list(): array
2626

2727
return $result;
2828
}
29+
30+
/**
31+
* Returns information about a specific file.
32+
*
33+
* @see https://beta.openai.com/docs/api-reference/files/retrieve
34+
*
35+
* @return array<string, mixed>
36+
*/
37+
public function retrieve(string $file): array
38+
{
39+
$payload = Payload::retrieve('files', $file);
40+
41+
return $this->transporter->request($payload);
42+
}
43+
44+
/**
45+
* Returns the contents of the specified file.
46+
*
47+
* @see https://beta.openai.com/docs/api-reference/files/retrieve-content
48+
*
49+
* @return string
50+
*/
51+
public function retrieveContent(string $file): string
52+
{
53+
$payload = Payload::retrieveContent('files', $file);
54+
55+
return $this->transporter->requestContent($payload);
56+
}
57+
58+
/**
59+
* Delete a file.
60+
*
61+
* @see https://beta.openai.com/docs/api-reference/files/delete
62+
*
63+
* @return array<string, mixed>
64+
*/
65+
public function delete(string $file): array
66+
{
67+
$payload = Payload::delete('files', $file);
68+
69+
return $this->transporter->request($payload);
70+
}
2971
}

src/Transporters/HttpTransporter.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,20 @@ public function request(Payload $payload): array
5656

5757
return $response;
5858
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function requestContent(Payload $payload): string
64+
{
65+
$request = $payload->toRequest($this->baseUri, $this->headers);
66+
67+
try {
68+
$response = $this->client->sendRequest($request);
69+
} catch (ClientExceptionInterface $clientException) {
70+
throw new TransporterException($clientException);
71+
}
72+
73+
return $response->getBody()->getContents();
74+
}
5975
}

src/ValueObjects/ResourceUri.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public static function retrieve(string $resource, string $id): self
5151
return new self("{$resource}/{$id}");
5252
}
5353

54+
/**
55+
* Creates a new ResourceUri value object that retrieves the given resource content.
56+
*/
57+
public static function retrieveContent(string $resource, string $id): self
58+
{
59+
return new self("{$resource}/{$id}/content");
60+
}
61+
62+
/**
63+
* Creates a new ResourceUri value object that deletes the given resource.
64+
*/
65+
public static function delete(string $resource, string $id): self
66+
{
67+
return new self("{$resource}/{$id}");
68+
}
69+
5470
/**
5571
* {@inheritDoc}
5672
*/

src/ValueObjects/Transporter/Payload.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public static function retrieve(string $resource, string $id): self
5252
return new self($contentType, $method, $uri);
5353
}
5454

55+
/**
56+
* Creates a new Payload value object from the given parameters.
57+
*/
58+
public static function retrieveContent(string $resource, string $id): self
59+
{
60+
$contentType = ContentType::JSON;
61+
$method = Method::GET;
62+
$uri = ResourceUri::retrieveContent($resource, $id);
63+
64+
return new self($contentType, $method, $uri);
65+
}
66+
5567
/**
5668
* Creates a new Payload value object from the given parameters.
5769
*
@@ -80,6 +92,18 @@ public static function upload(string $resource, array $parameters): self
8092
return new self($contentType, $method, $uri, $parameters);
8193
}
8294

95+
/**
96+
* Creates a new Payload value object from the given parameters.
97+
*/
98+
public static function delete(string $resource, string $id): self
99+
{
100+
$contentType = ContentType::JSON;
101+
$method = Method::DELETE;
102+
$uri = ResourceUri::delete($resource, $id);
103+
104+
return new self($contentType, $method, $uri);
105+
}
106+
83107
/**
84108
* Creates a new Psr 7 Request instance.
85109
*/

tests/Fixtures/File.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,23 @@ function fileResource(): array
1414
'purpose' => 'fine-tune',
1515
];
1616
}
17+
18+
/**
19+
* @return string
20+
*/
21+
function fileContentResource(): string
22+
{
23+
return file_get_contents(__DIR__.'/MyFile.json');
24+
}
25+
26+
/**
27+
* @return array<string, mixed>
28+
*/
29+
function fileDeleteResource(): array
30+
{
31+
return [
32+
'id' => 'file-XjGxS3KTG0uNmNOK362iJua3',
33+
'object' => 'file',
34+
'deleted' => true,
35+
];
36+
}

tests/Pest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use OpenAI\ValueObjects\Transporter\Headers;
88
use OpenAI\ValueObjects\Transporter\Payload;
99

10-
function mockClient(string $method, string $resource, array $params, array $response)
10+
function mockClient(string $method, string $resource, array $params, array|string $response, $methodName = 'request')
1111
{
1212
$transporter = Mockery::mock(Transporter::class);
1313

1414
$transporter
15-
->shouldReceive('request')
15+
->shouldReceive($methodName)
1616
->once()
1717
->withArgs(function (Payload $payload) use ($method, $resource) {
1818
$baseUri = BaseUri::from('api.openai.com/v1');
@@ -26,3 +26,8 @@ function mockClient(string $method, string $resource, array $params, array $resp
2626

2727
return new Client($transporter);
2828
}
29+
30+
function mockContentClient(string $method, string $resource, array $params, string $response)
31+
{
32+
return mockClient($method, $resource, $params, $response, 'requestContent');
33+
}

tests/Resources/Files.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,27 @@
1919
],
2020
]);
2121
});
22+
23+
test('retreive', function () {
24+
$client = mockClient('GET', 'files/file-XjGxS3KTG0uNmNOK362iJua3', [], fileResource());
25+
26+
$result = $client->files()->retrieve('file-XjGxS3KTG0uNmNOK362iJua3');
27+
28+
expect($result)->toBeArray()->toBe(fileResource());
29+
});
30+
31+
test('retreive content', function () {
32+
$client = mockContentClient('GET', 'files/file-XjGxS3KTG0uNmNOK362iJua3/content', [], fileContentResource());
33+
34+
$result = $client->files()->retrieveContent('file-XjGxS3KTG0uNmNOK362iJua3');
35+
36+
expect($result)->toBeString()->toBe(fileContentResource());
37+
});
38+
39+
test('delete', function () {
40+
$client = mockClient('DELETE', 'files/file-XjGxS3KTG0uNmNOK362iJua3', [], fileDeleteResource());
41+
42+
$result = $client->files()->delete('file-XjGxS3KTG0uNmNOK362iJua3');
43+
44+
expect($result)->toBeArray()->toBe(fileDeleteResource());
45+
});

0 commit comments

Comments
 (0)