From ddf478f9b4857654bf19b8b57ba47f79ac43253b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 5 Oct 2023 23:46:11 +1100 Subject: [PATCH] - Request custom token name - Set log verbosity level --- inventree/api.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/inventree/api.py b/inventree/api.py index 3aff521..6e72d04 100644 --- a/inventree/api.py +++ b/inventree/api.py @@ -63,6 +63,7 @@ def __init__(self, host=None, **kwargs): self.username = kwargs.get('username', os.environ.get('INVENTREE_API_USERNAME', None)) self.password = kwargs.get('password', os.environ.get('INVENTREE_API_PASSWORD', None)) self.token = kwargs.get('token', os.environ.get('INVENTREE_API_TOKEN', None)) + self.token_name = kwargs.get('token_name', 'inventree-python') self.timeout = kwargs.get('timeout', os.environ.get('INVENTREE_API_TIMEOUT', 10)) self.proxies = kwargs.get('proxies', dict()) @@ -72,6 +73,10 @@ def __init__(self, host=None, **kwargs): self.auth = None self.connected = False + self.verbose = kwargs.get('verbose', False) + + logger.setLevel(logging.DEBUG if self.verbose else logging.INFO) + if kwargs.get('connect', True): self.connect() @@ -167,7 +172,6 @@ def testAuth(self): try: response = self.get('/user/me/') - response.raise_for_status() except Exception as err: return False @@ -236,7 +240,7 @@ def requestToken(self): if not self.username or not self.password: raise AttributeError('Supply username and password to request token') - + logger.info("Requesting auth token from server...") if not self.connected: @@ -245,11 +249,14 @@ def requestToken(self): # Request an auth token from the server try: - response = self.get('/user/token/') + url = '/user/token/' + if self.token_name: + url += f'?name={self.token_name}' + response = self.get(url) except Exception as e: logger.error(f"Error requesting token: {str(type(e))}") return None - + if 'token' not in response: logger.error(f"Token not returned by server: {response}") return None