Skip to content

Create job progress end point #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: 3.0.0-g6
Choose a base branch
from
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
37 changes: 37 additions & 0 deletions examples/jobs-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,43 @@ function authorizeJobDemo($authProvider, $projectId, $jobId)
echo \vsprintf('Request took %s seconds.%s', [\round($time, 3), "\n\r"]);
}

/**
* @param \Smartling\AuthApi\AuthApiInterface $authProvider
* @param string $projectId
* @param string $jobId
* @param string $targetLocaleId
* @return array
*/
function getJobProgress($authProvider, $projectId, $jobId, $targetLocaleId)
{
echo "--- Retrieving job progress ---\n";

$jobs = \Smartling\Jobs\JobsApi::create($authProvider, $projectId);
$info = FALSE;
$progressParameters = new \Smartling\Jobs\Params\JobProgressParameters();
$progressParameters->setTargetLocaleId($targetLocaleId);
$st = \microtime(true);

try {
$info = $jobs->getJobProgress($jobId, $progressParameters);
} catch (\Smartling\Exceptions\SmartlingApiException $e) {
\var_dump($e->getErrors());
}

$et = \microtime(true);
$time = $et - $st;

echo \vsprintf('Request took %s seconds.%s', [\round($time, 3), "\n\r"]);

if (!empty($info)) {
\var_dump($info);
}

return $info;
}

$fileUri = 'JobID1_en_fr.xml';
$targetLocaleId = 'fr';
$jobs = listJobsDemo($authProvider, $projectId);
$jobId = createJobDemo($authProvider, $projectId);
$jobId = updateJobDemo($authProvider, $projectId, $jobId);
Expand All @@ -312,3 +348,4 @@ function authorizeJobDemo($authProvider, $projectId, $jobId)
$job = searchJobDemo($authProvider, $projectId, $fileUri);
authorizeJobDemo($authProvider, $projectId, $jobId);
cancelJobDemo($authProvider, $projectId, $jobId);
$jobProgress = getJobProgress($authProvider, $projectId, $jobId, $targetLocaleId);
15 changes: 15 additions & 0 deletions src/Jobs/JobsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Smartling\Jobs\Params\AddLocaleToJobParameters;
use Smartling\Jobs\Params\CancelJobParameters;
use Smartling\Jobs\Params\CreateJobParameters;
use Smartling\Jobs\Params\JobProgressParameters;
use Smartling\Jobs\Params\ListJobsParameters;
use Smartling\Jobs\Params\SearchJobsParameters;
use Smartling\Jobs\Params\UpdateJobParameters;
Expand Down Expand Up @@ -233,4 +234,18 @@ public function checkAsynchronousProcessingStatus($jobId, $processId)
return $this->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET);
}

/**
* Returns the status of a job
*
* @param string $jobId Job Id
* @param JobProgressParameters $parameters Job Progress parameters
* @return array
* @throws SmartlingApiException
*/
public function getJobProgress($jobId, JobProgressParameters $parameters) {
$endpoint = \vsprintf('jobs/%s/progress', [$jobId]);
$requestData = $this->getDefaultRequestData('query', $parameters->exportToArray());

return $this->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET);
}
}
17 changes: 17 additions & 0 deletions src/Jobs/Params/JobProgressParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Smartling\Jobs\Params;

use Smartling\Parameters\BaseParameters;

class JobProgressParameters extends BaseParameters
{

/**
* @param string $localeId
*/
public function setTargetLocaleId($localeId) {
$this->set('targetLocaleId', $localeId);
}

}
19 changes: 19 additions & 0 deletions tests/functional/JobsApiFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Smartling\Jobs\Params\AddLocaleToJobParameters;
use Smartling\Jobs\Params\CancelJobParameters;
use Smartling\Jobs\Params\CreateJobParameters;
use Smartling\Jobs\Params\JobProgressParameters;
use Smartling\Jobs\Params\ListJobsParameters;
use Smartling\Jobs\Params\SearchJobsParameters;
use Smartling\Jobs\Params\UpdateJobParameters;
Expand Down Expand Up @@ -303,4 +304,22 @@ public function testJobsApiCheckAsynchronousProcessingStatus()
}
}

/**
* Test for retrieving job progress
*/
public function testJobsApiGetJobProgress()
{
try {
$params = new JobProgressParameters();
$params->setTargetLocaleId('fr');

$result = $this->jobsApi->getJobProgress($this->jobId, $params);

$this->assertArrayHasKey('contentProgressReport', $result);
$this->assertArrayHasKey('progress', $result);
} catch (SmartlingApiException $e) {
$this->fail($e->getMessage());
}
}

}
39 changes: 39 additions & 0 deletions tests/unit/JobsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Smartling\Jobs\Params\AddLocaleToJobParameters;
use Smartling\Jobs\Params\CancelJobParameters;
use Smartling\Jobs\Params\CreateJobParameters;
use Smartling\Jobs\Params\JobProgressParameters;
use Smartling\Jobs\Params\ListJobsParameters;
use Smartling\Jobs\Params\SearchJobsParameters;
use Smartling\Jobs\Params\UpdateJobParameters;
Expand Down Expand Up @@ -503,4 +504,42 @@ public function testCreateJobParametersSetCallbackMethodValidation()
{
(new CreateJobParameters())->setCallbackMethod("TEST");
}

/**
* @covers \Smartling\Jobs\JobsApi::getJobProgress
*
*/
public function testGetJobProgress()
{
$jobId = 'Some job id';
$localeId = 'some locale id';
$endpointUrl = \vsprintf('%s/%s/jobs/%s/progress', [
JobsApi::ENDPOINT_URL,
$this->projectId,
$jobId,
]);

$params = new JobProgressParameters();
$params->setTargetLocaleId($localeId);

$this->client
->expects(self::once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => \vsprintf('%s %s', [
$this->authProvider->getTokenType(),
$this->authProvider->getAccessToken(),
]),
],
'exceptions' => FALSE,
'query' => [
'targetLocaleId' => $localeId
],
])
->willReturn($this->responseMock);

$this->object->getJobProgress($jobId, $params);
}
}