Skip to content

Commit

Permalink
Merge branch 'master' into pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolinski committed Mar 24, 2019
2 parents a936cb4 + 2b1c7ea commit ca9a567
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
38 changes: 24 additions & 14 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# flake8: noqa: F403, F405

import types
import time

import pytest
from tests.client import MockRequests
Expand Down Expand Up @@ -30,29 +31,38 @@ def test_executor():
client.count(type="shows")


def test_refresh_token():
http = lambda client: DefaultHttpComponent(
client,
requests_dependency=MockRequests(
{"countries/shows": [COUNTRIES, 200], "oauth/token": [OAUTH_GET_TOKEN, 200]}
),
)
TOKEN_REFRESH_HTTP = lambda client: DefaultHttpComponent(
client,
requests_dependency=MockRequests(
{"countries/shows": [[], 200], "oauth/token": [OAUTH_GET_TOKEN, 200]}
),
)

credentials = TraktCredentials("access", "refresh", "scope", 100)

### refresh off
def test_refresh_token_off():
credentials = TraktCredentials("access", "refresh", "scope", 100)

client = Trakt("", "", http_component=http, user=credentials)
client = Trakt("", "", http_component=TOKEN_REFRESH_HTTP, user=credentials)
client.get_countries(type="shows")

assert client.user.refresh_token == "refresh"
assert client.user.access_token == "access"

### refresh on

client = Trakt(
"", "", http_component=http, user=credentials, auto_refresh_token=True
)
def test_refresh_token_on():
client = Trakt("", "", http_component=TOKEN_REFRESH_HTTP, auto_refresh_token=True)

# token is not going to expire soon (should not refresh)
expire_at = int(time.time()) + 2 * 30 * 24 * 60 * 60 # 60 days
client.set_user(TraktCredentials("access", "refresh", "scope", expire_at))
client.get_countries(type="shows")

assert client.user.refresh_token == "refresh"
assert client.user.access_token == "access"

# token is going to expire soon
expire_at = int(time.time()) + 15 * 24 * 60 * 60 # 15 days
client.set_user(TraktCredentials("access", "refresh", "scope", expire_at))
client.get_countries(type="shows")

assert client.user.refresh_token == OAUTH_GET_TOKEN["refresh_token"]
Expand Down
5 changes: 4 additions & 1 deletion trakt/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def __setitem__(self, name: str, value: Any) -> None:

DEFAULT_CONFIG: InternalConfigType = {
"http": {"base_url": "https://api.trakt.tv", "max_retries": 3},
"oauth": {"default_redirect_uri": "urn:ietf:wg:oauth:2.0:oob"},
"oauth": {
"default_redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"refresh_token_s": 30 * 24 * 60 * 60,
},
}


Expand Down
6 changes: 5 additions & 1 deletion trakt/core/executors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import time
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from trakt.core import json_parser
Expand Down Expand Up @@ -29,7 +30,10 @@ def __init__(
self.path_suites = []

if self.client.config["auto_refresh_token"] and self.client.user:
self.client.oauth.refresh_token()
expires_in = self.client.user.expires_at - int(time.time())

if expires_in < self.client.config["oauth"]["refresh_token_s"]:
self.client.oauth.refresh_token()

def __getattr__(self, param: str) -> Executor:
self.params.append(param)
Expand Down

0 comments on commit ca9a567

Please sign in to comment.