Skip to content

Commit 95ef1b2

Browse files
authored
Merge pull request #383 from keboola/pepa_PST-1658_applicationToken
PST-1658 Allow listing jobs using application token
2 parents ccf5875 + 643a402 commit 95ef1b2

File tree

7 files changed

+273
-80
lines changed

7 files changed

+273
-80
lines changed

azure-pipelines.tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
testCommand: composer ci
1212
variables:
1313
CLOUD_PROVIDER: aws
14+
HOSTNAME_SUFFIX: $(HOSTNAME_SUFFIX__AWS)
1415
STORAGE_API_URL: $(STORAGE_API_URL__AWS)
1516
GOOGLE_APPLICATION_CREDENTIALS: /code/var/gcp-private-key.json
1617
secrets:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"phpstan/phpstan": "^1.0",
4848
"phpstan/phpstan-phpunit": "^1.3",
4949
"phpunit/phpunit": "^9.5",
50+
"sempro/phpunit-pretty-print": "^1.4",
5051
"symfony/dotenv": "^6.3"
5152
},
5253
"scripts": {

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
bootstrap="tests/bootstrap.php"
99
convertDeprecationsToExceptions="false"
1010
cacheResultFile="/tmp/phpunit.result.cache"
11+
printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinterForPhpUnit9"
1112
>
1213
<php>
1314
<ini name="display_errors" value="1" />

src/Client.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ public function __construct(
4747
string $internalQueueApiUrl,
4848
?string $internalQueueToken,
4949
?string $storageApiToken,
50+
?string $applicationToken,
5051
array $options = [],
5152
) {
52-
$this->validateConfiguration($internalQueueApiUrl, $internalQueueToken, $storageApiToken, $options);
53+
$this->validateConfiguration(
54+
$internalQueueApiUrl,
55+
$internalQueueToken,
56+
$storageApiToken,
57+
$applicationToken,
58+
$options,
59+
);
5360
if (!empty($options['backoffMaxTries'])) {
5461
$options['backoffMaxTries'] = intval($options['backoffMaxTries']);
5562
} else {
@@ -59,7 +66,13 @@ public function __construct(
5966
$options['userAgent'] = self::DEFAULT_USER_AGENT;
6067
}
6168

62-
$this->guzzle = $this->initClient($internalQueueApiUrl, $internalQueueToken, $storageApiToken, $options);
69+
$this->guzzle = $this->initClient(
70+
$internalQueueApiUrl,
71+
$internalQueueToken,
72+
$storageApiToken,
73+
$applicationToken,
74+
$options,
75+
);
6376
$this->existingJobFactory = $existingJobFactory;
6477
$this->logger = $logger;
6578
}
@@ -68,15 +81,24 @@ private function validateConfiguration(
6881
string $internalQueueApiUrl,
6982
?string $internalQueueToken,
7083
?string $storageApiToken,
84+
?string $applicationToken,
7185
array $options,
7286
): void {
7387
$validator = Validation::createValidator();
7488
$errors = $validator->validate($internalQueueApiUrl, [new Url()]);
75-
if ($internalQueueToken === null && $storageApiToken === null) {
76-
throw new ClientException('Both InternalApiToken and StorageAPIToken are empty.');
89+
90+
$providedTokens = array_filter(
91+
[$internalQueueToken, $storageApiToken, $applicationToken],
92+
fn(?string $token) => $token !== null,
93+
);
94+
95+
if (count($providedTokens) === 0) {
96+
throw new ClientException(
97+
'No token provided (internalQueueToken, storageApiToken and applicationToken are empty)',
98+
);
7799
}
78-
if ($internalQueueToken !== null && $storageApiToken !== null) {
79-
throw new ClientException('Both InternalApiToken and StorageAPIToken are non-empty. Use only one.');
100+
if (count($providedTokens) > 1) {
101+
throw new ClientException('More than one authentication token provided');
80102
}
81103
if ($internalQueueToken !== null) {
82104
$errors->addAll(
@@ -88,6 +110,11 @@ private function validateConfiguration(
88110
$validator->validate($storageApiToken, [new NotBlank()]),
89111
);
90112
}
113+
if ($applicationToken !== null) {
114+
$errors->addAll(
115+
$validator->validate($applicationToken, [new NotBlank()]),
116+
);
117+
}
91118
if (!empty($options['backoffMaxTries'])) {
92119
$errors->addAll($validator->validate($options['backoffMaxTries'], [new Range(['min' => 0, 'max' => 100])]));
93120
}
@@ -347,6 +374,7 @@ private function initClient(
347374
string $url,
348375
?string $internalToken,
349376
?string $storageToken,
377+
?string $applicationToken,
350378
array $options = [],
351379
): GuzzleClient {
352380
// Initialize handlers (start with those supplied in constructor)
@@ -359,7 +387,7 @@ private function initClient(
359387
$handlerStack->push(Middleware::retry($this->createDefaultDecider($options['backoffMaxTries'])));
360388
// Set handler to set default headers
361389
$handlerStack->push(Middleware::mapRequest(
362-
function (RequestInterface $request) use ($internalToken, $storageToken, $options) {
390+
function (RequestInterface $request) use ($internalToken, $storageToken, $applicationToken, $options) {
363391
$request = $request->withHeader('User-Agent', $options['userAgent'])
364392
->withHeader('Content-type', 'application/json');
365393
if ($internalToken !== null) {
@@ -368,6 +396,9 @@ function (RequestInterface $request) use ($internalToken, $storageToken, $option
368396
if ($storageToken !== null) {
369397
$request = $request->withHeader('X-StorageApi-Token', $storageToken);
370398
}
399+
if ($applicationToken !== null) {
400+
$request = $request->withHeader('X-KBC-ManageApiToken', $applicationToken);
401+
}
371402
return $request;
372403
},
373404
));

tests/BaseClientFunctionalTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected function getClient(
9090
self::getRequiredEnv('TEST_QUEUE_API_URL'),
9191
self::getRequiredEnv('TEST_QUEUE_API_TOKEN'),
9292
null,
93+
null,
9394
);
9495
}
9596

tests/ClientExceptionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private function getClient(array $options): Client
5252
'http://example.com/',
5353
'testToken',
5454
null,
55+
null,
5556
$options,
5657
);
5758
}

0 commit comments

Comments
 (0)