Skip to content

Commit

Permalink
Fixed the issue where client will cause infinite loop 👏 #58.
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Oct 14, 2017
1 parent 4b30d69 commit 80a3a70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Context/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private function dropSessionContext()
*/
public function ensureSessionActive()
{
if ($this->isSessionActive()) {
if (!$this->isSessionActive()) {
$this->resetSession();
}
}
Expand All @@ -290,7 +290,7 @@ public function isSessionActive(): bool
$timeExpiry = $this->sessionContext->getExpiryTime()->getTimestamp();
$timeToExpiry = $timeExpiry - time();

return $timeToExpiry < self::TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS;
return $timeToExpiry > self::TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS;
}

/**
Expand Down
23 changes: 20 additions & 3 deletions src/Http/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Uri;
use phpDocumentor\Reflection\Types\Self_;
use Psr\Http\Message\ResponseInterface;

/**
*/
class ApiClient
{
/**
* These are the endpoints where there is no need to have an active session for the request
* to succeed.
*/
const SESSION_SERVER_URL = "session-server";
const DEVICE_SERVER_URL = "device-server";
const INSTALLATION_URL = "installation";

const URLS_TO_NOT_ENSURE_ACTIVE_SESSION = [
self::INSTALLATION_URL,
self::DEVICE_SERVER_URL,
self::SESSION_SERVER_URL];

/**
* Error constants.
*/
Expand Down Expand Up @@ -166,7 +180,7 @@ private function request(
array $params,
array $customHeaders
): BunqResponseRaw {
$this->initialize();
$this->initialize($uri);

$response = $this->httpClient->request(
$method,
Expand All @@ -179,9 +193,12 @@ private function request(

/**
*/
private function initialize()
private function initialize(string $uri)
{
$this->apiContext->ensureSessionActive();
if (!in_array($uri, self::URLS_TO_NOT_ENSURE_ACTIVE_SESSION, true)) {
$this->apiContext->ensureSessionActive();
}

$this->initializeHttpClient();
}

Expand Down

0 comments on commit 80a3a70

Please sign in to comment.