klicktipp-php-client/src/Connection.php

183 lines
4.9 KiB
PHP
Raw Normal View History

2024-12-20 23:45:55 +01:00
<?php
/**
2024-12-29 00:42:21 +01:00
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
2024-12-20 23:45:55 +01:00
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\KlicktippPhpClient;
2025-01-07 14:01:44 +01:00
use Assert\Assert;
2024-12-20 23:45:55 +01:00
use Composer\InstalledVersions;
2025-01-08 15:29:20 +01:00
use D3\KlicktippPhpClient\Exceptions\CommunicationException;
2025-01-07 14:01:44 +01:00
use D3\KlicktippPhpClient\Exceptions\NoCredentialsException;
2025-01-08 15:29:20 +01:00
use D3\KlicktippPhpClient\Exceptions\ResponseContentException;
2024-12-20 23:45:55 +01:00
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Cookie\CookieJar;
2024-12-20 23:45:55 +01:00
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
2024-12-20 23:45:55 +01:00
use Psr\Http\Message\ResponseInterface;
class Connection
{
public const URL = 'https://api.klicktipp.com/';
public const USERAGENT = 'Klicktipp-php-client';
protected string $client_key;
protected string $secret_key;
protected CookieJar $cookies_jar;
2024-12-20 23:45:55 +01:00
/**
* Contains the HTTP client (e.g. Guzzle)
*/
private ClientInterface $client;
public function __construct(string $client_key, string $secret_key)
{
2025-01-07 14:01:44 +01:00
Assert::lazy()
->setExceptionClass(NoCredentialsException::class)
->that($client_key, 'client_key')
->notBlank()
->that($secret_key, 'secret_key')
->notBlank()
->verifyNow();
2024-12-20 23:45:55 +01:00
$this->client_key = $client_key;
$this->secret_key = $secret_key;
$this->cookies_jar = new CookieJar();
2024-12-20 23:45:55 +01:00
}
public function getClientKey(): string
{
return $this->client_key;
}
public function getSecretKey(): string
{
return $this->secret_key;
}
/**
* @param ClientInterface $client
*/
public function setClient(ClientInterface $client): void
{
$this->client = $client;
}
/**
* @return ClientInterface
*/
public function getClient(): ClientInterface
{
2025-01-03 00:05:00 +01:00
$this->client ??=
2024-12-20 23:45:55 +01:00
new Client([
'base_uri' => self::URL,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
2024-12-29 00:42:21 +01:00
'User-Agent' => self::USERAGENT.'/'.InstalledVersions::getVersion('d3/klicktipp-php-client'),
],
2024-12-20 23:45:55 +01:00
]);
return $this->client;
}
/**
* @param string $method
* @param string $uri
* @param array $options
2025-01-08 15:29:20 +01:00
*
2024-12-20 23:45:55 +01:00
* @return ResponseInterface
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-20 23:45:55 +01:00
*/
public function request(string $method, string $uri, array $options = []): ResponseInterface
{
try {
2024-12-29 00:42:21 +01:00
$options['query'] ??= [];
$options[RequestOptions::COOKIES] = $this->getCookiesJar();
2024-12-20 23:45:55 +01:00
if (! empty($options['body'])) {
$options['body'] = json_encode($options['body']);
}
return $this->getClient()->request($method, $uri, $options);
} catch (RequestException $e) {
if ($e->hasResponse()) {
$this->parseResponse($e->getResponse());
}
2025-01-08 15:29:20 +01:00
throw new CommunicationException(
2024-12-20 23:45:55 +01:00
$e->getResponse()->getBody(),
$e->getResponse()->getStatusCode(),
$e
);
2024-12-29 00:42:21 +01:00
} catch (GuzzleException $e) {
2025-01-08 15:29:20 +01:00
throw new CommunicationException($e->getMessage(), $e->getCode(), $e);
2024-12-20 23:45:55 +01:00
}
}
/**
* @param string $method
* @param string $uri
2025-01-08 15:29:20 +01:00
* @param array $options
*
2024-12-20 23:45:55 +01:00
* @return array
2025-01-08 15:29:20 +01:00
* @throws ResponseContentException
* @throws CommunicationException
2024-12-20 23:45:55 +01:00
*/
public function requestAndParse(string $method, string $uri, array $options = []): array
{
return $this->parseResponse($this->request($method, $uri, $options));
}
/**
* @param ResponseInterface $response
2025-01-08 15:29:20 +01:00
*
2024-12-20 23:45:55 +01:00
* @return array Parsed JSON result
2025-01-08 15:29:20 +01:00
* @throws ResponseContentException
2024-12-20 23:45:55 +01:00
*/
public function parseResponse(ResponseInterface $response): array
{
2025-01-08 15:29:20 +01:00
// Rewind the response (middlewares might have read it already)
$response->getBody()->rewind();
2024-12-20 23:45:55 +01:00
2025-01-08 15:29:20 +01:00
$response_body = $response->getBody()->getContents();
2025-01-03 00:05:00 +01:00
2025-01-08 15:29:20 +01:00
$result_array = json_decode($response_body, true);
2024-12-20 23:45:55 +01:00
2025-01-08 15:29:20 +01:00
if ($response->getStatusCode() === 204) {
return [];
}
2024-12-20 23:45:55 +01:00
2025-01-08 15:29:20 +01:00
Assert::lazy()
->setExceptionClass(ResponseContentException::class)
->that($result_array)
->isArray(sprintf('%s: %s', $response->getStatusCode(), $response_body))
->verifyNow();
2024-12-20 23:45:55 +01:00
2025-01-08 15:29:20 +01:00
return $result_array;
2024-12-20 23:45:55 +01:00
}
public function getCookiesJar(): CookieJar
2024-12-20 23:45:55 +01:00
{
return $this->cookies_jar;
2024-12-20 23:45:55 +01:00
}
}