mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-17 07:40:09 -07:00
Factor out the refresh function
This commit is contained in:
parent
0648876cc0
commit
1dfbc7db8f
78
wideq.py
78
wideq.py
@ -153,6 +153,47 @@ def login(api_root, access_token):
|
|||||||
return lgedm_post(url, data)
|
return lgedm_post(url, data)
|
||||||
|
|
||||||
|
|
||||||
|
def refresh_auth(oauth_root, refresh_token):
|
||||||
|
"""Get a new access_token using a refresh_token.
|
||||||
|
|
||||||
|
May raise a `TokenError`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
token_url = urljoin(oauth_root, '/oauth2/token')
|
||||||
|
data = {
|
||||||
|
'grant_type': 'refresh_token',
|
||||||
|
'refresh_token': refresh_token,
|
||||||
|
}
|
||||||
|
|
||||||
|
# The timestamp for labeling OAuth requests can be obtained
|
||||||
|
# through a request to the date/time endpoint:
|
||||||
|
# https://us.lgeapi.com/datetime
|
||||||
|
# But we can also just generate a timestamp.
|
||||||
|
timestamp = datetime.datetime.utcnow().strftime(DATE_FORMAT)
|
||||||
|
|
||||||
|
# The signature for the requests is on a string consisting of two
|
||||||
|
# parts: (1) a fake request URL containing the refresh token, and (2)
|
||||||
|
# the timestamp.
|
||||||
|
req_url = ('/oauth2/token?grant_type=refresh_token&refresh_token=' +
|
||||||
|
refresh_token)
|
||||||
|
sig = oauth2_signature('{}\n{}'.format(req_url, timestamp),
|
||||||
|
OAUTH_SECRET_KEY)
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'lgemp-x-app-key': OAUTH_CLIENT_KEY,
|
||||||
|
'lgemp-x-signature': sig,
|
||||||
|
'lgemp-x-date': timestamp,
|
||||||
|
'Accept': 'application/json',
|
||||||
|
}
|
||||||
|
|
||||||
|
res = requests.post(token_url, data=data, headers=headers)
|
||||||
|
res_data = res.json()
|
||||||
|
|
||||||
|
if res_data['status'] != 1:
|
||||||
|
raise TokenError()
|
||||||
|
return res_data['access_token']
|
||||||
|
|
||||||
|
|
||||||
class Gateway(object):
|
class Gateway(object):
|
||||||
def __init__(self, auth_base, api_root, oauth_root):
|
def __init__(self, auth_base, api_root, oauth_root):
|
||||||
self.auth_base = auth_base
|
self.auth_base = auth_base
|
||||||
@ -213,39 +254,12 @@ class Auth(object):
|
|||||||
return cls(gateway, data['access_token'], data['refresh_token'])
|
return cls(gateway, data['access_token'], data['refresh_token'])
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
token_url = urljoin(self.gateway.oauth_root, '/oauth2/token')
|
"""Refresh the authentication, returning a new Auth object.
|
||||||
data = {
|
"""
|
||||||
'grant_type': 'refresh_token',
|
|
||||||
'refresh_token': self.refresh_token,
|
|
||||||
}
|
|
||||||
|
|
||||||
# The timestamp for labeling OAuth requests can be obtained
|
new_access_token = refresh_auth(self.gateway.oauth_root,
|
||||||
# through a request to the date/time endpoint:
|
self.refresh_token)
|
||||||
# https://us.lgeapi.com/datetime
|
return Auth(self.gateway, new_access_token, self.refresh_token)
|
||||||
# But we can also just generate a timestamp.
|
|
||||||
timestamp = datetime.datetime.utcnow().strftime(DATE_FORMAT)
|
|
||||||
|
|
||||||
# The signature for the requests is on a string consisting of two
|
|
||||||
# parts: (1) a fake request URL containing the refresh token, and (2)
|
|
||||||
# the timestamp.
|
|
||||||
req_url = ('/oauth2/token?grant_type=refresh_token&refresh_token=' +
|
|
||||||
self.refresh_token)
|
|
||||||
sig = oauth2_signature('{}\n{}'.format(req_url, timestamp),
|
|
||||||
OAUTH_SECRET_KEY)
|
|
||||||
|
|
||||||
headers = {
|
|
||||||
'lgemp-x-app-key': OAUTH_CLIENT_KEY,
|
|
||||||
'lgemp-x-signature': sig,
|
|
||||||
'lgemp-x-date': timestamp,
|
|
||||||
'Accept': 'application/json',
|
|
||||||
}
|
|
||||||
|
|
||||||
res = requests.post(token_url, data=data, headers=headers)
|
|
||||||
res_data = res.json()
|
|
||||||
|
|
||||||
if res_data['status'] != 1:
|
|
||||||
raise TokenError()
|
|
||||||
return res_data['access_token']
|
|
||||||
|
|
||||||
|
|
||||||
class Session(object):
|
class Session(object):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user