Skip to content

Commit 0b432cd

Browse files
authored
Merge pull request #101 from keboola/odin-PS-3098
Update client wrapper
2 parents c430aac + f2c8577 commit 0b432cd

13 files changed

+205
-199
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"ext-json": "*",
3131
"guzzlehttp/guzzle": "^6.3",
3232
"keboola/object-encryptor": "^0.3",
33-
"keboola/storage-api-client": "^11.3|^12.0",
34-
"keboola/storage-api-php-client-branch-wrapper": "^1.1",
33+
"keboola/storage-api-client": "^12.0",
34+
"keboola/storage-api-php-client-branch-wrapper": "^2.0",
3535
"psr/log": "^1.1",
3636
"symfony/config": "^4.4|^5.0",
3737
"symfony/validator": "^4.4|>=5.0,<=5.3.8"

src/ClientFactory.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient;
6+
7+
use Keboola\ObjectEncryptor\ObjectEncryptorFactory;
8+
use Keboola\StorageApiBranch\Factory\StorageClientPlainFactory;
9+
use Psr\Log\LoggerInterface;
10+
11+
class ClientFactory
12+
{
13+
private string $internalApiUrl;
14+
private string $internalApiToken;
15+
private string $region;
16+
private string $kmsKeyId;
17+
private string $akvUrl;
18+
private LoggerInterface $logger;
19+
private StorageClientPlainFactory $storageClientPlainFactory;
20+
21+
public function __construct(
22+
string $internalApiUrl,
23+
string $internalApiToken,
24+
string $region,
25+
string $kmsKeyId,
26+
string $akvUrl,
27+
StorageClientPlainFactory $storageClientPlainFactory,
28+
LoggerInterface $logger
29+
) {
30+
$this->internalApiUrl = $internalApiUrl;
31+
$this->internalApiToken = $internalApiToken;
32+
$this->region = $region;
33+
$this->kmsKeyId = $kmsKeyId;
34+
$this->logger = $logger;
35+
$this->akvUrl = $akvUrl;
36+
$this->storageClientPlainFactory = $storageClientPlainFactory;
37+
}
38+
39+
public function getClient(): Client
40+
{
41+
$objectEncryptorFactory = new ObjectEncryptorFactory(
42+
$this->kmsKeyId,
43+
$this->region,
44+
'',
45+
'',
46+
$this->akvUrl
47+
);
48+
$objectEncryptorFactory->setStackId((string) parse_url(
49+
(string) $this->storageClientPlainFactory->getClientOptionsReadOnly()->getUrl(),
50+
PHP_URL_HOST
51+
));
52+
$jobFactory = new JobFactory($this->storageClientPlainFactory, $objectEncryptorFactory);
53+
54+
return new Client(
55+
$this->logger,
56+
$jobFactory,
57+
$this->internalApiUrl,
58+
$this->internalApiToken,
59+
[
60+
'backoffMaxTries' => 2,
61+
'userAgent' => 'Public API',
62+
]
63+
);
64+
}
65+
}

src/JobFactory.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
use Keboola\JobQueueInternalClient\JobFactory\JobInterface;
1212
use Keboola\JobQueueInternalClient\JobFactory\JobRuntimeResolver;
1313
use Keboola\JobQueueInternalClient\JobFactory\NewJobDefinition;
14-
use Keboola\JobQueueInternalClient\JobFactory\StorageClientFactory;
1514
use Keboola\ObjectEncryptor\ObjectEncryptorFactory;
1615
use Keboola\StorageApi\ClientException as StorageClientException;
1716
use Keboola\StorageApiBranch\ClientWrapper;
17+
use Keboola\StorageApiBranch\Factory\ClientOptions;
18+
use Keboola\StorageApiBranch\Factory\StorageClientPlainFactory;
1819
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1920

2021
class JobFactory
@@ -40,19 +41,22 @@ class JobFactory
4041
public const PARALLELISM_INFINITY = 'infinity';
4142
public const ORCHESTRATOR_COMPONENT = 'keboola.orchestrator';
4243

43-
private StorageClientFactory $storageClientFactory;
44+
private StorageClientPlainFactory $storageClientFactory;
4445
private ObjectEncryptorFactory $objectEncryptorFactory;
4546

4647
public function __construct(
47-
StorageClientFactory $storageClientFactory,
48+
StorageClientPlainFactory $storageClientFactory,
4849
ObjectEncryptorFactory $objectEncryptorFactory
4950
) {
5051
$this->storageClientFactory = $storageClientFactory;
5152
// it's important to clone here because we change state of the factory!,
5253
// this is tested by JobFactoryTest::testEncryptionFactoryIsolation()
5354
$this->objectEncryptorFactory = clone $objectEncryptorFactory;
5455
$this->objectEncryptorFactory->setStackId(
55-
(string) parse_url($this->storageClientFactory->getStorageApiUrl(), PHP_URL_HOST)
56+
(string) parse_url(
57+
(string) $this->storageClientFactory->getClientOptionsReadOnly()->getUrl(),
58+
PHP_URL_HOST
59+
)
5660
);
5761
}
5862

@@ -136,10 +140,10 @@ private function validateJobData(array $data, string $validatorClass): array
136140
private function initializeNewJobData(array $data): array
137141
{
138142
try {
139-
$client = $this->storageClientFactory->getClientWrapper(
140-
$data['#tokenString'],
141-
ClientWrapper::BRANCH_MAIN
142-
)->getBasicClient();
143+
$client = $this->storageClientFactory->createClientWrapper(new ClientOptions(
144+
null,
145+
$data['#tokenString']
146+
))->getBasicClient();
143147
$tokenInfo = $client->verifyToken();
144148
$jobId = $client->generateId();
145149
$runId = empty($data['parentRunId']) ? $jobId :

src/JobFactory/JobRuntimeResolver.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
use Keboola\StorageApi\ClientException as StorageClientException;
1010
use Keboola\StorageApi\Components;
1111
use Keboola\StorageApiBranch\ClientWrapper;
12+
use Keboola\StorageApiBranch\Factory\ClientOptions;
13+
use Keboola\StorageApiBranch\Factory\StorageClientPlainFactory;
1214
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1315

1416
/**
1517
* @internal
1618
*/
1719
class JobRuntimeResolver
1820
{
19-
private StorageClientFactory $storageClientFactory;
21+
private StorageClientPlainFactory $storageClientFactory;
2022
private ?array $configuration;
2123
private array $jobData;
2224

23-
public function __construct(StorageClientFactory $storageClientFactory)
25+
public function __construct(StorageClientPlainFactory $storageClientFactory)
2426
{
2527
$this->storageClientFactory = $storageClientFactory;
2628
}
@@ -64,7 +66,7 @@ private function resolveTag(): string
6466
if (!empty($configuration['runtime']['image_tag'])) {
6567
return (string) $configuration['runtime']['image_tag'];
6668
}
67-
$componentsApi = $this->getComponentsApiClient(ClientWrapper::BRANCH_MAIN);
69+
$componentsApi = $this->getComponentsApiClient(null);
6870
$componentData = $componentsApi->getComponent($this->jobData['componentId']);
6971
if (!empty($componentData['data']['definition']['tag'])) {
7072
return $componentData['data']['definition']['tag'];
@@ -169,10 +171,11 @@ private function getConfiguration(): array
169171
private function getComponentsApiClient(?string $branchId): Components
170172
{
171173
return new Components(
172-
$this->storageClientFactory->getClientWrapper(
174+
$this->storageClientFactory->createClientWrapper(new ClientOptions(
175+
null,
173176
$this->jobData['#tokenString'],
174177
$branchId
175-
)->getBranchClientIfAvailable()
178+
))->getBranchClientIfAvailable()
176179
);
177180
}
178181

src/JobFactory/StorageClientFactory.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

tests/BaseClientFunctionalTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use Keboola\JobQueueInternalClient\JobFactory;
99
use Keboola\JobQueueInternalClient\JobFactory\Job;
1010
use Keboola\ObjectEncryptor\ObjectEncryptorFactory;
11+
use Keboola\StorageApiBranch\Factory\ClientOptions;
12+
use Keboola\StorageApiBranch\Factory\StorageClientPlainFactory;
1113
use Psr\Log\NullLogger;
12-
use Psr\Log\Test\TestLogger;
1314

1415
abstract class BaseClientFunctionalTest extends BaseTest
1516
{
@@ -36,10 +37,9 @@ protected function getClient(?string $kmsKeyId = null, ?string $keyVaultUrl = nu
3637

3738
private function getJobFactory(?string $kmsKeyId = null, ?string $keyVaultUrl = null): JobFactory
3839
{
39-
$storageClientFactory = new JobFactory\StorageClientFactory(
40-
(string) getenv('TEST_STORAGE_API_URL'),
41-
new TestLogger()
42-
);
40+
$storageClientFactory = new StorageClientPlainFactory(new ClientOptions(
41+
(string) getenv('TEST_STORAGE_API_URL')
42+
));
4343
$objectEncryptorFactory = new ObjectEncryptorFactory(
4444
$kmsKeyId ?? (string) getenv('TEST_KMS_KEY_ID'),
4545
(string) getenv('TEST_KMS_REGION'),

tests/ClientExceptionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
use Keboola\JobQueueInternalClient\JobFactory;
1717
use Keboola\JobQueueInternalClient\Result\JobResult;
1818
use Keboola\ObjectEncryptor\ObjectEncryptorFactory;
19+
use Keboola\StorageApiBranch\Factory\ClientOptions;
20+
use Keboola\StorageApiBranch\Factory\StorageClientPlainFactory;
1921
use Psr\Log\LoggerInterface;
2022
use Psr\Log\NullLogger;
21-
use Psr\Log\Test\TestLogger;
2223
use Throwable;
2324

2425
class ClientExceptionTest extends BaseTest
2526
{
2627
private function getJobFactory(): JobFactory
2728
{
28-
$storageClientFactory = new JobFactory\StorageClientFactory(
29+
$storageClientFactory = new StorageClientPlainFactory(new ClientOptions(
2930
'http://example.com/',
30-
new TestLogger()
31-
);
31+
));
3232
$objectEncryptorFactory = new ObjectEncryptorFactory('alias/some-key', 'us-east-1', '', '', '');
3333
return new JobFactory($storageClientFactory, $objectEncryptorFactory);
3434
}

tests/ClientFactoryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient\Tests;
6+
7+
use Keboola\JobQueueInternalClient\Client;
8+
use Keboola\JobQueueInternalClient\ClientFactory;
9+
use Keboola\StorageApiBranch\Factory\ClientOptions;
10+
use Keboola\StorageApiBranch\Factory\StorageClientPlainFactory;
11+
use PHPUnit\Framework\TestCase;
12+
use Psr\Log\Test\TestLogger;
13+
14+
class ClientFactoryTest extends TestCase
15+
{
16+
public function testGetClient(): void
17+
{
18+
$testLogger = new TestLogger();
19+
$factory = new ClientFactory(
20+
(string) getenv('TEST_QUEUE_API_URL'),
21+
(string) getenv('TEST_QUEUE_API_TOKEN'),
22+
'us-east-1',
23+
'alias/some-key',
24+
'',
25+
new StorageClientPlainFactory(new ClientOptions((string) getenv('TEST_STORAGE_API_URL'))),
26+
$testLogger
27+
);
28+
$client = $factory->getClient();
29+
self::assertInstanceOf(Client::class, $client);
30+
}
31+
}

tests/ClientTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Keboola\JobQueueInternalClient\Result\JobMetrics;
1919
use Keboola\JobQueueInternalClient\Result\JobResult;
2020
use Keboola\ObjectEncryptor\ObjectEncryptorFactory;
21+
use Keboola\StorageApiBranch\Factory\ClientOptions;
22+
use Keboola\StorageApiBranch\Factory\StorageClientPlainFactory;
2123
use Psr\Log\LoggerInterface;
2224
use Psr\Log\NullLogger;
2325
use Psr\Log\Test\TestLogger;
@@ -26,10 +28,9 @@ class ClientTest extends BaseTest
2628
{
2729
private function getJobFactory(): JobFactory
2830
{
29-
$storageClientFactory = new JobFactory\StorageClientFactory(
31+
$storageClientFactory = new StorageClientPlainFactory(new ClientOptions(
3032
'http://example.com/',
31-
new TestLogger()
32-
);
33+
));
3334
$objectEncryptorFactory = new ObjectEncryptorFactory('alias/some-key', 'us-east-1', '', '', '');
3435
return new JobFactory($storageClientFactory, $objectEncryptorFactory);
3536
}

0 commit comments

Comments
 (0)