From c202ecf505cbc212024de3d80f8ff4741b48cbb4 Mon Sep 17 00:00:00 2001 From: Manuel Reinhard Date: Tue, 30 Jul 2013 15:00:37 +0200 Subject: [PATCH] Added async mode --- src/Bytes/Docraptor/Client.php | 114 ++++++++++++++++++++++++++- tests/Bytes/Docraptor/ClientTest.php | 60 +++++++++++++- 2 files changed, 168 insertions(+), 6 deletions(-) diff --git a/src/Bytes/Docraptor/Client.php b/src/Bytes/Docraptor/Client.php index 6bba380..df40b5a 100644 --- a/src/Bytes/Docraptor/Client.php +++ b/src/Bytes/Docraptor/Client.php @@ -31,6 +31,20 @@ class Client implements ClientInterface */ private $_testMode; + /** + * Flag indicating mode of asynchronous job + * + * @var boolean + */ + private $_asyncMode; + + /** + * The optional callback url if querying as asynchronous job + * + * @var string + */ + private $_callbackUrl; + /** * Create new client @@ -40,6 +54,7 @@ public function __construct(HttpClientInterface $httpClient, $apiKey) $this->setHttpClient($httpClient); $this->setApiKey($apiKey); $this->setTestMode(false); + $this->setAsyncMode(false); $httpClient ->setBaseUrl('https://docraptor.com') @@ -131,6 +146,56 @@ public function getTestMode() return $this->_testMode; } + /** + * Set async job + * + * @param boolean $asyncMode Flag indicating the async mode + * @return Client + */ + public function setAsyncMode($asyncMode) + { + if (!is_bool($asyncMode)) { + throw new \InvalidArgumentException('The asyncMode must be a boolean, '.gettype($asyncMode).' given.'); + } + + $this->_asyncMode = $asyncMode; + + return $this; + } + + /** + * Get test mode + * + * @return boolean + */ + public function getAsyncMode() + { + return $this->_asyncMode; + } + + /** + * Set callback url + * + * @param $callbackUrl + * @return Client + */ + public function setCallbackUrl($callbackUrl) + { + $this->_callbackUrl = $callbackUrl; + + return $this; + } + + /** + * Returns callback url + * + * @return string + */ + public function getCallbackUrl() + { + return $this->_callbackUrl; + } + /** * Convert document using docraptor API * @@ -164,9 +229,43 @@ public function convert(DocumentInterface $document) } } + //If in async mode the response will be in json + if ($this->getAsyncMode()) { + return json_decode($response->getBody(true)); + } + return $response->getBody(true); } + /** + * Returns status information for a document which has been converted in async mode. + * + * The status id is returned by the convert() method if a document is converted in async mode. + * + * @param string $statusId + * @return array + */ + public function getStatus($statusId) + { + $request = $this->getHttpClient()->get('/status/'.$statusId); + $response = $request->send(); + + if ($response->getStatusCode() != 200) { + switch ($response->getStatusCode()) { + case 400: + throw new Exception\BadRequestException(); + case 401: + throw new Exception\UnauthorizedException(); + case 403: + throw new Exception\ForbiddenException(); + default: + throw new Exception\UnexpectedValueException($response->getStatusCode()); + } + } + + return json_decode($response->getBody(true)); + } + /** * Build a complete parameter list by merging document-specific parameters * with client-specific parameters. Client-specific parameters are given @@ -175,12 +274,19 @@ public function convert(DocumentInterface $document) * @param array $parameters Document-specific parameters * @return array Merged parameters, giving precedence to client-parameters */ - private function _buildParameters($parameters) + private function _buildParameters($docParameters) { - return array_replace_recursive($parameters, array( + $clientParameters = array( 'doc' => array( - 'test' => var_export($this->getTestMode(), true) + 'test' => var_export($this->getTestMode(), true), + 'async' => var_export($this->getAsyncMode(), true), ) - )); + ); + + if ($this->getAsyncMode() && null !== $this->getCallbackUrl()) { + $clientParameters['doc']['callback_url'] = $this->getCallbackUrl(); + } + + return array_replace_recursive($docParameters, $clientParameters); } } diff --git a/tests/Bytes/Docraptor/ClientTest.php b/tests/Bytes/Docraptor/ClientTest.php index d885899..e988ef3 100644 --- a/tests/Bytes/Docraptor/ClientTest.php +++ b/tests/Bytes/Docraptor/ClientTest.php @@ -79,7 +79,7 @@ public function testSettingInvalidApiKey() /** * Assert that by default the test mode is enabled */ - public function testDefaultTestmode() + public function testDefaultTestMode() { $httpClient = $this->_getHttpClientMock(null); @@ -118,7 +118,63 @@ public function testSettingInvalidTestMode() } /** - * Test interactiosn between docraptor client and http client + * Assert that by default the async mode is disabled + */ + public function testDefaultAsyncMode() + { + $httpClient = $this->_getHttpClientMock(null); + + $client = new Client($httpClient, '4pik3y'); + + $this->assertEquals(false, $client->getAsyncMode()); + } + + /** + * Test setting the async mode to a valid value + */ + public function testSettingValidAsnycMode() + { + $httpClient = $this->_getHttpClientMock(null); + + $client = new Client($httpClient, '4pik3y'); + + $this->assertEquals(false, $client->getAsyncMode()); + + $client->setAsyncMode(true); + + $this->assertEquals(true, $client->getAsyncMode()); + } + + /** + * Assert that by default the callback url is null + */ + public function testDefaultCallbackUrl() + { + $httpClient = $this->_getHttpClientMock(null); + + $client = new Client($httpClient, '4pik3y'); + + $this->assertEquals(null, $client->getCallbackUrl()); + } + + /** + * Test setting the callback url to a valid value + */ + public function testSettingValidCallbackUrl() + { + $httpClient = $this->_getHttpClientMock(null); + + $client = new Client($httpClient, '4pik3y'); + + $this->assertEquals(null, $client->getCallbackUrl()); + + $client->setCallbackUrl('http://www.mydomain.com/mypath'); + + $this->assertEquals('http://www.mydomain.com/mypath', $client->getCallbackUrl()); + } + + /** + * Test interaction between docraptor client and http client */ public function testSendDocument() {