2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
2020-05-19 15:48:24 +03:00
|
|
|
use App\Utilities\Info;
|
2019-12-19 18:02:51 +03:00
|
|
|
use Exception;
|
2017-09-14 22:21:00 +03:00
|
|
|
use GuzzleHttp\Client;
|
2019-12-19 18:02:51 +03:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2018-05-25 06:11:53 +03:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
trait SiteApi
|
|
|
|
{
|
2019-12-04 18:10:38 +03:00
|
|
|
public static $base_uri = 'https://api.akaunting.com/';
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-12-04 18:10:38 +03:00
|
|
|
protected static function siteApiRequest($method, $path, $extra_data = [])
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-12-04 18:10:38 +03:00
|
|
|
$client = new Client(['verify' => false, 'base_uri' => static::$base_uri]);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$headers['headers'] = [
|
|
|
|
'Authorization' => 'Bearer ' . setting('apps.api_key'),
|
2017-09-14 22:21:00 +03:00
|
|
|
'Accept' => 'application/json',
|
2021-04-16 00:59:43 +03:00
|
|
|
'Referer' => app()->runningInConsole() ? config('app.url') : url('/'),
|
2018-12-19 15:36:40 +03:00
|
|
|
'Akaunting' => version('short'),
|
2019-12-04 18:10:38 +03:00
|
|
|
'Language' => language()->getShortCode(),
|
2020-05-19 15:48:24 +03:00
|
|
|
'Information' => json_encode(Info::all()),
|
2019-11-16 10:21:14 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-12-04 18:10:38 +03:00
|
|
|
$data = array_merge([
|
|
|
|
'timeout' => 30,
|
|
|
|
'referer' => true,
|
|
|
|
'http_errors' => false,
|
|
|
|
], $extra_data);
|
2017-10-17 16:56:53 +03:00
|
|
|
|
2019-12-04 18:10:38 +03:00
|
|
|
$options = array_merge($data, $headers);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-05-25 06:11:53 +03:00
|
|
|
try {
|
2019-12-04 18:10:38 +03:00
|
|
|
$response = $client->request($method, $path, $options);
|
2019-12-19 18:02:51 +03:00
|
|
|
} catch (ConnectException | Exception | RequestException $e) {
|
2019-12-04 18:10:38 +03:00
|
|
|
$response = $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:02:51 +03:00
|
|
|
public static function getResponse($method, $path, $data = [], $status_code = 200)
|
2019-12-04 18:10:38 +03:00
|
|
|
{
|
|
|
|
$response = static::siteApiRequest($method, $path, $data);
|
|
|
|
|
2019-12-19 18:02:51 +03:00
|
|
|
$is_exception = (($response instanceof ConnectException) || ($response instanceof Exception) || ($response instanceof RequestException));
|
|
|
|
|
|
|
|
if (!$response || $is_exception || ($response->getStatusCode() != $status_code)) {
|
2019-12-04 18:10:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-04-25 09:46:22 +03:00
|
|
|
public static function getResponseBody($method, $path, $data = [], $status_code = 200)
|
2019-12-04 18:10:38 +03:00
|
|
|
{
|
2023-04-25 09:46:22 +03:00
|
|
|
if (! $response = static::getResponse($method, $path, $data, $status_code)) {
|
2019-12-04 18:10:38 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$body = json_decode($response->getBody());
|
|
|
|
|
2023-04-25 09:46:22 +03:00
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getResponseData($method, $path, $data = [], $status_code = 200)
|
|
|
|
{
|
|
|
|
if (! $body = static::getResponseBody($method, $path, $data, $status_code)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! is_object($body)) {
|
2019-12-04 18:10:38 +03:00
|
|
|
return [];
|
2018-05-25 06:11:53 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-12-04 18:10:38 +03:00
|
|
|
return $body->data;
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2018-05-25 16:57:58 +03:00
|
|
|
}
|