|
|
@@ -6,24 +6,30 @@ use GuzzleHttp\Client;
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
use Opentalent\OtTemplating\Exception\ApiRequestException;
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
+use Psr\Log\LoggerAwareInterface;
|
|
|
+use Psr\Log\LoggerAwareTrait;
|
|
|
use TYPO3\CMS\Extbase\Persistence\Repository;
|
|
|
|
|
|
/**
|
|
|
* Base class for repositories based on the Opentalent API
|
|
|
*
|
|
|
*/
|
|
|
-abstract class BaseApiRepository extends Repository
|
|
|
+abstract class BaseApiRepository extends Repository implements LoggerAwareInterface
|
|
|
{
|
|
|
+ use LoggerAwareTrait;
|
|
|
+
|
|
|
const BASE_URI = 'https://api.opentalent.fr/api/';
|
|
|
const HYDRA_TYPE = '';
|
|
|
const HTTP_METHOD = 'GET';
|
|
|
- const ITEMS_PER_PAGE = 999;
|
|
|
+ const DEFAULT_ITEMS_PER_PAGE = 999;
|
|
|
|
|
|
private $client;
|
|
|
+ private $context;
|
|
|
|
|
|
public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
|
|
|
parent::__construct($objectManager);
|
|
|
$this->client = new Client(['base_uri' => static::BASE_URI]);
|
|
|
+ $this->context = \TYPO3\CMS\Core\Core\Environment::getContext();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -35,13 +41,19 @@ abstract class BaseApiRepository extends Repository
|
|
|
* @return ResponseInterface
|
|
|
* @throws ApiRequestException
|
|
|
*/
|
|
|
- protected function get($uri, $params = [])
|
|
|
+ protected function get(string $uri, $params = [])
|
|
|
{
|
|
|
- $uri = $uri . '?_format=json&itemsPerPage=' . (string)self::ITEMS_PER_PAGE . '';
|
|
|
+ $uri = $uri . '?_format=json';
|
|
|
+ if(!isset($params['itemsPerPage'])) {
|
|
|
+ $params['itemsPerPage'] = (string)self::DEFAULT_ITEMS_PER_PAGE;
|
|
|
+ }
|
|
|
if (!empty($params)) {
|
|
|
$uri = $uri . '&' . http_build_query($params);
|
|
|
}
|
|
|
try {
|
|
|
+ if (!$this->context->isProduction()) {
|
|
|
+ $this->logger->info('API Call: ' . $uri);
|
|
|
+ }
|
|
|
return $this->client->request(static::HTTP_METHOD, $uri);
|
|
|
} catch (GuzzleException $e) {
|
|
|
throw ApiRequestException::from_exception($e);
|
|
|
@@ -57,7 +69,7 @@ abstract class BaseApiRepository extends Repository
|
|
|
* @return string
|
|
|
* @throws ApiRequestException
|
|
|
*/
|
|
|
- protected function getBody($uri, $params = [])
|
|
|
+ protected function getBody(string $uri, $params = [])
|
|
|
{
|
|
|
try {
|
|
|
return (string)$this->get($uri, $params)->getBody();
|
|
|
@@ -75,7 +87,7 @@ abstract class BaseApiRepository extends Repository
|
|
|
* @return array
|
|
|
* @throws ApiRequestException
|
|
|
*/
|
|
|
- protected function getJson($uri, $params = [])
|
|
|
+ protected function getJson(string $uri, $params = [])
|
|
|
{
|
|
|
return json_decode($this->getBody($uri, $params));
|
|
|
}
|
|
|
@@ -89,7 +101,7 @@ abstract class BaseApiRepository extends Repository
|
|
|
* @return array
|
|
|
* @throws ApiRequestException
|
|
|
*/
|
|
|
- protected function getApiRecords($uri, $params = []) {
|
|
|
+ protected function getApiRecords(string $uri, $params = []) {
|
|
|
try {
|
|
|
$data = $this->getJson($uri, $params);
|
|
|
} catch (GuzzleException $e) {
|
|
|
@@ -115,7 +127,7 @@ abstract class BaseApiRepository extends Repository
|
|
|
* @return array
|
|
|
* @throws ApiRequestException
|
|
|
*/
|
|
|
- protected function getApiFirstRecord($uri, $params = []) {
|
|
|
+ protected function getApiFirstRecord(string $uri, $params = []) {
|
|
|
try {
|
|
|
$records = $this->getApiRecords($uri, $params);
|
|
|
} catch (GuzzleException $e) {
|