Merge tag '2.0.11'

tag 2.0.11
This commit is contained in:
Jérémy Munsch 2021-08-17 11:14:43 +02:00
commit 078201fe04
4 changed files with 36 additions and 10 deletions

View file

@ -1,5 +1,5 @@
{
"version": "2.0.10",
"version": "2.0.11",
"tokenTimeout": 60,
"apiRequestRatePerMinute": 256
}

View file

@ -3,7 +3,7 @@
"type": "library",
"description": "LDC CRM Client V2",
"license": "MIT",
"version": "2.0.10",
"version": "2.0.11",
"authors": [
{
"name": "Jérémy Munsch",

View file

@ -47,6 +47,9 @@ use Kwaadpepper\Enum\BaseEnum;
* a replay attack scenario. Timestamps are provided in headers
* and in the http body
*
* @method static self missingParameter()
* The request is missing some parameters
*
* @method static self throttleRequestsException()
* Too many requests have been attempted. The is a throttle security.
* Normally you should not trigger this.
@ -78,6 +81,7 @@ class ApiError extends BaseEnum
'singleOptinCustomerExists' => 120,
'methodNotAllowed' => 150,
'wrongTimeStamp' => 151,
'missingParameter' => 400,
'authorizationException' => 403,
'throttleRequestsException' => 429
];
@ -96,6 +100,7 @@ class ApiError extends BaseEnum
'needAuth' => 'Vous devez être authentifié',
'methodNotAllowed' => 'This method is not allowed for this request',
'wrongTimeStamp' => 'Your request is malformed, please check timestamp header and parameter',
'missingParameter' => 'The request is missing some parameters',
'authorizationException' => 'You are not autorized to access this',
'throttleRequestsException' => 'Throttle: too many request have been attempted, please retry later'
];

View file

@ -58,6 +58,8 @@ class ApiInterface
*/
protected static $apiToken;
protected static $authentification = null;
public function __construct(string $endpoint = null, string $bearerToken = null, string $bearerPwd = null)
{
static::$endpoint = static::$endpoint ?: $endpoint;
@ -76,23 +78,33 @@ class ApiInterface
if (!static::$endpoint) {
throw new CrmException('Either config or .env file need to have the endpoint var set');
}
// handle http Auth
if (\strpos(static::$endpoint, '@')) {
$vars = \parse_url(static::$endpoint);
static::$authentification = \base64_encode($vars['user'] . ':' . $vars['pass']);
}
}
/**
* Make a crm http call to get a reponse
*
* @param Request $request
* @param bool $addBearer
* @return \LDC\LDCCRM\Transport\Response
* @throws \LDC\LDCCRM\Exceptions\CrmException if an error occurs on server side or on getting response
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs
*/
public function call(Request $request): Response
public function call(Request $request, bool $addBearer = false): Response
{
$headers = [];
$data = [];
$this->setHeaders($headers);
$this->setData($data);
$data = array_merge($request->data, $data);
$this->addCredentials($request, $addBearer);
$response = $this->processRequest($request, $headers, $data);
try {
if ($response->status->equals(ApiStatus::KO())) {
@ -188,8 +200,7 @@ class ApiInterface
protected function authenticate(): bool
{
$request = Utils::getResquest('apiV2.authenticate');
$this->addBearerCredentials($request);
$response = $this->call($request);
$response = $this->call($request, true);
if ($response->status->equals(ApiStatus::KO())) {
throw new LoginException($response->apiError->label, $response->apiError->value);
}
@ -200,13 +211,23 @@ class ApiInterface
* Inject bearer credentials into the request
*
* @param Request $request
* @param bool $addBearer
* @return void
*/
protected function addBearerCredentials(Request $request)
protected function addCredentials(Request $request, bool $addBearer = false)
{
$request->setHidden([
'headers' => ['Authorization' => sprintf('Bearer %s', static::$bearerToken)],
'data' => ['bearer_pwd' => static::$bearerPwd]
]);
$params = [];
$authentification = [];
if (static::$authentification) {
$authentification[] = sprintf('Basic %s', static::$authentification);
}
if ($addBearer) {
$authentification[] = sprintf('Bearer %s', static::$bearerToken);
$params['data'] = ['bearer_pwd' => static::$bearerPwd];
}
if ($authentification) {
$params['headers'] = ['Authorization' => implode(',', $authentification)];
}
$request->setHidden($params);
}
}