1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-20 09:00:09 -07:00

Refactor common request function

This commit is contained in:
Adrian Sampson 2018-01-08 12:00:01 -08:00
parent 7ff25e9eea
commit 7aacb19082

View File

@ -16,20 +16,42 @@ LOGIN_PATH = 'member/login'
DEVICE_LIST_PATH = 'device/deviceList' DEVICE_LIST_PATH = 'device/deviceList'
def gateway_info(): def lgedm_post(url, data=None, access_token=None, session_id=None):
"""Load information about the hosts to use for API interaction. """Make an HTTP request in the format used by the API servers.
In this format, the request POST data sent as JSON under a special
key; authentication sent in headers. Return the JSON data extracted
from the response.
The `access_token` and `session_id` are required for most normal,
authenticated requests. They are not required, for example, to load
the gateway server data or to start a session.
""" """
req_data = {DATA_ROOT: {'countryCode': COUNTRY, 'langCode': LANGUAGE}}
headers = { headers = {
'x-thinq-application-key': APP_KEY, 'x-thinq-application-key': APP_KEY,
'x-thinq-security-key': SECURITY_KEY, 'x-thinq-security-key': SECURITY_KEY,
'Accept': 'application/json', 'Accept': 'application/json',
} }
res = requests.post(GATEWAY_URL, json=req_data, headers=headers) if access_token:
headers['x-thinq-token'] = access_token
if session_id:
headers['x-thinq-jsessionId'] = session_id
res = requests.post(url, json={DATA_ROOT: data}, headers=headers)
return res.json()[DATA_ROOT] return res.json()[DATA_ROOT]
def gateway_info():
"""Load information about the hosts to use for API interaction.
"""
return lgedm_post(
GATEWAY_URL,
{'countryCode': COUNTRY, 'langCode': LANGUAGE},
)
def oauth_url(oauth_base): def oauth_url(oauth_base):
"""Construct the URL for users to log in (in a browser) to start an """Construct the URL for users to log in (in a browser) to start an
authenticated session. authenticated session.
@ -63,35 +85,21 @@ def login(api_root, access_token):
""" """
url = urljoin(api_root + '/', LOGIN_PATH) url = urljoin(api_root + '/', LOGIN_PATH)
req_data = {DATA_ROOT: { data = {
'countryCode': COUNTRY, 'countryCode': COUNTRY,
'langCode': LANGUAGE, 'langCode': LANGUAGE,
'loginType': 'EMP', 'loginType': 'EMP',
'token': access_token, 'token': access_token,
}}
headers = {
'x-thinq-application-key': APP_KEY,
'x-thinq-security-key': SECURITY_KEY,
'Accept': 'application/json',
} }
res = requests.post(url, json=req_data, headers=headers) return lgedm_post(url, data)
return res.json()[DATA_ROOT]
def get_devices(api_root, access_token, session_id): def get_devices(api_root, access_token, session_id):
"""Get a list of devices.""" """Get a list of devices."""
url = urljoin(api_root + '/', DEVICE_LIST_PATH) url = urljoin(api_root + '/', DEVICE_LIST_PATH)
req_data = {DATA_ROOT: {}} return lgedm_post(url, access_token=access_token,
headers = { session_id=session_id)['item']
'x-thinq-application-key': APP_KEY,
'x-thinq-security-key': SECURITY_KEY,
'x-thinq-token': access_token,
'x-thinq-jsessionId': session_id,
'Accept': 'application/json',
}
res = requests.post(url, json=req_data, headers=headers)
return res.json()[DATA_ROOT]['item']
class Gateway(object): class Gateway(object):