Skip to content

Commit

Permalink
Update guzzle client (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Ducoudray authored and cdaguerre committed May 24, 2016
1 parent cbb83e2 commit 84d2016
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 946 deletions.
9 changes: 1 addition & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ php:
- 7.0
- hhvm

matrix:
include:
- php: 5.3
env: PHPUNIT_FLAGS='--exclude-group sylius'
- php: 5.4
env: PHPUNIT_FLAGS='--exclude-group sylius'

cache:
directories:
- $HOME/.composer/cache
Expand All @@ -25,4 +18,4 @@ before_script:

script:
- composer validate
- vendor/bin/phpunit $PHPUNIT_FLAGS
- vendor/bin/phpunit
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
{ "name": "Pierre Ducoudray", "email": "[email protected]" }
],
"require": {
"php": ">=5.3.3",
"php": ">=5.5.0",
"ext-curl": "*",
"guzzle/guzzle": "~3.7",
"guzzlehttp/guzzle": "^6.2",
"phpspec/php-diff": "^1.0"
},
"require-dev": {
Expand All @@ -33,7 +33,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "0.2.x-dev"
}
}
}
65 changes: 27 additions & 38 deletions lib/Textmaster/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Textmaster\Api;

use GuzzleHttp\Psr7\Response;
use Textmaster\Client;
use Textmaster\HttpClient\Message\ResponseMediator;

Expand Down Expand Up @@ -103,11 +104,11 @@ public function getClient()
* @param array $parameters GET parameters.
* @param array $requestHeaders Request Headers.
*
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
* @return \GuzzleHttp\Psr7\Stream|mixed|\Psr\Http\Message\StreamInterface
*/
protected function get($path, array $parameters = array(), $requestHeaders = array())
protected function get($path, array $parameters = [], $requestHeaders = [])
{
$defaultParams = array('page' => 'page', 'per_page' => 'perPage');
$defaultParams = ['page' => 'page', 'per_page' => 'perPage'];

foreach ($defaultParams as $snake => $camel) {
if (isset($this->$camel) && !isset($parameters[$snake])) {
Expand All @@ -129,13 +130,13 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
* @param array $parameters HEAD parameters.
* @param array $requestHeaders Request headers.
*
* @return \Guzzle\Http\Message\Response
* @return Response
*/
protected function head($path, array $parameters = array(), $requestHeaders = array())
protected function head($path, array $parameters = [], $requestHeaders = [])
{
$response = $this->client->getHttpClient()->request($path, null, 'HEAD', $requestHeaders, array(
$response = $this->client->getHttpClient()->request($path, [], 'HEAD', $requestHeaders, [
'query' => $parameters,
));
]);

return $response;
}
Expand All @@ -144,16 +145,16 @@ protected function head($path, array $parameters = array(), $requestHeaders = ar
* Send a POST request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $body POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
* @return \GuzzleHttp\Psr7\Stream|mixed|\Psr\Http\Message\StreamInterface
*/
protected function post($path, array $parameters = array(), $requestHeaders = array())
protected function post($path, array $body = [], $requestHeaders = [])
{
return $this->postRaw(
$path,
$this->createJsonBody($parameters),
$body,
$requestHeaders
);
}
Expand All @@ -162,12 +163,12 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
* Send a POST request with raw data.
*
* @param string $path Request path.
* @param string $body Request body.
* @param array $body Request body.
* @param array $requestHeaders Request headers.
*
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
* @return \GuzzleHttp\Psr7\Stream|mixed|\Psr\Http\Message\StreamInterface
*/
protected function postRaw($path, $body, $requestHeaders = array())
protected function postRaw($path, array $body, $requestHeaders = [])
{
$response = $this->client->getHttpClient()->post(
$path,
Expand All @@ -182,16 +183,16 @@ protected function postRaw($path, $body, $requestHeaders = array())
* Send a PATCH request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $body POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
* @return \GuzzleHttp\Psr7\Stream|mixed|\Psr\Http\Message\StreamInterface
*/
protected function patch($path, array $parameters = array(), $requestHeaders = array())
protected function patch($path, array $body = [], $requestHeaders = [])
{
$response = $this->client->getHttpClient()->patch(
$path,
$this->createJsonBody($parameters),
$body,
$requestHeaders
);

Expand All @@ -202,16 +203,16 @@ protected function patch($path, array $parameters = array(), $requestHeaders = a
* Send a PUT request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $body POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
* @return \GuzzleHttp\Psr7\Stream|mixed|\Psr\Http\Message\StreamInterface
*/
protected function put($path, array $parameters = array(), $requestHeaders = array())
protected function put($path, array $body = [], $requestHeaders = [])
{
$response = $this->client->getHttpClient()->put(
$path,
$this->createJsonBody($parameters),
$body,
$requestHeaders
);

Expand All @@ -222,31 +223,19 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
* Send a DELETE request with JSON-encoded parameters.
*
* @param string $path Request path.
* @param array $parameters POST parameters to be JSON encoded.
* @param array $body POST parameters to be JSON encoded.
* @param array $requestHeaders Request headers.
*
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
* @return \GuzzleHttp\Psr7\Stream|mixed|\Psr\Http\Message\StreamInterface
*/
protected function delete($path, array $parameters = array(), $requestHeaders = array())
protected function delete($path, array $body = [], $requestHeaders = [])
{
$response = $this->client->getHttpClient()->delete(
$path,
$this->createJsonBody($parameters),
$body,
$requestHeaders
);

return ResponseMediator::getContent($response);
}

/**
* Create a JSON encoded version of an array of parameters.
*
* @param array $parameters Request parameters
*
* @return null|string
*/
protected function createJsonBody(array $parameters)
{
return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0);
}
}
98 changes: 5 additions & 93 deletions lib/Textmaster/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@
*/
class Client
{
/**
* @var array
*/
private $options = array(
'base_url' => 'http://api.textmaster.com/%s/clients',
'api_version' => 'v1',
'sandbox' => false,
'user_agent' => 'php-textmaster-api (http://github.com/cdaguerre/php-textmaster-api)',
'timeout' => 10,
'cache_dir' => null,
);

/**
* @var HttpClientInterface
*/
Expand All @@ -72,10 +60,10 @@ class Client
/**
* Instantiate a new Textmaster client.
*
* @param null|HttpClientInterface $httpClient Textmaster http client
* @param HttpClientInterface $httpClient Textmaster http client
* @param null|EventDispatcherInterface $dispatcher Event dispatcher
*/
public function __construct(HttpClientInterface $httpClient = null, EventDispatcherInterface $dispatcher = null)
public function __construct(HttpClientInterface $httpClient, EventDispatcherInterface $dispatcher = null)
{
$this->httpClient = $httpClient;
$this->dispatcher = $dispatcher;
Expand All @@ -91,9 +79,9 @@ public function __construct(HttpClientInterface $httpClient = null, EventDispatc
public function api($name)
{
$name = Inflector::singularize($name);
$apis = array(
$apis = [
'author', 'billing', 'bundle', 'category', 'expertise', 'language', 'locale', 'project', 'template', 'user',
);
];

if (!in_array($name, $apis, true)) {
throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name));
Expand All @@ -104,37 +92,14 @@ public function api($name)
return new $class($this);
}

/**
* Authenticate a user for all next requests.
*
* @param string $key Textmaster api key
* @param string $secret Textmaster secret
*/
public function authenticate($key, $secret)
{
$this->getHttpClient()->authenticate($key, $secret);
}

/**
* @return HttpClient
*/
public function getHttpClient()
{
if (null === $this->httpClient) {
$this->httpClient = new HttpClient($this->options);
}

return $this->httpClient;
}

/**
* @param HttpClientInterface $httpClient
*/
public function setHttpClient(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}

/**
* @return EventDispatcherInterface
*/
Expand All @@ -155,52 +120,9 @@ public function setEventDispatcher(EventDispatcherInterface $dispatcher)
$this->dispatcher = $dispatcher;
}

/**
* Clears used headers.
*/
public function clearHeaders()
{
$this->getHttpClient()->clearHeaders();
}

/**
* @param array $headers
*/
public function setHeaders(array $headers)
{
$this->getHttpClient()->setHeaders($headers);
}

/**
* @param string $name
*
* @throws InvalidArgumentException
*
* @return mixed
*/
public function getOption($name)
{
$this->validateOptionExistence($name);

return $this->options[$name];
}

/**
* @param string $name
* @param mixed $value
*
* @throws InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setOption($name, $value)
{
$this->validateOptionExistence($name);

$this->options[$name] = $value;
}

/**
* @param string $name
* @param string $args
*
* @throws BadMethodCallException
*
Expand All @@ -214,14 +136,4 @@ public function __call($name, $args)
throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name));
}
}

/**
* @param string $name
*/
private function validateOptionExistence($name)
{
if (!array_key_exists($name, $this->options)) {
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
}
}
}
Loading

0 comments on commit 84d2016

Please sign in to comment.