2018-12-14 12:50:44 +01:00
|
|
|
<?php
|
|
|
|
|
2019-07-19 15:23:00 +02:00
|
|
|
namespace API;
|
2018-12-14 12:50:44 +01:00
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classe per l'interazione con API esterne.
|
|
|
|
*
|
|
|
|
* @since 2.4.3
|
|
|
|
*/
|
2019-07-19 15:23:00 +02:00
|
|
|
class Services
|
2018-12-14 12:50:44 +01:00
|
|
|
{
|
|
|
|
protected static $client = null;
|
|
|
|
|
|
|
|
public static function isEnabled()
|
|
|
|
{
|
|
|
|
return !empty(setting('OSMCloud Services API Token'));
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:52:30 +01:00
|
|
|
public static function request($type, $resource, $data = [], $options = [])
|
2018-12-14 12:50:44 +01:00
|
|
|
{
|
|
|
|
$client = static::getClient();
|
|
|
|
|
|
|
|
$json = array_merge($data, [
|
|
|
|
'token' => setting('OSMCloud Services API Token'),
|
2019-07-19 17:58:03 +02:00
|
|
|
'version' => setting('OSMCloud Services API Version'),
|
2018-12-14 12:50:44 +01:00
|
|
|
'resource' => $resource,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$options = array_merge($options, [
|
2019-02-19 15:59:27 +01:00
|
|
|
'json' => $json,
|
2018-12-14 12:50:44 +01:00
|
|
|
'http_errors' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $client->request($type, '', $options);
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:52:30 +01:00
|
|
|
public static function responseBody($response)
|
2018-12-14 12:50:44 +01:00
|
|
|
{
|
|
|
|
$body = $response->getBody();
|
|
|
|
|
|
|
|
return json_decode($body, true) ?: [];
|
|
|
|
}
|
2018-12-29 12:03:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'oggetto per la connessione all'API del progetto.
|
|
|
|
*
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
protected static function getClient()
|
|
|
|
{
|
|
|
|
if (!isset(self::$client)) {
|
2019-04-19 03:22:49 +02:00
|
|
|
$url = setting('OSMCloud Services API URL');
|
|
|
|
|
2018-12-29 12:03:22 +01:00
|
|
|
self::$client = new Client([
|
2019-04-19 03:22:49 +02:00
|
|
|
'base_uri' => $url,
|
2018-12-29 12:03:22 +01:00
|
|
|
'verify' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$client;
|
|
|
|
}
|
2018-12-14 12:50:44 +01:00
|
|
|
}
|