Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, message, errors=None):
super(TokenException, self).__init__(message)
self.errors = errors
def get_new_token(self):
url = self.API_PREFIX + '/oauth/token'
payload = {'username': self.username,
'password': self.password,
'grant_type': 'password', }
token_response = requests.post(url, auth=('particle', 'particle'), data=payload)
if token_response.ok:
data = token_response.json()
self.token_list = self.get_token_list()
return data
else:
raise TokenException(token_response.reason)
def get_session(self):
if not self.session:
self.session = requests.session()
if self.current_token_is_valid():
self.session.headers.update({
'Authorization': "Bearer %s" % self.token,
})
else:
raise TokenException("Token has expired, please re instantiate the class")
return self.session
def get_device_list(self):
url = self.get_api_prefix() + 'devices'
session = self.get_session()
# session = requests.session()
device_list = session.get(url)
if device_list.ok and device_list.status_code == 200:
return device_list.json()
else:
TokenException(device_list.reason, device_list.status_code)