Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/is expired#58 #59

Merged
merged 8 commits into from
Oct 18, 2017
Merged

Feature/is expired#58 #59

merged 8 commits into from
Oct 18, 2017

Conversation

OGKevin
Copy link
Contributor

@OGKevin OGKevin commented Oct 12, 2017

Context

#58

What has been done

This pr introduces a new way to handle SessionContext. Now it is possible to do the following:

if (!$apiContext->isSessionActive()) {
    $apiContext->resetSession();
    $apiContext->save();
}

This way there is no saving an unchanged context.

Closes #58

@OGKevin OGKevin self-assigned this Oct 12, 2017
@OGKevin OGKevin requested a review from dnl-blkv October 12, 2017 19:48
Copy link
Contributor

@DennisSnijder DennisSnijder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple yet effective PR!
Nice one 😄

*
* @return bool
*/
public function isExpired()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isExpired(): bool <- strict return type 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aahhh good old strict type 😁 thx


/**
* Checks if the session has expired
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps rename this function to something like isSessionExpired which makes this comment unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Fair enough will make this change in all the SDK's then 👍

Copy link
Contributor

@dnl-blkv dnl-blkv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments :)

@@ -273,15 +273,19 @@ private function dropSessionContext()
*/
public function ensureSessionActive()
{
if (is_null($this->sessionContext)) {
return;
if (!is_null($this->sessionContext) && $this->isSessionExpired()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check for is_null($this->sessionContext) can go from here, because you do it in isSessionExpired already :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you follow my suggestion and change isSessionExpired to isSessionActive or hasActiveSession, Please do not forget to add an exclamation mark here (only reset if NOT has an active session :))

Copy link
Contributor Author

@OGKevin OGKevin Oct 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhmm AFAIK if we do this then if session is null it will run in an infinite loop resetting the session. At least that was my expiernce in Java 🤔 will double check to make sure.

Copy link
Contributor Author

@OGKevin OGKevin Oct 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea! The infinite loop:

Maximum function nesting level of '256' reached, aborting!

And it also fails because:

 $timeExpiry = $this->sessionContext->getExpiryTime()->getTimestamp();

This will cause an error as of sessionContext is null! So therefore we need !is_null($this->sessionContext) in ensureSessionActive still 🤔. The null sessionContext will get handled higher up the code before a request is made.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah but then based on your suggestion below this can be fixed 👍

if ($timeExpiry - time() < self::TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS) {
$this->resetSession();
}
return is_null($this->sessionContext) || $timeExpiry - time() < self::TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you do is_null($this->sessionContext) check here, the method can be be opposite to what we've had and be called isSessionActive or hasActiveSession. This also fits well into our ensureSessionActive :)

Also, I'd go for an explanatory variable here. I missed it when initially built ensureSessionActive %).

So something like this:

if (is_null($this->sessionContext)) {
    return false;
} else {
    $timeExpiry = $this->sessionContext->getExpiryTime()->getTimestamp();
    $timeToExpiry = $timeExpiry - time();

    return self::TIME_TO_SESSION_EXPIRY_MINIMUM_SECONDS < $timeToExpiry;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bueno

@dnl-blkv
Copy link
Contributor

For the changes in my review: let's have this one merged, and then distribute the approach over the other SDKs :)

@OGKevin
Copy link
Contributor Author

OGKevin commented Oct 13, 2017

@dnl-blkv As we discussed, this pr is not ready. Fixing the issue in client that is causing the infinite loop due to client also resetting session. This must be prevent for endpoints:session-server, device-server and installation.

@OGKevin
Copy link
Contributor Author

OGKevin commented Oct 14, 2017

@dnl-blkv All yours 👀

Copy link
Contributor

@dnl-blkv dnl-blkv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments %)

use Psr\Http\Message\ResponseInterface;

/**
*/
class ApiClient
{
/**
* These are the endpoints where there is no need to have an active session for the request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please go for Endpoints not requiring active session for the request to succeed (less letters :P)

const INSTALLATION_URL = "installation";

const URLS_TO_NOT_ENSURE_ACTIVE_SESSION = [
self::INSTALLATION_URL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make it an associative array:

const URLS_TO_NOT_ENSURE_ACTIVE_SESSION = [
         self::INSTALLATION_URL => true,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

* 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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use single quotes for the constants!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please post the URIS_NOT_REQUIRING_ACTIVE_SESSION right after the doc header, and the constants right after it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java struggles 😁

{
$this->apiContext->ensureSessionActive();
if (!in_array($uri, self::URLS_TO_NOT_ENSURE_ACTIVE_SESSION, true)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you have an associative array instead of a list, you can use isset(self::URLS_TO_NOT_ENSURE_ACTIVE_SESSION[$uri])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

const DEVICE_SERVER_URL = "device-server";
const INSTALLATION_URL = "installation";

const URLS_TO_NOT_ENSURE_ACTIVE_SESSION = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say URIS_NOT_REQUIRING_ACTIVE_SESSION for the sake of consistency :)

@dnl-blkv dnl-blkv merged commit 816f0fa into develop Oct 18, 2017
@dnl-blkv
Copy link
Contributor

@andrederoos

@dnl-blkv dnl-blkv deleted the feature/isExpired#58 branch October 18, 2017 09:04
@OGKevin OGKevin added this to the 0.12.2 milestone Oct 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants