Skip to content

Commit

Permalink
Added async mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Jul 30, 2013
1 parent 9a532b8 commit c202ecf
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 6 deletions.
114 changes: 110 additions & 4 deletions src/Bytes/Docraptor/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
60 changes: 58 additions & 2 deletions tests/Bytes/Docraptor/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit c202ecf

Please sign in to comment.