def _stream_chunk_to_request_result(self, response, raw_chunk, start_time, end_time): """ Converts a stream chunk to a RequestResult. """ response_content = parse_json_or_none(raw_chunk) return RequestResult("POST", "/stream", self._query, self._data, raw_chunk, response_content, None, response.headers, start_time, end_time)
def _execute(self, action, path, data=None, query=None, with_txn_time=False): """Performs an HTTP action, logs it, and looks for errors.""" if query is not None: query = {k: v for k, v in query.items() if v is not None} headers = {} if with_txn_time: headers.update(self._last_txn_time.request_header) start_time = time() response = self._perform_request(action, path, data, query, headers) end_time = time() if with_txn_time: if "X-Txn-Time" in response.headers: new_txn_time = int(response.headers["X-Txn-Time"]) self.sync_last_txn_time(new_txn_time) response_raw = response.text response_content = parse_json_or_none(response_raw) request_result = RequestResult( action, path, query, data, response_raw, response_content, response.status_code, response.headers, start_time, end_time) if self.observer is not None: self.observer(request_result) if response_content is None: raise UnexpectedError("Invalid JSON.", request_result) FaunaError.raise_for_status_code(request_result) return _get_or_raise(request_result, response_content, "resource")