Skip to content
Merged
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"guzzlehttp/guzzle": ">=4.0,<6.0"
"guzzlehttp/guzzle": "6.*"
},
"require-dev": {
"phpunit/phpunit": "4.0.*",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cloudconvert-php",
"version": "2.1.0",
"version": "2.2.0",
"project": "CloudConvert",
"dependencies": {
"grunt": "~0.4.1",
Expand Down
59 changes: 29 additions & 30 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use CloudConvert\Exceptions\InvalidParameterException;
use Exception;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ParseException;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Exception\RequestException;

/**
* Base Wrapper to manage login and exchanges with CloudConvert API
Expand Down Expand Up @@ -72,13 +71,14 @@ public function __construct($api_key, GuzzleClient $http_client = null)
* @param string $path relative url of API request
* @param string $content body of the request
* @param boolean $is_authenticated if the request use authentication
* @return mixed
*
* @throws Exception
* @throws Exceptions\ApiBadRequestException
* @throws Exceptions\ApiConversionFailedException
* @throws Exceptions\ApiException if the CloudConvert API returns an error
* @throws Exceptions\ApiTemporaryUnavailableException
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*/
private function rawCall($method, $path, $content = null, $is_authenticated = true)
{
Expand All @@ -89,49 +89,48 @@ private function rawCall($method, $path, $content = null, $is_authenticated = tr
$url = $this->protocol . '://' . $this->endpoint . $path;
}

$request = $this->http_client->createRequest($method, $url);
$options = array(
'query' => array(),
'body' => null,
'headers' => array()
);


if (is_array($content) && $method == 'GET') {
$query = $request->getQuery();
foreach ($content as $key => $value) {
$query->set($key, $value);
}
$options['query'] = $content;
} elseif (gettype($content) == 'resource' && $method == 'PUT') {
// is upload
$request->setBody(Stream::factory($content));
$options['body'] = \GuzzleHttp\Psr7\stream_for($content);

} elseif (is_array($content)) {
$body = json_encode($content);
$request->setBody(Stream::factory($body));
$request->setHeader('Content-Type', 'application/json; charset=utf-8');
$options['body'] = \GuzzleHttp\Psr7\stream_for($body);
$options['headers']['Content-Type'] = 'application/json; charset=utf-8';
}

if ($is_authenticated) {
$request->setHeader('Authorization', 'Bearer ' . $this->api_key);
$options['headers']['Authorization'] = 'Bearer ' . $this->api_key;
}




try {
$response = $this->http_client->send($request);
if (strpos($response->getHeader('Content-Type'), 'application/json') === 0) {
return $response->json();
$response = $this->http_client->request($method, $url, $options);
if ($response->getHeader('Content-Type') && strpos($response->getHeader('Content-Type')[0], 'application/json') === 0) {
return json_decode($response->getBody(), true);
} elseif ($response->getBody()->isReadable()) {
// if response is a download, return the stream
return $response->getBody();
}
} catch (Exception $e) {
} catch (RequestException $e) {
if (!$e->getResponse()) {
throw $e;
}
// check if response is JSON error message from the CloudConvert API
try {
$json = $e->getResponse()->json();
} catch (ParseException $parseexception) {
throw $e;
$json = json_decode($e->getResponse()->getBody(), true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new \RuntimeException('Error parsing JSON response');
}

if (isset($json['message']) || isset($json['error'])) {
$msg = isset($json['error']) ? $json['error'] : $json['message'];
$code = $e->getResponse()->getStatusCode();
Expand All @@ -143,7 +142,7 @@ private function rawCall($method, $path, $content = null, $is_authenticated = tr
throw new Exceptions\ApiTemporaryUnavailableException(
$msg,
$code,
$e->getResponse()->getHeader('Retry-After')
$e->getResponse()->getHeader('Retry-After') ? $e->getResponse()->getHeader('Retry-After')[0] : null
);
} else {
throw new Exceptions\ApiException($msg, $code);
Expand All @@ -162,7 +161,7 @@ private function rawCall($method, $path, $content = null, $is_authenticated = tr
* @param boolean $is_authenticated if the request use authentication
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function get($path, $content = null, $is_authenticated = true)
Expand All @@ -178,7 +177,7 @@ public function get($path, $content = null, $is_authenticated = true)
* @param boolean $is_authenticated if the request use authentication
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function post($path, $content, $is_authenticated = true)
Expand All @@ -194,7 +193,7 @@ public function post($path, $content, $is_authenticated = true)
* @param boolean $is_authenticated if the request use authentication
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function put($path, $content, $is_authenticated = true)
Expand All @@ -210,7 +209,7 @@ public function put($path, $content, $is_authenticated = true)
* @param boolean $is_authenticated if the request use authentication
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function delete($path, $content = null, $is_authenticated = true)
Expand Down Expand Up @@ -245,7 +244,7 @@ public function getHttpClient()
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function createProcess($parameters)
Expand All @@ -261,7 +260,7 @@ public function createProcess($parameters)
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function convert($parameters)
Expand Down
2 changes: 1 addition & 1 deletion src/ApiObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(Api $api, $url)
* @return \CloudConvert\ApiObject
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function refresh($parameters = null)
Expand Down
19 changes: 9 additions & 10 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace CloudConvert;

use CloudConvert\Exceptions\InvalidParameterException;
use GuzzleHttp\Stream\Stream;

/**
* CloudConvert Process Wrapper
Expand Down Expand Up @@ -39,7 +38,7 @@ public function __construct(Api $api, $url)
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/

Expand All @@ -48,7 +47,7 @@ public function start($parameters)
if (isset($parameters['file']) && gettype($parameters['file']) == 'resource') {
$file = $parameters['file'];
unset($parameters['file']);
if ($parameters['wait']) {
if (isset($parameters['wait']) && $parameters['wait']) {
unset($parameters['wait']);
$wait = true;
}
Expand All @@ -70,7 +69,7 @@ public function start($parameters)
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/

Expand All @@ -94,7 +93,7 @@ public function upload($stream, $filename = null)
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function wait()
Expand All @@ -116,7 +115,7 @@ public function wait()
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
* @throws Exceptions\InvalidParameterException
*
*/
Expand Down Expand Up @@ -146,7 +145,7 @@ public function download($localfile = null, $remotefile = null)
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function downloadStream($stream, $remotefile = null)
Expand All @@ -155,7 +154,7 @@ public function downloadStream($stream, $remotefile = null)
throw new Exceptions\ApiException("There is no output file available (yet)", 400);
}

$local = Stream::factory($stream);
$local = \GuzzleHttp\Psr7\stream_for($stream);
$download = $this->api->get($this->output->url . (isset($remotefile) ? '/' . rawurlencode($remotefile) : ''), false, false);
$local->write($download);
return $this;
Expand All @@ -169,7 +168,7 @@ public function downloadStream($stream, $remotefile = null)
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function downloadAll($directory = null)
Expand All @@ -192,7 +191,7 @@ public function downloadAll($directory = null)
* @return \CloudConvert\Process
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception if there is a general HTTP / network error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function delete()
Expand Down
18 changes: 13 additions & 5 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
use CloudConvert\Api;
use CloudConvert\Exceptions\ApiTemporaryUnavailableException;
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;


/**
* Tests of Api class
Expand Down Expand Up @@ -105,11 +108,16 @@ public function testIfProcessCreationWithInvalidFormatThrowsTheRightException()
*/
public function testIfApiTemporaryUnavailableExceptionIsThrown()
{
$client = new Client();
$mock = new Mock([
"HTTP/1.1 503 Service Unavailable\r\nRetry-After: 30\r\nContent-Type: application/json; charset=utf-8\r\n\r\n{\"message\":\"API unavailable. Please try later.\"}"

$mock = new MockHandler([
new Response(503, ['Retry-After' => 30, 'Content-Type' => 'application/json; charset=utf-8'], "{\"message\":\"API unavailable. Please try later.\"}"),
]);
$client->getEmitter()->attach($mock);

$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);



$api = new Api($this->api_key, $client);
$invoker = self::getPrivateMethod('rawCall');

Expand Down