Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] list blobs by prefix #23

Merged
merged 3 commits into from
Sep 28, 2024
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
24 changes: 19 additions & 5 deletions src/BlobStorage/Managers/Blob/BlobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,38 @@ public function __construct(

/**
* @param array<string, scalar> $options
* @param string[] $includes
* @param array{includes?: string[], prefix?: string} $queryParams
*/
public function list(array $options = [], array $includes = []): Blobs
public function list(array $options = [], array $queryParams = []): Blobs
{
/** @var string[] $includes */
$includes = $queryParams['includes'] ?? [];

if (array_diff($includes, $availableOptions = BlobIncludeOption::toArray()) !== []) {
throw InvalidArgumentException::create(sprintf("Invalid include option. \nValid options: %s", implode(', ', $availableOptions)));
}

$include = '';
$parameters = [];

if (!empty($includes)) {
$include = sprintf('&include=%s', implode(',', $includes));
$parameters[] = sprintf('include=%s', implode(',', $includes));
}

/** @var string $prefix */
$prefix = rtrim($queryParams['prefix'] ?? '', '/') . '/';

if (trim($prefix) !== '/') {
$parameters[] = "prefix={$prefix}";
}

$queryString = count($parameters) >= 1
? ('&' . implode('&', $parameters))
: '';

try {
$response = $this->request
->withOptions($options)
->get("{$this->containerName}/?restype=container&comp=list{$include}")
->get("{$this->containerName}/?restype=container&comp=list{$queryString}")
->getBody();

// @codeCoverageIgnoreStart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@
$request = (new RequestFake())
->withFakeResponse(new ResponseFake($body));

$result = (new BlobManager($request, $container = 'container'))->list(['option' => 'value'], includes: ['metadata', 'snapshots']);
$result = (new BlobManager($request, $container = 'container'))->list(
['option' => 'value'],
['includes' => ['metadata', 'snapshots'], 'prefix' => 'test'],
);

expect($result)
->toBeInstanceOf(Blobs::class)
Expand All @@ -166,14 +169,14 @@
->leaseState->toBe('available')
->serverEncrypted->toBe(true);

$request->assertGet("{$container}/?restype=container&comp=list&include=metadata,snapshots")
$request->assertGet("{$container}/?restype=container&comp=list&include=metadata,snapshots&prefix=test/")
->assertSentWithOptions(['option' => 'value']);
});

it('should an exception if the BlobIncludeOption is invalid', function () {
$blobManager = new BlobManager(new RequestFake(), 'container');

$blobManager->list(includes: ['invalid']);
$blobManager->list(queryParams: ['includes' => ['invalid']]);
})->throws(InvalidArgumentException::class);

it('should find by tag', function () {
Expand Down
Loading