def _get_request_args(self): """Extracts service request arguments from HTTP request. Supports various methods and forms of encoding. Separates arguments for special parameters from service operation parameters. Returns a dict with the service request arguments, containing key params with the actual values for the service operation parameters. """ str_args = False request_args = {} if request.method == "POST" or request.method == "PUT": # Use only body args and ignore any args from query string if request.headers.get("content-type", "").startswith(CONT_TYPE_JSON): # JSON body request if request.data: request_args = json_loads(request.data) if GATEWAY_ARG_PARAMS not in request_args: # Magic fallback: Directly use JSON first level as args if params key not present request_args = {GATEWAY_ARG_PARAMS: request_args} elif request.form: # Form encoded payload if GATEWAY_ARG_JSON in request.form: payload = request.form[GATEWAY_ARG_JSON] request_args = json_loads(str(payload)) if GATEWAY_ARG_PARAMS not in request_args: # Magic fallback: Directly use JSON first level as args if params key not present request_args = {GATEWAY_ARG_PARAMS: request_args} else: # Fallback: Directly use form values str_args = True request_args = { GATEWAY_ARG_PARAMS: request.form.to_dict(flat=True) } else: # No args found in body request_args = {GATEWAY_ARG_PARAMS: {}} elif request.method == "GET": str_args = True REQ_ARGS_SPECIAL = {"authtoken", "timeout", "headers"} args_dict = request.args.to_dict(flat=True) request_args = { k: request.args[k] for k in args_dict if k in REQ_ARGS_SPECIAL } req_params = { k: request.args[k] for k in args_dict if k not in REQ_ARGS_SPECIAL } request_args[GATEWAY_ARG_PARAMS] = req_params request_args[ "str_args"] = str_args # Indicate downstream that args are str (GET or form encoded) #log.info("Request args: %s" % request_args) return request_args
def _get_request_args(self): """Extracts service request arguments from HTTP request. Supports various methods and forms of encoding. Separates arguments for special parameters from service operation parameters. Returns a dict with the service request arguments, containing key params with the actual values for the service operation parameters. """ str_args = False request_args = {} if request.method == "POST" or request.method == "PUT": # Use only body args and ignore any args from query string if request.headers.get("content-type", "").startswith(CONT_TYPE_JSON): # JSON body request if request.data: request_args = json_loads(request.data) if GATEWAY_ARG_PARAMS not in request_args: # Magic fallback: Directly use JSON first level as args if params key not present request_args = {GATEWAY_ARG_PARAMS: request_args} elif request.form: # Form encoded payload if GATEWAY_ARG_JSON in request.form: payload = request.form[GATEWAY_ARG_JSON] request_args = json_loads(payload) if GATEWAY_ARG_PARAMS not in request_args: # Magic fallback: Directly use JSON first level as args if params key not present request_args = {GATEWAY_ARG_PARAMS: request_args} else: # Fallback: Directly use form values str_args = True request_args = {GATEWAY_ARG_PARAMS: request.form.to_dict(flat=True)} else: # No args found in body request_args = {GATEWAY_ARG_PARAMS: {}} # Extract file args for file_arg in request.files: try: file_handle = request.files[file_arg] arg_val = file_handle.read() request_args[GATEWAY_ARG_PARAMS][file_arg] = arg_val except Exception as ex: log.exception("Error reading request file argument %s", file_arg) elif request.method == "GET": str_args = True REQ_ARGS_SPECIAL = {"authtoken", "timeout", "headers"} args_dict = request.args.to_dict(flat=True) request_args = {k: request.args[k] for k in args_dict if k in REQ_ARGS_SPECIAL} req_params = {k: request.args[k] for k in args_dict if k not in REQ_ARGS_SPECIAL} request_args[GATEWAY_ARG_PARAMS] = req_params request_args["str_args"] = str_args # Indicate downstream that args are str (GET or form encoded) #log.info("Request args: %s" % request_args) return request_args
def _extract_payload_data(self): request_obj = None if request.headers.get("content-type", "").startswith(CONT_TYPE_JSON): if request.data: request_obj = json_loads(request.data) elif request.form: # Form encoded if GATEWAY_ARG_JSON in request.form: payload = request.form[GATEWAY_ARG_JSON] request_obj = json_loads(str(payload)) if request_obj and is_ion_object_dict(request_obj): request_obj = self.create_ion_object(request_obj) return request_obj
def create_attachment(self): try: payload = request.form[GATEWAY_ARG_JSON] json_params = json_loads(str(payload)) actor_id, expiry = self.get_governance_info_from_request( json_params) actor_id, expiry = self.validate_request(actor_id, expiry) headers = self.build_message_headers(actor_id, expiry) data_params = json_params[GATEWAY_ARG_PARAMS] resource_id = str(data_params.get("resource_id", "")) fil = request.files["file"] content = fil.read() keywords = [] keywords_str = data_params.get("keywords", "") if keywords_str.strip(): keywords = [str(x.strip()) for x in keywords_str.split(",")] created_by = data_params.get("attachment_created_by", "unknown user") modified_by = data_params.get("attachment_modified_by", "unknown user") # build attachment attachment = Attachment( name=str(data_params["attachment_name"]), description=str(data_params["attachment_description"]), attachment_type=int(data_params["attachment_type"]), content_type=str(data_params["attachment_content_type"]), keywords=keywords, created_by=created_by, modified_by=modified_by, content=content) ret = self.rr_client.create_attachment(resource_id=resource_id, attachment=attachment, headers=headers) return self.gateway_json_response(ret) except Exception as ex: log.exception("Error creating attachment") return self.gateway_error_response(ex)
def create_attachment(self): try: payload = request.form[GATEWAY_ARG_JSON] json_params = json_loads(str(payload)) actor_id, expiry = self.get_governance_info_from_request(json_params) actor_id, expiry = self.validate_request(actor_id, expiry) headers = self.build_message_headers(actor_id, expiry) data_params = json_params[GATEWAY_ARG_PARAMS] resource_id = str(data_params.get("resource_id", "")) fil = request.files["file"] content = fil.read() keywords = [] keywords_str = data_params.get("keywords", "") if keywords_str.strip(): keywords = [str(x.strip()) for x in keywords_str.split(",")] created_by = data_params.get("attachment_created_by", "unknown user") modified_by = data_params.get("attachment_modified_by", "unknown user") # build attachment attachment = Attachment( name=str(data_params["attachment_name"]), description=str(data_params["attachment_description"]), attachment_type=int(data_params["attachment_type"]), content_type=str(data_params["attachment_content_type"]), keywords=keywords, created_by=created_by, modified_by=modified_by, content=content, ) ret = self.rr_client.create_attachment(resource_id=resource_id, attachment=attachment, headers=headers) return self.gateway_json_response(ret) except Exception as ex: log.exception("Error creating attachment") return self.gateway_error_response(ex)