Commit 67765269 authored by mickael's avatar mickael
Browse files

add fetch method for client

parent 155ec9ad
......@@ -3,21 +3,37 @@ declare(strict_types=1);
namespace Certic\Circe;
use PHP_CodeSniffer\Tokenizers\PHP;
class Client
{
private $endpoint;
private $uuid;
private $secret;
public function __construct(string $api_endpoint, string $secret_key, string $app_uuid)
public function __construct(string $api_endpoint = null, string $secret_key = null, string $app_uuid = null)
{
$this->endpoint = $api_endpoint;
$this->secret = $secret_key;
$this->uuid = $app_uuid;
$this->endpoint = $api_endpoint ?: getenv('CIRCE_ENDPOINT');
$this->secret = $secret_key ?: getenv('CIRCE_SECRET');
$this->uuid = $app_uuid ?: getenv('CIRCE_APP_UUID');
if (!$this->endpoint || !$this->secret || !$this->uuid) {
throw new Exception("Credentials and/or endpoint not set");
}
}
public function availableTransformations(): array
{
$endpoint = $this->endpoint . '/transformations/';
$endpoint = \str_replace('//transformations/', '/transformations/', $endpoint);
$body = file_get_contents($endpoint);
if ($body === false) {
throw new Exception('Could not fetch available transformations.');
}
$data = json_decode($body, true);
if (is_null($data)) {
throw new Exception('Could not fetch available transformations.');
}
return $data;
}
public function send(Job $job, bool $wait = false, string $destination_file = null)
......@@ -28,6 +44,7 @@ class Client
$job_archive_path = $job->makeArchive();
$job_hash = \hash_hmac_file("sha256", $job_archive_path, $this->secret);
$endpoint = $wait ? $this->endpoint . '/job/?block=1' : $this->endpoint . "/job/";
$endpoint = \str_replace('//job', '/job', $endpoint);
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, $endpoint);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
......@@ -41,31 +58,62 @@ class Client
]);
$output = \curl_exec($ch);
if ($output === false) {
throw new Exception(curl_error($ch));
throw new Exception(\curl_error($ch));
}
$this->checkStatusCode($ch);
if ($wait) {
file_put_contents($destination_file, $output);
\file_put_contents($destination_file, $output);
$job->setResultFilePath($destination_file);
} else {
$job->setUuid($output);
}
}
public function poll(Job $job, string $destination_file = null, int $timeout = 30, int $poll_interval = 1)
public function fetch(Job $job, string $destination_file = null)
{
if (!$job->getUuid()) {
$this->send($job);
throw new Exception('Job has to be first sent to Circe server.');
}
if (!$destination_file) {
$destination_file = \tempnam(\sys_get_temp_dir(), "circe-") . '.tar.gz';
$destination_file = \tempnam(\sys_get_temp_dir(), "circe") . '.tar.gz';
}
$then = time();
while (true) {
$endpoint = $this->endpoint . '/job/' . $job->getUuid();
$endpoint = \str_replace('//job', '/job', $endpoint);
$job_hash = \hash_hmac("sha256", $job->getUuid(), $this->secret);
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, $endpoint);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization:' . $this->uuid . ' ' . $job_hash,
]);
$output = \curl_exec($ch);
if ($output === false) {
throw new Exception(\curl_error($ch));
}
$this->checkStatusCode($ch);
\file_put_contents($destination_file, $output);
$job->setResultFilePath($destination_file);
}
public function newJob(): Job
{
return new Job();
}
private function checkStatusCode($curl_handle)
{
$infos = curl_getinfo($curl_handle);
if ($infos['http_code'] !== 200) {
if ($infos['http_code'] === 202) {
throw new Exception('Job exists but result is not ready yet, try again later.');
}
if ($infos['http_code'] === 404) {
throw new Exception('No such job on the server.');
}
if ($infos['http_code'] === 403) {
throw new Exception('Forbidden. Check your credentials.');
}
}
}
}
......@@ -13,6 +13,11 @@ class Job
private $uuid;
public function __construct(string $uuid = null)
{
$this->uuid = $uuid;
}
public function addFile(string $file_path): Job
{
$this->files[] = $file_path;
......@@ -42,7 +47,7 @@ class Job
}
$this->dir_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . DIRECTORY_SEPARATOR;
if (!mkdir($this->dir_path, 0777, true)) {
throw new Exception("Could not create temporary directory");
throw new Exception("Could not create temporary directory.");
}
$tar_path = $this->dir_path . "job.tar";
$tar = new \PharData($tar_path);
......@@ -59,7 +64,7 @@ class Job
foreach ($this->files as $file) {
$tar->addFile($file, basename($file));
$tar->addFile($file, \basename($file));
}
$tar->compress(\Phar::GZ);
......@@ -68,15 +73,17 @@ class Job
public function __destruct()
{
$this->deleteTree($this->dir_path);
if ($this->dir_path) {
$this->deleteTree($this->dir_path);
}
}
private function deleteTree(string $folder, bool $keepRootFolder = false): bool
{
if (empty($folder) || !file_exists($folder)) {
if (empty($folder) || !\file_exists($folder)) {
return true;
} elseif (is_file($folder) || is_link($folder)) {
return @unlink($folder);
} elseif (\is_file($folder) || \is_link($folder)) {
return @\unlink($folder);
}
$files = new \RecursiveIteratorIterator(
......@@ -96,6 +103,9 @@ class Job
public function getResultFilePath(): string
{
if (!$this->result_file_path) {
throw new Exception('Fetch job result file from Circe server first.');
}
return $this->result_file_path;
}
......@@ -118,8 +128,6 @@ class Job
public function getResult(): JobResult
{
if ($this->getResultFilePath()) {
return new JobResult($this->getResultFilePath());
}
return new JobResult($this->getResultFilePath());
}
}
......@@ -14,8 +14,13 @@ class JobResult
public function getFiles(): \Iterator
{
$tar = new \PharData($this->result_file_path);
foreach ($tar as $file){
foreach ($tar as $file) {
yield $file;
}
}
public function __destruct()
{
@\unlink($this->result_file_path);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment