openstamanager/src/API/Services.php

82 lines
2.1 KiB
PHP
Raw Normal View History

2018-12-14 12:50:44 +01:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-12-14 12:50:44 +01:00
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
}