def send_request(self, url_path, http_method, body_params=None, path_params=None, query_params=None): url = construct_url_path(url_path, path_params, query_params) data = json.dumps(body_params) if body_params else None try: self._display(http_method, 'url', url) if data: self._display(http_method, 'data', data) response, response_data = self.connection.send( url, data, method=http_method, headers=BASE_HEADERS) value = self._get_response_value(response_data) self._display(http_method, 'response', value) return { ResponseParams.SUCCESS: True, ResponseParams.STATUS_CODE: response.getcode(), ResponseParams.RESPONSE: self._response_to_json(value) } # Being invoked via JSON-RPC, this method does not serialize and pass HTTPError correctly to the method caller. # Thus, in order to handle non-200 responses, we need to wrap them into a simple structure and pass explicitly. except HTTPError as e: error_msg = to_text(e.read()) self._display(http_method, 'error', error_msg) return { ResponseParams.SUCCESS: False, ResponseParams.STATUS_CODE: e.code, ResponseParams.RESPONSE: self._response_to_json(error_msg) }
def send_request(self, method, params): """ Responsible for actual sending of data to the connection httpapi base plugin. Does some formatting as well. :param params: A formatted dictionary that was returned by self.common_datagram_params() before being called here. :param method: The preferred API Request method (GET, ADD, POST, etc....) :type method: basestring :return: Dictionary of status, if it logged in or not. """ try: if self.sid is None and params[0]["url"] != "sys/login/user": raise FAZBaseException( "An attempt was made to login with the SID None and URL != login url." ) except IndexError: raise FAZBaseException( "An attempt was made at communicating with a FAZ with " "no valid session and an incorrectly formatted request.") except Exception: raise FAZBaseException( "An attempt was made at communicating with a FAZ with " "no valid session and an unexpected error was discovered.") self._update_request_id() json_request = { "method": method, "params": params, "session": self.sid, "id": self.req_id, "verbose": 1 } data = json.dumps(json_request, ensure_ascii=False).replace('\\\\', '\\') try: # Sending URL and Data in Unicode, per Ansible Specifications for Connection Plugins response, response_data = self.connection.send( path=to_text(self._url), data=to_text(data), headers=BASE_HEADERS) # Get Unicode Response - Must convert from StringIO to unicode first so we can do a replace function below result = json.loads(to_text(response_data.getvalue())) self._update_self_from_response(result, self._url, data) return self._handle_response(result) except Exception as err: raise FAZBaseException(err)
def send_request(self, request_method, path, payload=None): data = json.dumps(payload) if payload else '{}' try: self._display_request(request_method) response, response_data = self.connection.send( path, payload, method=request_method, headers=BASE_HEADERS, force_basic_auth=True) value = self._get_response_value(response_data) return response.getcode(), self._response_to_json(value) except AnsibleConnectionFailure as e: if to_text('401') in to_text(e): return 401, 'Authentication failure' else: return 404, 'Object not found' except HTTPError as e: error = json.loads(e.read()) return e.code, error
def _send_service_request(self, path, error_msg_prefix, data=None, **kwargs): try: self._ignore_http_errors = True return self.connection.send(path, data, **kwargs) except HTTPError as e: # HttpApi connection does not read the error response from HTTPError, so we do it here and wrap it up in # ConnectionError, so the actual error message is displayed to the user. error_msg = self._response_to_json(to_text(e.read())) raise ConnectionError('%s: %s' % (error_msg_prefix, error_msg), http_code=e.code) finally: self._ignore_http_errors = False
def send_request(self, **message_kwargs): """ Responsible for actual sending of data to the connection httpapi base plugin. :param message_kwargs: A formatted dictionary containing request info: url, data, method :return: Status code and response data. """ url = message_kwargs.get('url', '/') data = message_kwargs.get('data', '') method = message_kwargs.get('method', 'GET') try: response, response_data = self.connection.send(url, data, method=method) response_status = None if hasattr(response, 'status'): response_status = response.status else: response_status = response.headers.status return response_status, to_text(response_data.getvalue()) except Exception as err: raise Exception(err)
def _get_response_value(self, response_data): return to_text(response_data.getvalue())