diff --git a/src/Picqer/Financials/Exact/Connection.php b/src/Picqer/Financials/Exact/Connection.php index fb814b81..a61fc2e0 100644 --- a/src/Picqer/Financials/Exact/Connection.php +++ b/src/Picqer/Financials/Exact/Connection.php @@ -195,16 +195,26 @@ public function connect() $this->redirectForAuthorization(); } - // If access token is not set or token has expired, acquire new token - if (empty($this->accessToken) || $this->tokenHasExpired()) { - $this->acquireAccessToken(); - } + $this->checkOrAcquireAccessToken(); $client = $this->client(); return $client; } + /** + * Checks whether the access token is still valid. + * + * @throws \Picqer\Financials\Exact\ApiException + */ + public function checkOrAcquireAccessToken() + { + // If access token is not set or token has expired, acquire new token + if (empty($this->accessToken) || $this->tokenHasExpired()) { + $this->acquireAccessToken(); + } + } + /** * @param string $method * @param string $endpoint @@ -259,6 +269,7 @@ public function get($url, array $params = [], array $headers = []) try { $request = $this->createRequest('GET', $url, null, $params, $headers); + $this->checkOrAcquireAccessToken(); $response = $this->client()->send($request); return $this->parseResponse($response, $url != $this->nextUrl); @@ -281,6 +292,7 @@ public function post($url, $body) try { $request = $this->createRequest('POST', $url, $body); + $this->checkOrAcquireAccessToken(); $response = $this->client()->send($request); return $this->parseResponse($response); @@ -303,6 +315,7 @@ public function put($url, $body) try { $request = $this->createRequest('PUT', $url, $body); + $this->checkOrAcquireAccessToken(); $response = $this->client()->send($request); return $this->parseResponse($response); @@ -324,6 +337,7 @@ public function delete($url) try { $request = $this->createRequest('DELETE', $url); + $this->checkOrAcquireAccessToken(); $response = $this->client()->send($request); return $this->parseResponse($response);