Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a check when performing a post. #102

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions wideq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

GATEWAY_URL = 'https://kic.lgthinq.com:46030/api/common/gatewayUriList'
GATEWAY_URL = 'https://kic.lgthinq.com:46030'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this change where we split up the base URL and path out for now—it looks interesting, but I would love to review it carefully in a separate PR.

APP_KEY = 'wideq'
SECURITY_KEY = 'nuts_securitykey'
DATA_ROOT = 'lgedmRoot'
Expand Down Expand Up @@ -177,7 +177,7 @@ def __init__(self, device_id, code):
}


def lgedm_post(url, data=None, access_token=None, session_id=None):
def lgedm_post(api_root, path, data=None, access_token=None, session_id=None):
"""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
Expand All @@ -199,7 +199,18 @@ def lgedm_post(url, data=None, access_token=None, session_id=None):
headers['x-thinq-jsessionId'] = session_id

with retry_session() as session:
res = session.post(url, json={DATA_ROOT: data}, headers=headers)
res = session.post(urljoin(api_root + '/', path),
json={DATA_ROOT: data}, headers=headers)

if "rtiControl" in path:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this from lgedm_post, which is a low-level communication primitive, to the places that actually use rtiControl. That way we won't have to check what kind of request we're doing; we'll do it in exactly the right place "by construction."

I think that might juts mean set_device_controls, or possibly that and get_device_config. Can we try it with just the former, and see if it works, and if not, do the latter?

session.post(
urljoin(api_root + '/', 'rti/delControlPermission'),
json={
DATA_ROOT: {'deviceId': data.get('deviceId')}
}, headers=headers
)
# Ignore the response, since it's not a breaking error!

out = res.json()[DATA_ROOT]

# Check for API errors.
Expand Down Expand Up @@ -248,14 +259,13 @@ def login(api_root, access_token, country, language):
return information about the session.
"""

url = urljoin(api_root + '/', 'member/login')
data = {
'countryCode': country,
'langCode': language,
'loginType': 'EMP',
'token': access_token,
}
return lgedm_post(url, data)
return lgedm_post(api_root, 'member/login', data)


def refresh_auth(oauth_root, refresh_token):
Expand Down Expand Up @@ -315,7 +325,7 @@ def discover(cls, country, language) -> 'Gateway':
`country` and `language` are codes, like "US" and "en-US,"
respectively.
"""
gw = lgedm_post(GATEWAY_URL,
gw = lgedm_post(GATEWAY_URL, '/api/common/gatewayUriList',
{'countryCode': country, 'langCode': language})
return cls(gw['empUri'], gw['thinqUri'], gw['oauthUri'],
country, language)
Expand Down Expand Up @@ -390,8 +400,8 @@ def post(self, path, data=None):
request from an active Session.
"""

url = urljoin(self.auth.gateway.api_root + '/', path)
return lgedm_post(url, data, self.auth.access_token, self.session_id)
return lgedm_post(self.auth.gateway.api_root, path, data,
self.auth.access_token, self.session_id)

def get_devices(self) -> List[Dict[str, Any]]:
"""Get a list of devices associated with the user's account.
Expand Down