first commit
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
|
||||
class AutomaticServerAPI
|
||||
{
|
||||
private static function handleResponse($response)
|
||||
{
|
||||
$res = json_decode($response->getBody());
|
||||
|
||||
$data = null;
|
||||
$error = null;
|
||||
$success = false;
|
||||
|
||||
if (!isset($res)) {
|
||||
return show_error(500);
|
||||
}
|
||||
|
||||
if (property_exists($res, 'success')) {
|
||||
$success = $res->success;
|
||||
}
|
||||
|
||||
if (property_exists($res, 'message')) {
|
||||
$error = $res->message;
|
||||
}
|
||||
|
||||
if (property_exists($res, 'data')) {
|
||||
$data = $res->data;
|
||||
}
|
||||
|
||||
return array_merge((array) $res, ['data' => $data, 'success' => $success, 'error' => $error]);
|
||||
}
|
||||
|
||||
public static function get($url, $params)
|
||||
{
|
||||
$CI = &get_instance();
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.automation_api_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.automation_api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$headers = ['Authorization' => "Bearer $token"];
|
||||
|
||||
try {
|
||||
$response = $client->request('GET', $url, [
|
||||
'headers' => $headers,
|
||||
'query' => $params,
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleResponse($response);
|
||||
}
|
||||
|
||||
public static function post($url, $params)
|
||||
{
|
||||
$CI = &get_instance();
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.automation_api_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.automation_api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$headers = ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'];
|
||||
try {
|
||||
$response = $client->request('POST', $url, [
|
||||
'headers' => $headers,
|
||||
'body' => json_encode($params),
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleResponse($response);
|
||||
}
|
||||
|
||||
public static function put($url, $params)
|
||||
{
|
||||
$CI = &get_instance();
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.automation_api_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.automation_api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$headers = ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'];
|
||||
|
||||
try {
|
||||
$response = $client->request('PUT', $url, [
|
||||
'headers' => $headers,
|
||||
'body' => json_encode($params),
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleResponse($response);
|
||||
}
|
||||
|
||||
public static function delete($url)
|
||||
{
|
||||
$CI = &get_instance();
|
||||
global $savvyext;
|
||||
$token = $savvyext->cfgReadChar('system.automation_api_token');
|
||||
$baseUri = $savvyext->cfgReadChar('system.automation_api_url');
|
||||
|
||||
$client = new Client(['base_uri' => $baseUri]);
|
||||
$headers = ['Authorization' => 'Bearer ' . $token];
|
||||
|
||||
try {
|
||||
$response = $client->request('DELETE', $url, [
|
||||
'headers' => $headers,
|
||||
]);
|
||||
} catch (ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
} catch (ClientException $e) {
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
return self::handleResponse($response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user