Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Contracts/Transporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ interface Transporter
* @throws ErrorException|UnserializableResponse|TransporterException
*/
public function request(Payload $payload): array;

/**
* Sends a content request to a server.
**
*
* @throws ErrorException|UnserializableResponse|TransporterException
*/
public function requestContent(Payload $payload): string;
}
42 changes: 42 additions & 0 deletions src/Resources/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,46 @@ public function list(): array

return $result;
}

/**
* Returns information about a specific file.
*
* @see https://beta.openai.com/docs/api-reference/files/retrieve
*
* @return array<string, mixed>
*/
public function retrieve(string $file): array
{
$payload = Payload::retrieve('files', $file);

return $this->transporter->request($payload);
}

/**
* Returns the contents of the specified file.
*
* @see https://beta.openai.com/docs/api-reference/files/retrieve-content
*
* @return string
*/
public function retrieveContent(string $file): string
{
$payload = Payload::retrieveContent('files', $file);

return $this->transporter->requestContent($payload);
}

/**
* Delete a file.
*
* @see https://beta.openai.com/docs/api-reference/files/delete
*
* @return array<string, mixed>
*/
public function delete(string $file): array
{
$payload = Payload::delete('files', $file);

return $this->transporter->request($payload);
}
}
16 changes: 16 additions & 0 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ public function request(Payload $payload): array

return $response;
}

/**
* {@inheritDoc}
*/
public function requestContent(Payload $payload): string
{
$request = $payload->toRequest($this->baseUri, $this->headers);

try {
$response = $this->client->sendRequest($request);
} catch (ClientExceptionInterface $clientException) {
throw new TransporterException($clientException);
}

return $response->getBody()->getContents();
}
}
16 changes: 16 additions & 0 deletions src/ValueObjects/ResourceUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public static function retrieve(string $resource, string $id): self
return new self("{$resource}/{$id}");
}

/**
* Creates a new ResourceUri value object that retrieves the given resource content.
*/
public static function retrieveContent(string $resource, string $id): self
{
return new self("{$resource}/{$id}/content");
}

/**
* Creates a new ResourceUri value object that deletes the given resource.
*/
public static function delete(string $resource, string $id): self
{
return new self("{$resource}/{$id}");
}

/**
* {@inheritDoc}
*/
Expand Down
24 changes: 24 additions & 0 deletions src/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public static function retrieve(string $resource, string $id): self
return new self($contentType, $method, $uri);
}

/**
* Creates a new Payload value object from the given parameters.
*/
public static function retrieveContent(string $resource, string $id): self
{
$contentType = ContentType::JSON;
$method = Method::GET;
$uri = ResourceUri::retrieveContent($resource, $id);

return new self($contentType, $method, $uri);
}

/**
* Creates a new Payload value object from the given parameters.
*
Expand Down Expand Up @@ -80,6 +92,18 @@ public static function upload(string $resource, array $parameters): self
return new self($contentType, $method, $uri, $parameters);
}

/**
* Creates a new Payload value object from the given parameters.
*/
public static function delete(string $resource, string $id): self
{
$contentType = ContentType::JSON;
$method = Method::DELETE;
$uri = ResourceUri::delete($resource, $id);

return new self($contentType, $method, $uri);
}

/**
* Creates a new Psr 7 Request instance.
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Fixtures/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ function fileResource(): array
'purpose' => 'fine-tune',
];
}

/**
* @return string
*/
function fileContentResource(): string
{
return file_get_contents(__DIR__.'/MyFile.json');
}

/**
* @return array<string, mixed>
*/
function fileDeleteResource(): array
{
return [
'id' => 'file-XjGxS3KTG0uNmNOK362iJua3',
'object' => 'file',
'deleted' => true,
];
}
9 changes: 7 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use OpenAI\ValueObjects\Transporter\Headers;
use OpenAI\ValueObjects\Transporter\Payload;

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

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

return new Client($transporter);
}

function mockContentClient(string $method, string $resource, array $params, string $response)
{
return mockClient($method, $resource, $params, $response, 'requestContent');
}
24 changes: 24 additions & 0 deletions tests/Resources/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,27 @@
],
]);
});

test('retreive', function () {
$client = mockClient('GET', 'files/file-XjGxS3KTG0uNmNOK362iJua3', [], fileResource());

$result = $client->files()->retrieve('file-XjGxS3KTG0uNmNOK362iJua3');

expect($result)->toBeArray()->toBe(fileResource());
});

test('retreive content', function () {
$client = mockContentClient('GET', 'files/file-XjGxS3KTG0uNmNOK362iJua3/content', [], fileContentResource());

$result = $client->files()->retrieveContent('file-XjGxS3KTG0uNmNOK362iJua3');

expect($result)->toBeString()->toBe(fileContentResource());
});

test('delete', function () {
$client = mockClient('DELETE', 'files/file-XjGxS3KTG0uNmNOK362iJua3', [], fileDeleteResource());

$result = $client->files()->delete('file-XjGxS3KTG0uNmNOK362iJua3');

expect($result)->toBeArray()->toBe(fileDeleteResource());
});