Skip to content

Commit

Permalink
- Request custom token name
Browse files Browse the repository at this point in the history
- Set log verbosity level
  • Loading branch information
SchrodingersGat committed Oct 5, 2023
1 parent 506f235 commit ddf478f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions inventree/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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()

Expand Down Expand Up @@ -167,7 +172,6 @@ def testAuth(self):

try:
response = self.get('/user/me/')
response.raise_for_status()
except Exception as err:
return False

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit ddf478f

Please sign in to comment.