|
|
@@ -8,6 +8,8 @@ use Opentalent\OtCore\Exception\ApiRequestException;
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
use Psr\Log\LoggerAwareTrait;
|
|
|
+use Symfony\Component\Yaml\Exception\ParseException;
|
|
|
+use Symfony\Component\Yaml\Yaml;
|
|
|
use TYPO3\CMS\Core\Core\ApplicationContext;
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
|
|
|
|
|
|
@@ -19,13 +21,16 @@ abstract class BaseApiRepository implements LoggerAwareInterface
|
|
|
{
|
|
|
use LoggerAwareTrait;
|
|
|
|
|
|
- const BASE_URI = 'https://api.opentalent.fr/api/';
|
|
|
+ const DEFAULT_BASE_URI = 'https://api.opentalent.fr/api/';
|
|
|
+ const URI_TRAILING_PART = '';
|
|
|
const HYDRA_TYPE = '';
|
|
|
const HTTP_METHOD = 'GET';
|
|
|
const DEFAULT_ITEMS_PER_PAGE = 8;
|
|
|
|
|
|
- protected $client;
|
|
|
- protected $context;
|
|
|
+ protected string $base_uri = self::DEFAULT_BASE_URI;
|
|
|
+ protected array $variants_uris = [];
|
|
|
+ protected Client $client;
|
|
|
+ protected ApplicationContext $context;
|
|
|
|
|
|
/**
|
|
|
* BaseApiRepository constructor.
|
|
|
@@ -38,16 +43,49 @@ abstract class BaseApiRepository implements LoggerAwareInterface
|
|
|
?Client $client = null,
|
|
|
?ApplicationContext $context = null
|
|
|
) {
|
|
|
+ if ($context === null) {
|
|
|
+ $this->context = \TYPO3\CMS\Core\Core\Environment::getContext();
|
|
|
+ } else {
|
|
|
+ $this->context = $context;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->loadConf();
|
|
|
+
|
|
|
if ($client === null) {
|
|
|
- $this->client = new Client(['base_uri' => static::BASE_URI]);
|
|
|
+ $this->client = new Client(['base_uri' => $this->getApiUri()]);
|
|
|
} else {
|
|
|
$this->client = $client;
|
|
|
}
|
|
|
- if ($context === null) {
|
|
|
- $this->context = \TYPO3\CMS\Core\Core\Environment::getContext();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function loadConf() {
|
|
|
+ $conf_path = $_ENV['TYPO3_PATH_ROOT'] . '/typo3conf/ext/ot_core/Configuration/ot_config.yaml';
|
|
|
+ $conf = Yaml::parseFile($conf_path);
|
|
|
+
|
|
|
+ // api_variant_uri: Should we set an alternative uri for the API? (dev and testing only)
|
|
|
+ if ($this->context->isDevelopment() || $this->context->isTesting()) {
|
|
|
+ $this->variants_uris = $conf['api_variant_uri'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Return the API URI for the current repository
|
|
|
+ *
|
|
|
+ * @param string $trailing_part
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ protected function getApiUri(string $trailing_part = null): string
|
|
|
+ {
|
|
|
+ $host = $_SERVER['HTTP_HOST'];
|
|
|
+ if (isset($this->variants_uris[$host])) {
|
|
|
+ $uri = $this->variants_uris[$host];
|
|
|
} else {
|
|
|
- $this->context = $context;
|
|
|
+ $uri = self::DEFAULT_BASE_URI;
|
|
|
}
|
|
|
+
|
|
|
+ $trailing_part = $trailing_part ?? $this::URI_TRAILING_PART;
|
|
|
+ $uri = trim($uri, '/') . '/' . trim($trailing_part, '/');
|
|
|
+ return $uri;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -62,13 +100,13 @@ abstract class BaseApiRepository implements LoggerAwareInterface
|
|
|
* Send a request to the API and
|
|
|
* returns the records as an array (members)
|
|
|
*
|
|
|
- * @param string $uri
|
|
|
* @param array $params
|
|
|
* @return ApiPagedCollection
|
|
|
* @throws ApiRequestException
|
|
|
*/
|
|
|
- protected function getApiRecords(string $uri, $params = []) {
|
|
|
- $body = $this->getJsonDecoded($uri, $params);
|
|
|
+ protected function getApiRecords($params = []): ApiPagedCollection
|
|
|
+ {
|
|
|
+ $body = $this->getJsonDecoded($this->getApiUri(), $params);
|
|
|
|
|
|
$page = (int)($params['page'] ?? 1);
|
|
|
|
|
|
@@ -104,16 +142,15 @@ abstract class BaseApiRepository implements LoggerAwareInterface
|
|
|
* Send a request to the API and
|
|
|
* returns the first record (member)
|
|
|
*
|
|
|
- * @param string $uri
|
|
|
* @param array $params
|
|
|
* @return object
|
|
|
* @throws ApiRequestException
|
|
|
*/
|
|
|
- protected function getApiFirstRecord(string $uri, $params = []): object
|
|
|
+ protected function getApiFirstRecord($params = []): object
|
|
|
{
|
|
|
$params['page'] = '1';
|
|
|
$params['totalItems'] = '1';
|
|
|
- $collection = $this->getApiRecords($uri, $params);
|
|
|
+ $collection = $this->getApiRecords($params);
|
|
|
return $collection->getMembers()[0];
|
|
|
}
|
|
|
|
|
|
@@ -164,7 +201,7 @@ abstract class BaseApiRepository implements LoggerAwareInterface
|
|
|
$uri = $uri . '&' . http_build_query($params);
|
|
|
}
|
|
|
try {
|
|
|
- if (!$this->context->isProduction()) {
|
|
|
+ if ($this->context->isDevelopment() || $this->context->isTesting()) {
|
|
|
$this->logger->info('API Call: ' . $uri);
|
|
|
}
|
|
|
return $this->client->request(static::HTTP_METHOD, $uri);
|