Skip to content

Commit 3f63abb

Browse files
authored
Merge pull request #483 from keboola/jv-PST-2272-partial-result-update-endpoint-client
Partial job result update endpoint support
2 parents dd79064 + 84fb51e commit 3f63abb

File tree

5 files changed

+188
-36
lines changed

5 files changed

+188
-36
lines changed

provisioning/local/.terraform.lock.hcl

Lines changed: 30 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provisioning/local/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ terraform {
1515

1616
azurerm = {
1717
source = "hashicorp/azurerm"
18-
version = "~> 3.66.0"
18+
version = "~> 4.17.0"
1919
}
2020

2121
azuread = {
@@ -30,7 +30,9 @@ terraform {
3030
}
3131

3232
backend "s3" {
33-
role_arn = "arn:aws:iam::681277395786:role/kbc-local-dev-terraform"
33+
assume_role = {
34+
role_arn = "arn:aws:iam::681277395786:role/kbc-local-dev-terraform"
35+
}
3436
region = "eu-central-1"
3537
bucket = "local-dev-terraform-bucket"
3638
dynamodb_table = "local-dev-terraform-table"

src/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,20 @@ public function postJobResult(
261261
return $this->existingJobFactory->loadFromExistingJobData($this->sendRequest($request));
262262
}
263263

264+
public function patchJobResult(string $jobId, array $patchData): JobInterface
265+
{
266+
if ($jobId === '') {
267+
throw new ClientException(sprintf('Invalid job ID: "%s".', $jobId));
268+
}
269+
$request = new Request(
270+
'PATCH',
271+
'jobs/' . $jobId . '/result',
272+
[],
273+
json_encode($patchData, JSON_THROW_ON_ERROR),
274+
);
275+
return $this->existingJobFactory->loadFromExistingJobData($this->sendRequest($request));
276+
}
277+
264278
/**
265279
* @return array<JobInterface>
266280
*/

tests/ClientFunctionalTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Keboola\JobQueueInternalClient\JobFactory\JobInterface;
1212
use Keboola\JobQueueInternalClient\JobListOptions;
1313
use Keboola\JobQueueInternalClient\JobPatchData;
14+
use Keboola\JobQueueInternalClient\JobResultPatchData;
1415
use Keboola\JobQueueInternalClient\JobsSortOptions;
1516
use Keboola\JobQueueInternalClient\Result\JobMetrics;
1617
use Keboola\JobQueueInternalClient\Result\JobResult;
@@ -1187,4 +1188,80 @@ public function testSearchJobsGroupedRaw(): void
11871188
$client = $this->getClient();
11881189
$client->searchJobsGroupedRaw(['groupBy' => ['componentId']]);
11891190
}
1191+
1192+
public function testPatchJobResult(): void
1193+
{
1194+
$newJobFactory = $this->getNewJobFactory();
1195+
$client = $this->getClient();
1196+
1197+
$job = $newJobFactory->createNewJob([
1198+
'#tokenString' => getenv('TEST_STORAGE_API_TOKEN'),
1199+
'configData' => [],
1200+
'componentId' => self::COMPONENT_ID_1,
1201+
'mode' => 'run',
1202+
]);
1203+
$createdJob = $client->createJob($job);
1204+
1205+
$patchData = [
1206+
'configVersion' => '2',
1207+
];
1208+
$updatedJob = $client->patchJobResult(
1209+
$createdJob->getId(),
1210+
$patchData,
1211+
);
1212+
1213+
self::assertEquals($patchData, $updatedJob->getResult());
1214+
}
1215+
1216+
public function testPatchJobResultMerge(): void
1217+
{
1218+
$newJobFactory = $this->getNewJobFactory();
1219+
$client = $this->getClient();
1220+
1221+
$job = $newJobFactory->createNewJob([
1222+
'#tokenString' => getenv('TEST_STORAGE_API_TOKEN'),
1223+
'configData' => [],
1224+
'componentId' => self::COMPONENT_ID_1,
1225+
'mode' => 'run',
1226+
]);
1227+
$createdJob = $client->createJob($job);
1228+
1229+
$initialResult = (new JobResult())
1230+
->setMessage('bar')
1231+
->setImages(['image1', 'image2'])
1232+
;
1233+
$createdJobWithInitialResult = $client->patchJob(
1234+
$createdJob->getId(),
1235+
(new JobPatchData())->setResult($initialResult),
1236+
);
1237+
1238+
$patchData = [
1239+
'message' => 'baz',
1240+
'images' => [
1241+
'key' => 0,
1242+
],
1243+
'configVersion' => '2',
1244+
];
1245+
1246+
$updatedJob = $client->patchJobResult(
1247+
$createdJobWithInitialResult->getId(),
1248+
$patchData,
1249+
);
1250+
1251+
$expectedResult = [
1252+
'message' => 'baz',
1253+
'configVersion' => '2',
1254+
'images' => [
1255+
'key' => 0,
1256+
],
1257+
'input' => [
1258+
'tables' => [],
1259+
],
1260+
'output' => [
1261+
'tables' => [],
1262+
],
1263+
];
1264+
1265+
self::assertEquals($expectedResult, $updatedJob->getResult());
1266+
}
11901267
}

tests/ClientTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Keboola\JobQueueInternalClient\JobFactory\JobObjectEncryptor;
1919
use Keboola\JobQueueInternalClient\JobListOptions;
2020
use Keboola\JobQueueInternalClient\JobPatchData;
21+
use Keboola\JobQueueInternalClient\JobResultPatchData;
2122
use Keboola\JobQueueInternalClient\Result\JobMetrics;
2223
use Keboola\JobQueueInternalClient\Result\JobResult;
2324
use Keboola\ObjectEncryptor\EncryptorOptions;
@@ -1236,4 +1237,66 @@ public function testClientGetJobsDurationSum(): void
12361237
self::assertEquals('Internal PHP Client', $request->getHeader('User-Agent')[0]);
12371238
self::assertEquals('application/json', $request->getHeader('Content-type')[0]);
12381239
}
1240+
1241+
public function testPatchJobResult(): void
1242+
{
1243+
$mock = new MockHandler([
1244+
new Response(
1245+
200,
1246+
['Content-Type' => 'application/json'],
1247+
'{
1248+
"id": "123",
1249+
"runId": "123",
1250+
"projectId": "123",
1251+
"tokenId": "123",
1252+
"#tokenString": "KBC::XXX",
1253+
"componentId": "my-component",
1254+
"status": "processing",
1255+
"desiredStatus": "processing",
1256+
"branchType": "default"
1257+
}',
1258+
),
1259+
]);
1260+
1261+
$container = [];
1262+
$history = Middleware::history($container);
1263+
$stack = HandlerStack::create($mock);
1264+
$stack->push($history);
1265+
$client = $this->createClientWithInternalToken(
1266+
options: ['handler' => $stack],
1267+
);
1268+
1269+
$patchData = [
1270+
'message' => 'xyz',
1271+
'configVersion' => '2',
1272+
];
1273+
$result = $client->patchJobResult(
1274+
'123',
1275+
$patchData,
1276+
);
1277+
1278+
self::assertInstanceOf(Job::class, $result);
1279+
self::assertCount(1, $container);
1280+
/** @var Request $request */
1281+
$request = $container[0]['request'];
1282+
self::assertEquals('http://example.com/jobs/123/result', $request->getUri()->__toString());
1283+
self::assertEquals('PATCH', $request->getMethod());
1284+
self::assertEquals(
1285+
json_encode($patchData),
1286+
$request->getBody()->getContents(),
1287+
);
1288+
self::assertEquals('testToken', $request->getHeader('X-JobQueue-InternalApi-Token')[0]);
1289+
self::assertEquals('Internal PHP Client', $request->getHeader('User-Agent')[0]);
1290+
self::assertEquals('application/json', $request->getHeader('Content-type')[0]);
1291+
}
1292+
1293+
public function testPatchJobResultInvalidJobId(): void
1294+
{
1295+
$client = $this->createClientWithInternalToken();
1296+
1297+
$this->expectException(ClientException::class);
1298+
$this->expectExceptionMessage('Invalid job ID: "".');
1299+
1300+
$client->patchJobResult('', []);
1301+
}
12391302
}

0 commit comments

Comments
 (0)