|
|
@@ -4,11 +4,12 @@ namespace Opentalent\OtTemplating\Domain\Repository;
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
+use Opentalent\OtTemplating\Exception\ApiRequestException;
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
use TYPO3\CMS\Extbase\Persistence\Repository;
|
|
|
|
|
|
/**
|
|
|
- * Base class for repositories based on the opentalent API
|
|
|
+ * Base class for repositories based on the Opentalent API
|
|
|
*
|
|
|
*/
|
|
|
abstract class BaseApiRepository extends Repository
|
|
|
@@ -16,7 +17,7 @@ abstract class BaseApiRepository extends Repository
|
|
|
const BASE_URI = 'https://api.opentalent.fr/api/';
|
|
|
const HYDRA_TYPE = '';
|
|
|
const HTTP_METHOD = 'GET';
|
|
|
- const ITEMS_PER_PAGE = 1001;
|
|
|
+ const ITEMS_PER_PAGE = 999;
|
|
|
|
|
|
private $client;
|
|
|
|
|
|
@@ -32,7 +33,7 @@ abstract class BaseApiRepository extends Repository
|
|
|
* @param string $uri
|
|
|
* @param array $params
|
|
|
* @return ResponseInterface
|
|
|
- * @throws GuzzleException
|
|
|
+ * @throws ApiRequestException
|
|
|
*/
|
|
|
protected function get($uri, $params = [])
|
|
|
{
|
|
|
@@ -40,27 +41,39 @@ abstract class BaseApiRepository extends Repository
|
|
|
if (!empty($params)) {
|
|
|
$uri = $uri . '&' . http_build_query($params);
|
|
|
}
|
|
|
- return $this->client->request(
|
|
|
- static::HTTP_METHOD,
|
|
|
- $uri);
|
|
|
+ try {
|
|
|
+ return $this->client->request(static::HTTP_METHOD, $uri);
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
+ throw ApiRequestException::from_exception($e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Send a request to the API and returns
|
|
|
+ * the response's body as a string
|
|
|
+ *
|
|
|
* @param string $uri
|
|
|
* @param array $params
|
|
|
* @return string
|
|
|
- * @throws GuzzleException
|
|
|
+ * @throws ApiRequestException
|
|
|
*/
|
|
|
protected function getBody($uri, $params = [])
|
|
|
{
|
|
|
- return (string)$this->get($uri, $params)->getBody();
|
|
|
+ try {
|
|
|
+ return (string)$this->get($uri, $params)->getBody();
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
+ throw ApiRequestException::from_exception($e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Send a request to the API and
|
|
|
+ * returns the Json response as an array
|
|
|
+ *
|
|
|
* @param string $uri
|
|
|
* @param array $params
|
|
|
* @return array
|
|
|
- * @throws GuzzleException
|
|
|
+ * @throws ApiRequestException
|
|
|
*/
|
|
|
protected function getJson($uri, $params = [])
|
|
|
{
|
|
|
@@ -68,32 +81,46 @@ abstract class BaseApiRepository extends Repository
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Send a request to the API and
|
|
|
+ * returns the records as an array (members)
|
|
|
+ *
|
|
|
* @param string $uri
|
|
|
* @param array $params
|
|
|
* @return array
|
|
|
- * @throws GuzzleException
|
|
|
+ * @throws ApiRequestException
|
|
|
*/
|
|
|
protected function getApiRecords($uri, $params = []) {
|
|
|
- $data = $this->getJson($uri, $params);
|
|
|
+ try {
|
|
|
+ $data = $this->getJson($uri, $params);
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
+ throw ApiRequestException::from_exception($e);
|
|
|
+ }
|
|
|
|
|
|
if($data->{'@type'} == 'hydra:PagedCollection') {
|
|
|
$records = $data->{'hydra:member'};
|
|
|
} else if ($data->{'@type'} == $this::HYDRA_TYPE) {
|
|
|
$records = [$data];
|
|
|
} else {
|
|
|
- throw new \Exception('Invalid API reponse');
|
|
|
+ throw new ApiRequestException('The API response has an unexpected format');
|
|
|
}
|
|
|
return $records;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Send a request to the API and
|
|
|
+ * returns the first record (member)
|
|
|
+ *
|
|
|
* @param string $uri
|
|
|
* @param array $params
|
|
|
* @return array
|
|
|
- * @throws GuzzleException
|
|
|
+ * @throws ApiRequestException
|
|
|
*/
|
|
|
protected function getApiFirstRecord($uri, $params = []) {
|
|
|
- $records = $this->getApiRecords($uri, $params);
|
|
|
+ try {
|
|
|
+ $records = $this->getApiRecords($uri, $params);
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
+ throw ApiRequestException::from_exception($e);
|
|
|
+ }
|
|
|
return $records[0];
|
|
|
}
|
|
|
}
|