Try Token

This commit is contained in:
Juan Pablo Vial
2024-08-27 14:51:54 -04:00
parent ccd5b296f3
commit 7447579ef0

View File

@ -47,11 +47,28 @@ class Login
}
return file_get_contents($this->tokenFilename);
}
public function tryToken(string $token): bool
{
$url = '/api/tokens/try';
try {
$response = $this->client->request('GET', $url, [
'headers' => ['Authorization' => "Bearer {$token}"]
]);
} catch (ClientExceptionInterface $exception) {
$this->logger->error($exception);
return false;
}
return $response->getStatusCode() === 200;
}
public function getKey(string $apiKey, string $separator = 'g'): string
{
try {
$token = $this->retrieveToken();
} catch (Exception) {
if (!$this->tryToken(implode('', [md5($apiKey), $separator, $token]))) {
throw new Exception('Token not valid');
}
} catch (Exception $exception) {
$this->logger->error($exception);
$token = $this->login();
}
return implode('', [md5($apiKey), $separator, $token]);