File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Teamleader/TeamleaderApi.php
<?php
namespace App\Komma\Teamleader;
use App\Komma\Teamleader\Base\ApiException;
use App\Komma\Teamleader\Endpoints\AuthenticationEndpoint;
use App\Komma\Teamleader\Endpoints\CompanyEndpoint;
use App\Komma\Teamleader\Endpoints\ContactEndpoint;
use App\Komma\Teamleader\Endpoints\DealEndpoint;
use App\Komma\Teamleader\Endpoints\DepartmentEndpoint;
use App\Komma\Teamleader\Endpoints\InvoiceEndpoint;
use App\Komma\Teamleader\Endpoints\ProductEndpoint;
use App\Komma\Teamleader\Endpoints\QuotationEndpoint;
use App\Komma\Teamleader\Endpoints\TaxRateEndpoint;
use App\Komma\Teamleader\Endpoints\UserEndpoint;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;
class TeamleaderApi
{
protected ClientInterface $client;
const API_PATH = 'https://api.focus.teamleader.eu';
/**
* HTTP Methods
*/
const HTTP_GET = 'GET';
const HTTP_POST = 'POST';
const HTTP_PUT = 'PUT';
const HTTP_DELETE = 'DELETE';
const HTTP_PATCH = 'PATCH';
public AuthenticationEndpoint $authentication;
public ProductEndpoint $product;
public DepartmentEndpoint $department;
public UserEndpoint $user;
public TaxRateEndpoint $taxRate;
public ContactEndpoint $contact;
public CompanyEndpoint $company;
public DealEndpoint $deal;
public QuotationEndpoint $quotation;
// public InvoiceEndpoint $invoice;
public function __construct(array $clientSettings = [])
{
$this->client = new Client(array_merge([
'base_uri' => self::API_PATH,
'timeout' => 10.0,
], $clientSettings));
$this->initializeEndpoints();
}
private function initializeEndpoints()
{
$this->authentication = new AuthenticationEndpoint($this);
$this->product = new ProductEndpoint($this);
$this->department = new DepartmentEndpoint($this);
$this->taxRate = new TaxRateEndpoint($this);
$this->contact = new ContactEndpoint($this);
$this->company = new CompanyEndpoint($this);
$this->user = new UserEndpoint($this);
$this->deal = new DealEndpoint($this);
$this->quotation = new QuotationEndpoint($this);
// $this->invoice = new InvoiceEndpoint($this); // API access disabled, not needed ATM
}
/**
* Send the request to the client.
* Return the body and also to the guzzle response.
*
* @param Request $request
* @param bool $parseResponse
* @return array
* @throws ApiException
*/
public function send(Request $request, bool $parseResponse = true): array
{
try {
$guzzleResponse = $this->client->send($request, ['http_errors' => false]);
} catch (GuzzleException $e) {
if (is_a($e, ConnectException::class)) {
throw new \RuntimeException('Connection timeout. Try again');
}
throw new \RuntimeException(self::class.': '.$e);
}
if (! $guzzleResponse) {
throw new \RuntimeException(self::class.': Did not receive API response.');
}
if ($parseResponse) {
$parsedResponse = $this->parseResponseBody($guzzleResponse);
}
if (! isset($parsedResponse)) {
return [null, $guzzleResponse];
}
return [$parsedResponse, $guzzleResponse];
}
/**
* Send the request to the client.
* And just return the body.
*
* @param Request $request
* @return mixed
* @throws ApiException
*/
public function sendSimple(Request $request)
{
[$responseBody, $guzzleResponse] = $this->send($request);
return $responseBody;
}
/**
* Parse the PSR-7 Response body
*
* @param ResponseInterface $response
* @return object
* @throws ApiException
*/
private function parseResponseBody(ResponseInterface $response): ?object
{
// If 204 we can return empty object
if ($response->getStatusCode() === 204) {
return null;
}
$body = (string) $response->getBody();
if (empty($body)) {
throw new \RuntimeException('No response body found.');
}
$object = (object) @json_decode($body, false, 512, JSON_OBJECT_AS_ARRAY);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ApiException($response);
}
if ($response->getStatusCode() >= 400) {
throw new ApiException($response, $object);
}
return $object;
}
}