Skip to content

Commit

Permalink
Merge pull request #106 from WelcomingGroup/master
Browse files Browse the repository at this point in the history
chore: bump to php 7.4, guzzle 7, phpunit 9
  • Loading branch information
rbeuque74 authored Jan 20, 2021
2 parents c473b3d + 951e4ad commit d14a0d1
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 141 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ addons:

language: php
php:
- 7.3
- 7.2
- 7.1
- 7.0
- 5.6
- 7.4

before_script:
- composer self-update
- composer install

script: vendor/bin/phing test -Donly.units=true
script: vendor/bin/phpunit tests/ApiTest.php
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
"description": "Wrapper for OVH APIs",
"license": "BSD-3-Clause",
"require": {
"guzzlehttp/guzzle": "^6.0"
"php": ">=7.4",
"guzzlehttp/guzzle": "^6.0||^7.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {"Ovh\\": "src/"}
},
"require-dev": {
"phpunit/phpunit": "4.*",
"phpdocumentor/phpdocumentor": "2.*",
"squizlabs/php_codesniffer": "2.*",
"phing/phing": "^2.14"
"phpunit/phpunit": "^9.5",
"phpdocumentor/phpdocumentor": "v3.0.0",
"squizlabs/php_codesniffer": "^3.5",
"phpdocumentor/graphviz": "^2.0@dev",
"symfony/flex": "^1.11"
}
}
34 changes: 17 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="OVH APIs wrapper">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="ENDPOINT" value=""/>
<env name="APP_KEY" value=""/>
<env name="APP_SECRET" value=""/>
<env name="CONSUMER" value=""/>
</php>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="OVH APIs wrapper">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="ENDPOINT" value=""/>
<env name="APP_KEY" value=""/>
<env name="APP_SECRET" value=""/>
<env name="CONSUMER" value=""/>
</php>
</phpunit>
115 changes: 53 additions & 62 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
namespace Ovh;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;

/**
* Wrapper to manage login and exchanges with simpliest Ovh API
Expand Down Expand Up @@ -68,42 +71,42 @@ class Api
*
* @var string
*/
private $endpoint = null;
private ?string $endpoint;

/**
* Contain key of the current application
*
* @var string
*/
private $application_key = null;
private ?string $application_key;

/**
* Contain secret of the current application
*
* @var string
*/
private $application_secret = null;
private ?string $application_secret;

/**
* Contain consumer key of the current application
*
* @var string
*/
private $consumer_key = null;
private ?string $consumer_key;

/**
* Contain delta between local timestamp and api server timestamp
*
* @var string
*/
private $time_delta = null;
private ?string $time_delta;

/**
* Contain http client connection
*
* @var Client
*/
private $http_client = null;
private ?Client $http_client;

/**
* Construct a new wrapper instance
Expand All @@ -130,19 +133,14 @@ public function __construct(
throw new Exceptions\InvalidParameterException("Endpoint parameter is empty");
}

if (preg_match('/^https?:\/\/..*/',$api_endpoint))
{
$this->endpoint = $api_endpoint;
}
else
{
if (!array_key_exists($api_endpoint, $this->endpoints)) {
throw new Exceptions\InvalidParameterException("Unknown provided endpoint");
}
else
{
if (preg_match('/^https?:\/\/..*/', $api_endpoint)) {
$this->endpoint = $api_endpoint;
} else {
if (!array_key_exists($api_endpoint, $this->endpoints)) {
throw new Exceptions\InvalidParameterException("Unknown provided endpoint");
}

$this->endpoint = $this->endpoints[$api_endpoint];
}
}

if (!isset($http_client)) {
Expand All @@ -156,13 +154,12 @@ public function __construct(
$this->application_secret = $application_secret;
$this->http_client = $http_client;
$this->consumer_key = $consumer_key;
$this->time_delta = null;
}

/**
* Calculate time delta between local machine and API's server
*
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws ClientException if http request is an error
* @return int
*/
private function calculateTimeDelta()
Expand All @@ -189,7 +186,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 ClientException if http request is an error
*/
public function requestCredentials(
array $accessRules,
Expand Down Expand Up @@ -218,18 +215,20 @@ public function requestCredentials(
* This is the main method of this wrapper. It will
* sign a given query and return its result.
*
* @param string $method HTTP method of request (GET,POST,PUT,DELETE)
* @param string $path relative url of API request
* @param \stdClass|array|null $content body of the request
* @param bool $is_authenticated if the request use authentication
* @param string $method HTTP method of request (GET,POST,PUT,DELETE)
* @param string $path relative url of API request
* @param \stdClass|array|null $content body of the request
* @param bool $is_authenticated if the request use authentication
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @param null $headers
* @return ResponseInterface
* @throws Exceptions\InvalidParameterException
* @throws GuzzleException
* @throws \JsonException
*/
protected function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null)
protected function rawCall($method, $path, $content = null, $is_authenticated = true, $headers = null): ResponseInterface
{
if ( $is_authenticated )
{
if ($is_authenticated) {
if (!isset($this->application_key)) {
throw new Exceptions\InvalidParameterException("Application key parameter is empty");
}
Expand All @@ -241,7 +240,7 @@ protected function rawCall($method, $path, $content = null, $is_authenticated =

$url = $this->endpoint . $path;
$request = new Request($method, $url);
if (isset($content) && $method == 'GET') {
if (isset($content) && $method === 'GET') {
$query_string = $request->getUri()->getQuery();

$query = array();
Expand Down Expand Up @@ -270,20 +269,18 @@ protected function rawCall($method, $path, $content = null, $is_authenticated =
$request = $request->withUri($url);
$body = "";
} elseif (isset($content)) {
$body = json_encode($content, JSON_UNESCAPED_SLASHES);
$body = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);

$request->getBody()->write($body);
} else {
$body = "";
}
if(!is_array($headers))
{
if (!is_array($headers)) {
$headers = [];
}
$headers['Content-Type'] = 'application/json; charset=utf-8';

if ($is_authenticated) {

$headers['X-Ovh-Application'] = $this->application_key;

if (!isset($this->time_delta)) {
Expand All @@ -309,41 +306,38 @@ protected function rawCall($method, $path, $content = null, $is_authenticated =
/**
* Decode a Response object body to an Array
*
* @param Response $response
* @param Response $response
*
* @return array
* @throws \JsonException
*/
private function decodeResponse(Response $response)
{
return json_decode($response->getBody(), true);
return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
}

/**
* Wrap call to Ovh APIs for GET requests
*
* @param string $path path ask inside api
* @param array $content content to send inside body of request
* @param string $path path ask inside api
* @param array $content content to send inside body of request
* @param array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws ClientException if http request is an error
* @throws \JsonException
*/
public function get($path, $content = null, $headers = null, $is_authenticated = true)
{
if(preg_match('/^\/[^\/]+\.json$/', $path))
{
// Schema description must be access without authentication
return $this->decodeResponse(
$this->rawCall("GET", $path, $content, false, $headers)
);
}
else
{
return $this->decodeResponse(
$this->rawCall("GET", $path, $content, $is_authenticated, $headers)
);
if (preg_match('/^\/[^\/]+\.json$/', $path)) {
// Schema description must be access without authentication
return $this->decodeResponse(
$this->rawCall("GET", $path, $content, false, $headers)
);
}

return $this->decodeResponse(
$this->rawCall("GET", $path, $content, $is_authenticated, $headers)
);
}

/**
Expand All @@ -354,8 +348,7 @@ public function get($path, $content = null, $headers = null, $is_authenticated =
* @param array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws ClientException if http request is an error
*/
public function post($path, $content = null, $headers = null, $is_authenticated = true)
{
Expand All @@ -372,8 +365,7 @@ public function post($path, $content = null, $headers = null, $is_authenticated
* @param array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws ClientException if http request is an error
*/
public function put($path, $content, $headers = null, $is_authenticated = true)
{
Expand All @@ -390,8 +382,7 @@ public function put($path, $content, $headers = null, $is_authenticated = true)
* @param array headers custom HTTP headers to add on the request
* @param bool is_authenticated if the request need to be authenticated
*
* @return array
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
* @throws ClientException if http request is an error
*/
public function delete($path, $content = null, $headers = null, $is_authenticated = true)
{
Expand All @@ -403,15 +394,15 @@ public function delete($path, $content = null, $headers = null, $is_authenticate
/**
* Get the current consumer key
*/
public function getConsumerKey()
public function getConsumerKey(): ?string
{
return $this->consumer_key;
}

/**
* Return instance of http client
*/
public function getHttpClient()
public function getHttpClient(): ?Client
{
return $this->http_client;
}
Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@
*/
class ApiException extends Exception
{

}
1 change: 0 additions & 1 deletion src/Exceptions/InvalidParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@
*/
class InvalidParameterException extends Exception
{

}
1 change: 0 additions & 1 deletion src/Exceptions/NotLoggedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@
*/
class NotLoggedException extends Exception
{

}
Loading

0 comments on commit d14a0d1

Please sign in to comment.