Skip to content
Open
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
14 changes: 14 additions & 0 deletions libs/sandboxes-service-api-client/src/Apps/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class App
private string $id;
private string $projectId;
private string $componentId;
private ?string $type = null;
private ?string $branchId = null;
private string $configId;
private string $configVersion;
Expand All @@ -83,6 +84,7 @@ public static function fromArray(array $in): self
$app->setId((string) $in['id']);
$app->setProjectId((string) $in['projectId']);
$app->setComponentId((string) $in['componentId']);
$app->setType(isset($in['type']) ? (string) $in['type'] : null);
$app->setBranchId(isset($in['branchId']) ? $in['branchId'] : null);
$app->setConfigId((string) $in['configId']);
$app->setConfigVersion((string) $in['configVersion']);
Expand All @@ -102,6 +104,7 @@ public function toArray(): array
'id' => $this->id,
'projectId' => $this->projectId,
'componentId' => $this->componentId,
'type' => $this->type,
'branchId' => $this->branchId,
'configId' => $this->configId,
'configVersion' => $this->configVersion,
Expand Down Expand Up @@ -147,6 +150,17 @@ public function setComponentId(string $componentId): self
return $this;
}

public function getType(): ?string
{
return $this->type;
}

public function setType(?string $type): self
{
$this->type = $type;
return $this;
}

public function getBranchId(): ?string
{
return $this->branchId;
Expand Down
20 changes: 16 additions & 4 deletions libs/sandboxes-service-api-client/src/Apps/AppsApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,31 @@ public function __construct(ApiClientConfiguration $configuration)
}

/**
* @param int|null $offset
* @param int|null $limit
* @param int|null $offset
* @param int|null $limit
* @param list<string> $types Filter by app type (e.g. 'python', 'r', 'streamlit')
* @param bool $listAll When true, lists all apps in the project regardless of token ownership
* @return array<App>
*/
public function listApps(?int $offset = null, ?int $limit = null): array
{
public function listApps(
?int $offset = null,
?int $limit = null,
array $types = [],
bool $listAll = false,
): array {
$queryParams = [];
if ($offset !== null) {
$queryParams['offset'] = (string) $offset;
}
if ($limit !== null) {
$queryParams['limit'] = (string) $limit;
}
foreach ($types as $type) {
$queryParams['type'][] = $type;
}
if ($listAll) {
$queryParams['listAll'] = '1';
}

$uri = '/apps';
if (!empty($queryParams)) {
Expand Down
1 change: 1 addition & 0 deletions libs/sandboxes-service-api-client/tests/Apps/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function testToArray(): void
'id' => 'app-id',
'projectId' => 'project-id',
'componentId' => 'keboola.data-apps',
'type' => null,
'branchId' => 'branch-id',
'configId' => 'config-id',
'configVersion' => '5',
Expand Down
88 changes: 88 additions & 0 deletions libs/sandboxes-service-api-client/tests/Apps/AppsApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,94 @@ public function testListAppsWithOnlyLimit(): void
);
}

public function testListAppsWithListAll(): void
{
$responseBody = [];

$requestHandler = self::createRequestHandler($requestsHistory, [
new Response(
200,
['Content-Type' => 'application/json'],
Json::encodeArray($responseBody),
),
]);

$client = new AppsApiClient(
new ApiClientConfiguration(
baseUrl: 'https://data-apps.keboola.com',
storageToken: 'my-token',
userAgent: 'Keboola Sandboxes Service API PHP Client',
requestHandler: $requestHandler(...),
),
);
$result = $client->listApps(listAll: true);

self::assertEquals([], $result);

self::assertCount(1, $requestsHistory);
self::assertRequestEquals(
'GET',
'https://data-apps.keboola.com/apps?listAll=1',
[
'X-StorageApi-Token' => 'my-token',
],
'',
$requestsHistory[0]['request'],
);
}

public function testListAppsWithTypes(): void
{
$responseBody = [
[
'id' => 'app-id-1',
'projectId' => 'project-id',
'componentId' => 'keboola.jupyter-sandbox',
'type' => 'python',
'branchId' => null,
'configId' => 'config-id-1',
'configVersion' => '1',
'state' => 'running',
'desiredState' => 'running',
'lastRequestTimestamp' => '2024-02-01T08:00:00+01:00',
'url' => null,
'autoSuspendAfterSeconds' => 3600,
'provisioningStrategy' => 'operator',
],
];

$requestHandler = self::createRequestHandler($requestsHistory, [
new Response(
200,
['Content-Type' => 'application/json'],
Json::encodeArray($responseBody),
),
]);

$client = new AppsApiClient(
new ApiClientConfiguration(
baseUrl: 'https://data-apps.keboola.com',
storageToken: 'my-token',
userAgent: 'Keboola Sandboxes Service API PHP Client',
requestHandler: $requestHandler(...),
),
);
$result = $client->listApps(types: ['python', 'r']);

self::assertEquals([App::fromArray($responseBody[0])], $result);

self::assertCount(1, $requestsHistory);
self::assertRequestEquals(
'GET',
'https://data-apps.keboola.com/apps?type%5B0%5D=python&type%5B1%5D=r',
[
'X-StorageApi-Token' => 'my-token',
],
'',
$requestsHistory[0]['request'],
);
}

public function testCreateApp(): void
{
$responseBody = [
Expand Down