def _send_request(url, data, headers): '''Sends a POST request given the endpoint URL, JSON-encodable data and a dictionary with headers. ''' request = urlrequest.Request(url, json.dumps(data).encode(), headers) # this will eventually raise errors, e.g. if there's an unexpected http # status code result_obj = urlrequest.urlopen(request) result = result_obj.read().decode('utf-8') try: result_data = json.loads(result) log('debug', 'Valid JSON found') except ValueError: raise errors.RemoteError('Invalid JSON', str(result)) else: return result_data
def _send_request(url, data, headers, http_session=None): """Sends a POST request given the endpoint URL, JSON-encodable data, a dictionary with headers and, optionally, a session object for requests. """ if http_session is None: http_session = requests.session() r = http_session.post(url, data=json.dumps(data), headers=headers) result = r.text # this will eventually raise errors, e.g. on timeout try: result_data = json.loads(result) log("debug", "Valid JSON found") except ValueError: raise errors.RemoteError("Invalid JSON", result) else: return result_data
def _send_request(url, data, headers, http_session=None): """Sends a POST request given the endpoint URL, JSON-encodable data, a dictionary with headers and, optionally, a session object for requests. """ if http_session is None: http_session = requests.session() r = http_session.post(url, data=json.dumps(data), headers=headers) result = r.text # this will eventually raise errors, e.g. on timeout try: result_data = json.loads(result) log('debug', 'Valid JSON found') except ValueError: raise errors.RemoteError('Invalid JSON', result) else: return result_data