Skip to content

Commit

Permalink
Added support for updating files
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasse Lehtinen committed Jun 30, 2023
1 parent a5b0615 commit 5eaf889
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

These endpoints are not supported yet

- update files (metadata update always exists)
- createRelation
- removeRelation
- createAuthKey
16 changes: 14 additions & 2 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public function update(
?array $metadata = null,
?string $metadataToReturn = 'all',
) {
$response = $this->client->request('POST', 'update', [
// Form request
$request = [
'headers' => [
'Authorization' => 'Bearer '.$this->authToken,
],
Expand All @@ -180,7 +181,18 @@ public function update(
'metadata' => (! empty($metadata)) ? json_encode($metadata) : null,
'metadataToReturn' => $metadataToReturn,
],
]);
];

if (! empty($filename)) {
$request['multipart'] = [
[
'name' => 'Filedata',
'contents' => fopen($filename, 'r'),
],
];
}

$response = $this->client->request('POST', 'update', $request);

$body = json_decode($response->getBody()->getContents());

Expand Down
22 changes: 22 additions & 0 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@
expect($searchResults->hits[0]->metadata->gtin)->toBe('1234567890123');
});

test('can update asset contents', function () {
$temporaryFilename = tempnam('/tmp', 'ElvisTest');
file_put_contents($temporaryFilename, 'foobar');
$createResults = $this->assets->create(filename: $temporaryFilename, folderPath: '/Users/elvis-package-testing/');
expect($createResults)->toBeObject();
expect($createResults)->toHaveProperty('id');
expect($createResults->id)->toBeString();

// Update the file
$updatedTemporaryFilename = tempnam('/tmp', 'ElvisTest');
file_put_contents($updatedTemporaryFilename, 'foobaz');
$updateResults = $this->assets->update(id: $createResults->id, filename: $updatedTemporaryFilename);
expect($updateResults)->toBeObject();
expect($updateResults)->toHaveProperty('id');

// Download file and check contents
$searchResults = $this->assets->search(query: 'id:'.$updateResults->id, appendRequestSecret: true);
expect($searchResults)->toBeObject();
expect($searchResults->hits)->toHaveCount(1);
expect(file_get_contents($searchResults->hits[0]->originalUrl))->toBe('foobaz');
});

test('can remove files', function () {
// Upload a test file
$temporaryFilename = tempnam('/tmp', 'ElvisTest');
Expand Down

0 comments on commit 5eaf889

Please sign in to comment.