mirror of
https://github.com/ovh/php-ovh.git
synced 2023-11-05 03:20:26 +01:00
refactor: hint and simplify if/else
This commit is contained in:
parent
6bfd3cac33
commit
c042884a32
67
src/Api.php
67
src/Api.php
@ -31,8 +31,11 @@
|
|||||||
namespace Ovh;
|
namespace Ovh;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\ClientException;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use GuzzleHttp\Psr7\Request;
|
use GuzzleHttp\Psr7\Request;
|
||||||
use GuzzleHttp\Psr7\Response;
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper to manage login and exchanges with simpliest Ovh API
|
* Wrapper to manage login and exchanges with simpliest Ovh API
|
||||||
@ -68,42 +71,42 @@ class Api
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $endpoint = null;
|
private ?string $endpoint;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain key of the current application
|
* Contain key of the current application
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $application_key = null;
|
private ?string $application_key;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain secret of the current application
|
* Contain secret of the current application
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $application_secret = null;
|
private ?string $application_secret;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain consumer key of the current application
|
* Contain consumer key of the current application
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $consumer_key = null;
|
private ?string $consumer_key;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain delta between local timestamp and api server timestamp
|
* Contain delta between local timestamp and api server timestamp
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $time_delta = null;
|
private ?string $time_delta;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain http client connection
|
* Contain http client connection
|
||||||
*
|
*
|
||||||
* @var Client
|
* @var Client
|
||||||
*/
|
*/
|
||||||
private $http_client = null;
|
private ?Client $http_client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new wrapper instance
|
* Construct a new wrapper instance
|
||||||
@ -135,9 +138,9 @@ class Api
|
|||||||
} else {
|
} else {
|
||||||
if (!array_key_exists($api_endpoint, $this->endpoints)) {
|
if (!array_key_exists($api_endpoint, $this->endpoints)) {
|
||||||
throw new Exceptions\InvalidParameterException("Unknown provided endpoint");
|
throw new Exceptions\InvalidParameterException("Unknown provided endpoint");
|
||||||
} else {
|
|
||||||
$this->endpoint = $this->endpoints[$api_endpoint];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->endpoint = $this->endpoints[$api_endpoint];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($http_client)) {
|
if (!isset($http_client)) {
|
||||||
@ -151,13 +154,12 @@ class Api
|
|||||||
$this->application_secret = $application_secret;
|
$this->application_secret = $application_secret;
|
||||||
$this->http_client = $http_client;
|
$this->http_client = $http_client;
|
||||||
$this->consumer_key = $consumer_key;
|
$this->consumer_key = $consumer_key;
|
||||||
$this->time_delta = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate time delta between local machine and API's server
|
* 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
|
* @return int
|
||||||
*/
|
*/
|
||||||
private function calculateTimeDelta()
|
private function calculateTimeDelta()
|
||||||
@ -184,7 +186,7 @@ class Api
|
|||||||
* @param string $redirection url to redirect on your website after authentication
|
* @param string $redirection url to redirect on your website after authentication
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws ClientException if http request is an error
|
||||||
*/
|
*/
|
||||||
public function requestCredentials(
|
public function requestCredentials(
|
||||||
array $accessRules,
|
array $accessRules,
|
||||||
@ -218,10 +220,13 @@ class Api
|
|||||||
* @param \stdClass|array|null $content body of the request
|
* @param \stdClass|array|null $content body of the request
|
||||||
* @param bool $is_authenticated if the request use authentication
|
* @param bool $is_authenticated if the request use authentication
|
||||||
*
|
*
|
||||||
* @return array
|
* @param null $headers
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @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)) {
|
if (!isset($this->application_key)) {
|
||||||
@ -235,7 +240,7 @@ class Api
|
|||||||
|
|
||||||
$url = $this->endpoint . $path;
|
$url = $this->endpoint . $path;
|
||||||
$request = new Request($method, $url);
|
$request = new Request($method, $url);
|
||||||
if (isset($content) && $method == 'GET') {
|
if (isset($content) && $method === 'GET') {
|
||||||
$query_string = $request->getUri()->getQuery();
|
$query_string = $request->getUri()->getQuery();
|
||||||
|
|
||||||
$query = array();
|
$query = array();
|
||||||
@ -264,7 +269,7 @@ class Api
|
|||||||
$request = $request->withUri($url);
|
$request = $request->withUri($url);
|
||||||
$body = "";
|
$body = "";
|
||||||
} elseif (isset($content)) {
|
} 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);
|
$request->getBody()->write($body);
|
||||||
} else {
|
} else {
|
||||||
@ -304,10 +309,11 @@ class Api
|
|||||||
* @param Response $response
|
* @param Response $response
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws \JsonException
|
||||||
*/
|
*/
|
||||||
private function decodeResponse(Response $response)
|
private function decodeResponse(Response $response): array
|
||||||
{
|
{
|
||||||
return json_decode($response->getBody(), true);
|
return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -319,21 +325,22 @@ class Api
|
|||||||
* @param bool is_authenticated if the request need to be authenticated
|
* @param bool is_authenticated if the request need to be authenticated
|
||||||
*
|
*
|
||||||
* @return array
|
* @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)
|
public function get($path, $content = null, $headers = null, $is_authenticated = true): array
|
||||||
{
|
{
|
||||||
if (preg_match('/^\/[^\/]+\.json$/', $path)) {
|
if (preg_match('/^\/[^\/]+\.json$/', $path)) {
|
||||||
// Schema description must be access without authentication
|
// Schema description must be access without authentication
|
||||||
return $this->decodeResponse(
|
return $this->decodeResponse(
|
||||||
$this->rawCall("GET", $path, $content, false, $headers)
|
$this->rawCall("GET", $path, $content, false, $headers)
|
||||||
);
|
);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return $this->decodeResponse(
|
return $this->decodeResponse(
|
||||||
$this->rawCall("GET", $path, $content, $is_authenticated, $headers)
|
$this->rawCall("GET", $path, $content, $is_authenticated, $headers)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap call to Ovh APIs for POST requests
|
* Wrap call to Ovh APIs for POST requests
|
||||||
@ -344,9 +351,9 @@ class Api
|
|||||||
* @param bool is_authenticated if the request need to be authenticated
|
* @param bool is_authenticated if the request need to be authenticated
|
||||||
*
|
*
|
||||||
* @return array
|
* @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)
|
public function post($path, $content = null, $headers = null, $is_authenticated = true): array
|
||||||
{
|
{
|
||||||
return $this->decodeResponse(
|
return $this->decodeResponse(
|
||||||
$this->rawCall("POST", $path, $content, $is_authenticated, $headers)
|
$this->rawCall("POST", $path, $content, $is_authenticated, $headers)
|
||||||
@ -362,9 +369,9 @@ class Api
|
|||||||
* @param bool is_authenticated if the request need to be authenticated
|
* @param bool is_authenticated if the request need to be authenticated
|
||||||
*
|
*
|
||||||
* @return array
|
* @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)
|
public function put($path, $content, $headers = null, $is_authenticated = true): array
|
||||||
{
|
{
|
||||||
return $this->decodeResponse(
|
return $this->decodeResponse(
|
||||||
$this->rawCall("PUT", $path, $content, $is_authenticated, $headers)
|
$this->rawCall("PUT", $path, $content, $is_authenticated, $headers)
|
||||||
@ -380,9 +387,9 @@ class Api
|
|||||||
* @param bool is_authenticated if the request need to be authenticated
|
* @param bool is_authenticated if the request need to be authenticated
|
||||||
*
|
*
|
||||||
* @return array
|
* @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)
|
public function delete($path, $content = null, $headers = null, $is_authenticated = true): array
|
||||||
{
|
{
|
||||||
return $this->decodeResponse(
|
return $this->decodeResponse(
|
||||||
$this->rawCall("DELETE", $path, $content, $is_authenticated, $headers)
|
$this->rawCall("DELETE", $path, $content, $is_authenticated, $headers)
|
||||||
@ -392,7 +399,7 @@ class Api
|
|||||||
/**
|
/**
|
||||||
* Get the current consumer key
|
* Get the current consumer key
|
||||||
*/
|
*/
|
||||||
public function getConsumerKey()
|
public function getConsumerKey(): ?string
|
||||||
{
|
{
|
||||||
return $this->consumer_key;
|
return $this->consumer_key;
|
||||||
}
|
}
|
||||||
@ -400,7 +407,7 @@ class Api
|
|||||||
/**
|
/**
|
||||||
* Return instance of http client
|
* Return instance of http client
|
||||||
*/
|
*/
|
||||||
public function getHttpClient()
|
public function getHttpClient(): ?Client
|
||||||
{
|
{
|
||||||
return $this->http_client;
|
return $this->http_client;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user