From c1b6678dff9b01695ab87e423387fad7e6b84788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 6 Nov 2021 18:01:07 +0200 Subject: [PATCH 1/3] Add _bootstrap as class method --- trakt/core.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/trakt/core.py b/trakt/core.py index 04f9f4d..8e9ea6c 100644 --- a/trakt/core.py +++ b/trakt/core.py @@ -479,6 +479,29 @@ def __init__(self): # Map HTTP response codes to exception types self.error_map = {err.http_code: err for err in errs} + self._bootstrapped = False + + def _bootstrap(self): + """Bootstrap your authentication environment when authentication is + needed and if a file at `CONFIG_PATH` exists. + The process is completed by setting the client id header. + """ + + if self._bootstrapped: + return + self._bootstrapped = True + + global OAUTH_TOKEN_VALID, OAUTH_EXPIRES_AT + global OAUTH_REFRESH, OAUTH_TOKEN + + load_config() + # Check token validity and refresh token if needed + if (not OAUTH_TOKEN_VALID and OAUTH_EXPIRES_AT is not None + and OAUTH_REFRESH is not None): + _validate_token(self) + # For backwards compatibility with trakt<=2.3.0 + if api_key is not None and OAUTH_TOKEN is None: + OAUTH_TOKEN = api_key @staticmethod def _get_first(f, *args, **kwargs): From e820d0bcf94fff857109fc6547cd26cdbbfae987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 1 Nov 2021 22:30:12 +0200 Subject: [PATCH 2/3] Move @_bootstrapped to be executed in requests body --- trakt/core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/trakt/core.py b/trakt/core.py index 8e9ea6c..2e4c4e2 100644 --- a/trakt/core.py +++ b/trakt/core.py @@ -555,7 +555,6 @@ def _handle_request(self, method, url, data=None): json_data = json.loads(response.content.decode('UTF-8', 'ignore')) return json_data - @_bootstrapped def get(self, f): """Perform a HTTP GET request using the provided uri yielded from the *f* co-routine. The processed JSON results are then sent back to the @@ -567,6 +566,7 @@ def get(self, f): """ @wraps(f) def inner(*args, **kwargs): + self._bootstrap() resp = self._get_first(f, *args, **kwargs) if not isinstance(resp, tuple): # Handle cached property responses @@ -579,7 +579,6 @@ def inner(*args, **kwargs): return None return inner - @_bootstrapped def delete(self, f): """Perform an HTTP DELETE request using the provided uri @@ -587,13 +586,13 @@ def delete(self, f): """ @wraps(f) def inner(*args, **kwargs): + self._bootstrap() generator = f(*args, **kwargs) uri = next(generator) url = BASE_URL + uri self._handle_request('delete', url) return inner - @_bootstrapped def post(self, f): """Perform an HTTP POST request using the provided uri and optional args yielded from the *f* co-routine. The processed JSON results are @@ -606,6 +605,7 @@ def post(self, f): """ @wraps(f) def inner(*args, **kwargs): + self._bootstrap() url, generator, args = self._get_first(f, *args, **kwargs) json_data = self._handle_request('post', url, data=args) try: @@ -614,7 +614,6 @@ def inner(*args, **kwargs): return None return inner - @_bootstrapped def put(self, f): """Perform an HTTP PUT request using the provided uri and optional args yielded from the *f* co-routine. The processed JSON results are then @@ -627,6 +626,7 @@ def put(self, f): """ @wraps(f) def inner(*args, **kwargs): + self._bootstrap() url, generator, args = self._get_first(f, *args, **kwargs) json_data = self._handle_request('put', url, data=args) try: From 99d96445623f0fa58998bedc0e38b904ecbbf6b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 6 Nov 2021 18:03:52 +0200 Subject: [PATCH 3/3] Drop unused @_bootstrapped decorator --- trakt/core.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/trakt/core.py b/trakt/core.py index 2e4c4e2..dd3f893 100644 --- a/trakt/core.py +++ b/trakt/core.py @@ -441,29 +441,6 @@ def load_config(): APPLICATION_ID = config_data.get('APPLICATION_ID', None) -def _bootstrapped(f): - """Bootstrap your authentication environment when authentication is needed - and if a file at `CONFIG_PATH` exists. The process is completed by setting - the client id header. - """ - @wraps(f) - def inner(*args, **kwargs): - global OAUTH_TOKEN_VALID, OAUTH_EXPIRES_AT - global OAUTH_REFRESH, OAUTH_TOKEN - - load_config() - # Check token validity and refresh token if needed - if (not OAUTH_TOKEN_VALID and OAUTH_EXPIRES_AT is not None - and OAUTH_REFRESH is not None): - _validate_token(args[0]) - # For backwards compatability with trakt<=2.3.0 - if api_key is not None and OAUTH_TOKEN is None: - OAUTH_TOKEN = api_key - - return f(*args, **kwargs) - return inner - - class Core(object): """This class contains all of the functionality required for interfacing with the Trakt.tv API