1 | <?php |
||
12 | class AuthClient extends AbstractClient |
||
13 | { |
||
14 | private $apiKey; |
||
15 | private $apiToken = null; |
||
16 | private $loginEndpoint; |
||
17 | |||
18 | public function __construct(string $apiKey, string $loginEndpoint = null, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
||
25 | |||
26 | /** |
||
27 | * @throws AuthenticationException |
||
28 | * @throws InvalidResponseException |
||
29 | * @throws \Http\Client\Exception |
||
30 | */ |
||
31 | public function auth(bool $force = false): void |
||
32 | { |
||
33 | if (!$force && $this->isAuthenticated()) { |
||
34 | return; |
||
35 | } |
||
36 | |||
37 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
||
38 | $body = http_build_query(['api_key' => $this->apiKey]); |
||
39 | |||
40 | $request = $this->createRequest('POST', $this->loginEndpoint, $headers, $body); |
||
41 | $response = $this->sendRequest($request); |
||
42 | $json = $this->extractJson($request, $response); |
||
43 | |||
44 | $this->apiToken = $json['token']; |
||
45 | } |
||
46 | |||
47 | public function isAuthenticated(): bool |
||
51 | |||
52 | public function getApiKey(): string |
||
56 | |||
57 | public function getApiToken(): ?string |
||
61 | } |
||
62 |