diff --git a/cozify/hub_api.py b/cozify/hub_api.py index 5869abc..0a677f0 100644 --- a/cozify/hub_api.py +++ b/cozify/hub_api.py @@ -31,20 +31,50 @@ def get(call, hub_token_header=True, base=apiPath, **kwargs): **remote(bool): If call is to be local or remote (bounced via cloud). **cloud_token(str): Cloud authentication token. Only needed if remote = True. """ + return _call(method=requests.get, + call=call, + hub_token_header=hub_token_header, + base=base, + **kwargs + ) + +def put(call, payload, hub_token_header=True, base=apiPath, **kwargs): + """PUT method for calling hub API. For rest of kwargs parameters see get() + + Args: + call(str): API path to call after apiPath, needs to include leading /. + payload(str): json string to push out as the payload. + hub_token_header(bool): Set to False to omit hub_token usage in call headers. + base(str): Base path to call from API instead of global apiPath. Defaults to apiPath. + """ + return _call(method=requests.put, + call=call, + hub_token_header=hub_token_header, + base=base, + payload=payload, + **kwargs + ) + +def _call(*, call, method, base, hub_token_header, payload=None, **kwargs): + """Backend for get & put + """ response = None headers = None + if hub_token_header: + headers = _headers(kwargs['hub_token']) + if kwargs['remote']: # remote call if 'cloud_token' not in kwargs: raise AttributeError('Asked to do remote call but no cloud_token provided.') - logging.debug('GET turned remote.') - response = cloud_api.remote(apicall=base + call, **kwargs) # should the remote call be also getting the headers? + logging.debug('_call routing to cloud.remote()') + response = cloud_api.remote(apicall=base + call, payload=payload, **kwargs) else: # local call if not kwargs['host']: raise AttributeError('Local call but no hostname was provided. Either set keyword remote or host.') if hub_token_header: headers = _headers(kwargs['hub_token']) try: - response = requests.get(_getBase(host=kwargs['host'], api=base) + call, headers=headers) + response = method(_getBase(host=kwargs['host'], api=base) + call, headers=headers, data=payload) except RequestException as e: raise APIError('connection failure', 'issues connection to \'{0}\': {1}'.format(kwargs['host'], e)) @@ -56,35 +86,6 @@ def get(call, hub_token_header=True, base=apiPath, **kwargs): else: raise APIError(response.status_code, '%s - %s - %s' % (response.reason, response.url, response.text)) -def put(call, payload, hub_token_header=True, base=apiPath, **kwargs): - """PUT method for calling hub API. - - Args: - call(str): API path to call after apiPath, needs to include leading /. - payload(str): json string to push out as the payload. - hub_token_header(bool): Set to False to omit hub_token usage in call headers. - base(str): Base path to call from API instead of global apiPath. Defaults to apiPath. - **host(str): ip address or hostname of hub. - **hub_token(str): Hub authentication token. - **remote(bool): If call is to be local or remote (bounced via cloud). - **cloud_token(str): Cloud authentication token. Only needed if remote = True. - """ - response = None - headers = None - if kwargs['remote'] and kwargs['cloud_token']: - response = cloud_api.remote(apicall=base + call, put=True, payload=payload, **kwargs) - else: - if hub_token_header: - headers = _headers(kwargs['hub_token']) - response = requests.put(_getBase(host=kwargs['host'], api=base) + call, headers=headers, data=payload) - - if response.status_code == 200: - return response.json() - elif response.status_code == 410: - raise APIError(response.status_code, 'API version outdated. Update python-cozify. %s - %s - %s' % (response.reason, response.url, response.text)) - else: - raise APIError(response.status_code, '%s - %s - %s' % (response.reason, response.url, response.text)) - def hub(**kwargs): """1:1 implementation of /hub API call. For kwargs see cozify.hub_api.get()