Skip to content

Commit

Permalink
Merge pull request #22 from xray-labs/bugfix/list-blobs-by-prefix
Browse files Browse the repository at this point in the history
[Bugfix] list blobs by prefix
  • Loading branch information
sjspereira authored Sep 28, 2024
2 parents a449b43 + 643431f commit cec4c2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
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

0 comments on commit cec4c2f

Please sign in to comment.