Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on Guzzle #41

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"description": "Wrapper for OVH APIs",
"license": "BSD-3-Clause",
"require": {
"guzzlehttp/guzzle": "^6.0"
"guzzlehttp/psr7": "~1.2",
"php-http/httplug": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^0.8"
},
"autoload": {
"psr-4": {"Ovh\\": "src/"}
Expand All @@ -12,6 +15,7 @@
"phpunit/phpunit": "4.*",
"phpdocumentor/phpdocumentor": "2.*",
"squizlabs/php_codesniffer": "2.*",
"phing/phing": "^2.14"
"phing/phing": "^2.14",
"php-http/guzzle6-adapter": "^1.0"
}
}
77 changes: 45 additions & 32 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@

namespace Ovh;

use GuzzleHttp\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Http\Discovery\HttpClientDiscovery;

/**
* Wrapper to manage login and exchanges with simpliest Ovh API
Expand Down Expand Up @@ -100,7 +105,7 @@ class Api
/**
* Contain http client connection
*
* @var Client
* @var HttpClient
*/
private $http_client = null;

Expand All @@ -114,7 +119,7 @@ class Api
* @param string $api_endpoint name of api selected
* @param string $consumer_key If you have already a consumer key, this parameter prevent to do a
* new authentication
* @param Client $http_client instance of http client
* @param HttpClient $http_client instance of http client
*
* @throws Exceptions\InvalidParameterException if one parameter is missing or with bad value
*/
Expand All @@ -123,7 +128,7 @@ public function __construct(
$application_secret,
$api_endpoint,
$consumer_key = null,
Client $http_client = null
HttpClient $http_client = null
) {
if (!isset($application_key)) {
throw new Exceptions\InvalidParameterException("Application key parameter is empty");
Expand All @@ -141,13 +146,6 @@ public function __construct(
throw new Exceptions\InvalidParameterException("Unknown provided endpoint");
}

if (!isset($http_client)) {
$http_client = new Client([
'timeout' => 30,
'connect_timeout' => 5,
]);
}

$this->application_key = $application_key;
$this->endpoint = $this->endpoints[$api_endpoint];
$this->application_secret = $application_secret;
Expand All @@ -159,7 +157,7 @@ public function __construct(
/**
* Calculate time delta between local machine and API's server
*
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws \Http\Exception\TransferException if http request is an error
* @return int
*/
private function calculateTimeDelta()
Expand All @@ -186,7 +184,7 @@ private function calculateTimeDelta()
* @param string $redirection url to redirect on your website after authentication
*
* @return mixed
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws \Http\Exception\TransferException if http request is an error
*/
public function requestCredentials(
array $accessRules,
Expand Down Expand Up @@ -221,14 +219,16 @@ public function requestCredentials(
* @param bool $is_authenticated if the request use authentication
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws \Http\Exception\TransferException if http request is an error
*/
private function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null)
{
$url = $this->endpoint . $path;
$request = new Request($method, $url);
$uri = new Uri($url);
$body = null;

if (isset($content) && $method == 'GET') {
$query_string = $request->getUri()->getQuery();
$query_string = $uri->getQuery();

$query = array();
if (!empty($query_string)) {
Expand All @@ -251,19 +251,11 @@ private function rawCall($method, $path, $content = null, $is_authenticated = tr
}

$query = \GuzzleHttp\Psr7\build_query($query);

$url = $request->getUri()->withQuery($query);
$request = $request->withUri($url);
$body = "";
$uri = $uri->withQuery($query);
} elseif (isset($content)) {
$body = json_encode($content);

$request->getBody()->write($body);
} else {
$body = "";
}
if(!is_array($headers))
{
if (!is_array($headers)) {
$headers = [];
}
$headers['Content-Type'] = 'application/json; charset=utf-8';
Expand All @@ -286,8 +278,13 @@ private function rawCall($method, $path, $content = null, $is_authenticated = tr
}
}

/** @var Response $response */
return $this->http_client->send($request, ['headers' => $headers]);
/** @var ResponseInterface $response */
return $this->sendRequest(new Request(
$method,
$uri,
$headers,
$body
));
}

/**
Expand All @@ -309,7 +306,7 @@ private function decodeResponse(Response $response)
* @param array $content content to send inside body of request
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws \Http\Exception\TransferException if http request is an error
*/
public function get($path, $content = null, $headers = null)
{
Expand All @@ -325,7 +322,7 @@ public function get($path, $content = null, $headers = null)
* @param array $content content to send inside body of request
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws \Http\Exception\TransferException if http request is an error
*/
public function post($path, $content = null, $headers = null)
{
Expand All @@ -341,7 +338,7 @@ public function post($path, $content = null, $headers = null)
* @param array $content content to send inside body of request
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws \Http\Exception\TransferException if http request is an error
*/
public function put($path, $content, $headers = null)
{
Expand All @@ -357,7 +354,7 @@ public function put($path, $content, $headers = null)
* @param array $content content to send inside body of request
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws \Http\Exception\TransferException if http request is an error
*/
public function delete($path, $content = null, $headers = null)
{
Expand All @@ -368,6 +365,7 @@ public function delete($path, $content = null, $headers = null)

/**
* Get the current consumer key
* @return string
*/
public function getConsumerKey()
{
Expand All @@ -376,9 +374,24 @@ public function getConsumerKey()

/**
* Return instance of http client
* @return HttpClient
*/
public function getHttpClient()
{
if ($this->http_client === null) {
$this->http_client = HttpClientDiscovery::find();
}

return $this->http_client;
}

/**
* Send the Request through the HttpClient
* @param RequestInterface $request
* @return ResponseInterface
*/
private function sendRequest(RequestInterface $request)
{
return $this->getHttpClient()->sendRequest($request);
}
}
4 changes: 2 additions & 2 deletions tests/ApiFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace Ovh\tests;

use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client;
use Ovh\Api;

/**
Expand Down Expand Up @@ -238,7 +238,7 @@ public function testIfRequestWithoutAuthenticationWorks()
*/
public function testApiGetWithParameters()
{
$this->setExpectedException('\\GuzzleHttp\\Exception\\ClientException', '400');
$this->setExpectedException('Http\Client\Exception\HttpException', '400');

$this->api->get('/me/accessRestriction/ip', ['foo' => 'bar']);
}
Expand Down
Loading