def callfunction(*args, **kwargs): args = list(args) types = copy.copy(arg_types) more = [args.pop(0)] if types: argspec = inspect.getargspec(f) names = argspec.args[1:] for name in names: try: a = args.pop(0) more.append(types.pop(0)(a)) except IndexError: try: kwargs[name] = types.pop(0)(kwargs[name]) except IndexError: LOG.warning( "Type definition for '%s' argument of '%s' " "is missing.", name, f.__name__) except KeyError: pass if body_cls: if pecan.request.body: data = pecan.request.json else: data = {} obj = body_cls(**data) try: obj.validate() except jsonschema.ValidationError as e: raise exc.HTTPBadRequest(detail=e.message, comment=traceback.format_exc()) except Exception as e: raise exc.HTTPInternalServerError( detail=e.message, comment=traceback.format_exc()) more.append(obj) args = tuple(more) + tuple(args) noop_codes = [ http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN ] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) result = f(*args, **kwargs) if status_code: pecan.response.status = status_code if content_type == 'application/json': return json_encode(result, indent=None) else: return result
def callfunction(*args, **kwargs): try: args = list(args) types = list(argtypes) more = [args.pop(0)] if len(types): argspec = inspect.getargspec(f) names = argspec.args[1:] for name in names: try: a = args.pop(0) more.append(types.pop(0)(a)) except IndexError: try: kwargs[name] = types.pop(0)(kwargs[name]) except IndexError: LOG.warning("Type definition for '%s' argument of '%s' " "is missing.", name, f.__name__) except KeyError: pass body_cls = opts.get('body') if body_cls: if pecan.request.body: try: obj = body_cls(**pecan.request.json) except jsonschema.exceptions.ValidationError as e: return _handle_error(http_client.BAD_REQUEST, e) more.append(obj) else: more.append(None) args = tuple(more) + tuple(args) status_code = opts.get('status_code') noop_codes = [http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) try: result = f(*args, **kwargs) if status_code: pecan.response.status = status_code if content_type == 'application/json': return json_encode(result) else: return result except exc.HTTPException as e: return _handle_error(e.wsgi_response.status, e) except Exception as e: return _handle_error(http_client.INTERNAL_SERVER_ERROR, e)
def callfunction(*args, **kwargs): try: args = list(args) types = list(argtypes) more = [args.pop(0)] if len(types): argspec = inspect.getargspec(f) names = argspec.args[1:] for name in names: try: a = args.pop(0) more.append(types.pop(0)(a)) except IndexError: try: kwargs[name] = types.pop(0)(kwargs[name]) except IndexError: LOG.warning( "Type definition for '%s' argument of '%s' " "is missing.", name, f.__name__) except KeyError: pass body_cls = opts.get('body') if body_cls and pecan.request.body: try: obj = body_cls(**pecan.request.json) except jsonschema.exceptions.ValidationError as e: return _handle_error(http_client.BAD_REQUEST, e) more.append(obj) args = tuple(more) + tuple(args) status_code = opts.get('status_code') noop_codes = [ http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN ] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) try: result = f(*args, **kwargs) if status_code: pecan.response.status = status_code if content_type == 'application/json': return json_encode(result) else: return result except exc.HTTPException as e: return _handle_error(e.wsgi_response.status, e) except Exception as e: return _handle_error(http_client.INTERNAL_SERVER_ERROR, e)
def callfunction(*args, **kwargs): args = list(args) types = copy.copy(arg_types) more = [args.pop(0)] if types: argspec = inspect.getargspec(f) names = argspec.args[1:] for name in names: try: a = args.pop(0) more.append(types.pop(0)(a)) except IndexError: try: kwargs[name] = types.pop(0)(kwargs[name]) except IndexError: LOG.warning("Type definition for '%s' argument of '%s' " "is missing.", name, f.__name__) except KeyError: pass if body_cls: if pecan.request.body: data = pecan.request.json else: data = {} obj = body_cls(**data) try: obj.validate() except jsonschema.ValidationError as e: raise exc.HTTPBadRequest(detail=e.message, comment=traceback.format_exc()) except Exception as e: raise exc.HTTPInternalServerError(detail=e.message, comment=traceback.format_exc()) more.append(obj) args = tuple(more) + tuple(args) noop_codes = [http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) result = f(*args, **kwargs) if status_code: pecan.response.status = status_code if content_type == 'application/json': return json_encode(result, indent=None) else: return result
def on_error(self, state, e): error_msg = getattr(e, 'comment', str(e)) LOG.debug('API call failed: %s', error_msg) if hasattr(e, 'body') and isinstance(e.body, dict): body = e.body else: body = {} if isinstance(e, exc.HTTPException): status_code = state.response.status message = str(e) else: status_code = httplib.INTERNAL_SERVER_ERROR message = 'Internal Server Error' body['faultstring'] = message response_body = json_encode(body) headers = state.response.headers or {} headers['Content-Type'] = 'application/json' headers['Content-Length'] = str(len(response_body)) return webob.Response(response_body, status=status_code, headers=headers)
def on_error(self, state, e): request_path = state.request.path if cfg.CONF.api.serve_webui_files and request_path.startswith( '/webui'): # We want to return regular error response for requests to /webui return error_msg = getattr(e, 'comment', str(e)) LOG.debug('API call failed: %s', error_msg) if hasattr(e, 'body') and isinstance(e.body, dict): body = e.body else: body = {} if isinstance(e, exc.HTTPException): status_code = state.response.status message = str(e) else: status_code = httplib.INTERNAL_SERVER_ERROR message = 'Internal Server Error' body['faultstring'] = message response_body = json_encode(body) headers = state.response.headers or {} headers['Content-Type'] = 'application/json' headers['Content-Length'] = str(len(response_body)) return webob.Response(response_body, status=status_code, headers=headers)
def fetch_pack_index(index_url=None, logger=None, allow_empty=False, proxy_config=None): """ Fetch the pack indexes (either from the config or provided as an argument) and return the object. """ logger = logger or LOG index_urls = _build_index_list(index_url) index, status = _fetch_and_compile_index( index_urls=index_urls, logger=logger, proxy_config=proxy_config ) # If one of the indexes on the list is unresponsive, we do not throw # immediately. The only case where an exception is raised is when no # results could be obtained from all listed indexes. # This behavior allows for mirrors / backups and handling connection # or network issues in one of the indexes. if not index and not allow_empty: raise ValueError( "No results from the %s: tried %s.\nStatus: %s" % ( ("index" if len(index_urls) == 1 else "indexes"), ", ".join(index_urls), json_encode(status, indent=4), ) ) return (index, status)
def on_error(self, state, exc): request_path = state.request.path if cfg.CONF.api.serve_webui_files and request_path.startswith( '/webui'): # We want to return regular error response for requests to /webui return status_code = state.response.status if status_code == httplib.NOT_FOUND: message = 'The resource could not be found' elif status_code == httplib.INTERNAL_SERVER_ERROR: message = 'Internal Server Error' else: message = str(exc) response_body = json_encode({'faultstring': message}) headers = state.response.headers or {} if headers.get('Content-Type', None) == 'application/json': # Already a JSON response return headers['Content-Type'] = 'application/json' headers['Content-Length'] = str(len(response_body)) return webob.Response(response_body, status=status_code, headers=headers)
def on_error(self, state, e): error_msg = getattr(e, 'comment', str(e)) LOG.debug('API call failed: %s', error_msg) LOG.debug(traceback.format_exc()) if hasattr(e, 'body') and isinstance(e.body, dict): body = e.body else: body = {} if isinstance(e, exc.HTTPException): status_code = state.response.status message = str(e) else: status_code = httplib.INTERNAL_SERVER_ERROR message = 'Internal Server Error' body['faultstring'] = message response_body = json_encode(body) headers = state.response.headers or {} headers['Content-Type'] = 'application/json' headers['Content-Length'] = str(len(response_body)) return webob.Response(response_body, status=status_code, headers=headers)
def _abort_other_errors(): body = json_encode({'faultstring': 'Internal Server Error'}) headers = {} headers['Content-Type'] = 'application/json' status = httplib.INTERNAL_SERVER_ERROR return webob.Response(body=body, status=status, headers=headers)
def _render(node, render_context): """ Render the node depending on its type """ if "template" in node: complex_type = False if isinstance(node["template"], list) or isinstance( node["template"], dict): node["template"] = json_encode(node["template"]) # Finds occurrences of "{{variable}}" and adds `to_complex` filter # so types are honored. If it doesn't follow that syntax then it's # rendered as a string. node["template"] = re.sub(r'"{{([A-z0-9_-]+)}}"', r"{{\1 | to_complex}}", node["template"]) LOG.debug("Rendering complex type: %s", node["template"]) complex_type = True LOG.debug("Rendering node: %s with context: %s", node, render_context) result = ENV.from_string(str(node["template"])).render(render_context) LOG.debug("Render complete: %s", result) if complex_type: result = json_decode(result) LOG.debug("Complex Type Rendered: %s", result) return result if "value" in node: return node["value"]
def on_error(self, state, exc): request_path = state.request.path if cfg.CONF.api.serve_webui_files and request_path.startswith('/webui'): # We want to return regular error response for requests to /webui return status_code = state.response.status if status_code == httplib.NOT_FOUND: message = 'The resource could not be found' elif status_code == httplib.INTERNAL_SERVER_ERROR: message = 'Internal Server Error' else: message = str(exc) response_body = json_encode({'faultstring': message}) headers = state.response.headers or {} if headers.get('Content-Type', None) == 'application/json': # Already a JSON response return headers['Content-Type'] = 'application/json' headers['Content-Length'] = str(len(response_body)) return webob.Response(response_body, status=status_code, headers=headers)
def __init__( self, body=None, status=None, headerlist=None, app_iter=None, content_type=None, *args, **kwargs, ): # Do some sanity checking, and turn json_body into an actual body if ( app_iter is None and body is None and ("json_body" in kwargs or "json" in kwargs) ): if "json_body" in kwargs: json_body = kwargs.pop("json_body") else: json_body = kwargs.pop("json") body = json_encode(json_body).encode("utf-8") if content_type is None: content_type = "application/json" super(Response, self).__init__( body, status, headerlist, app_iter, content_type, *args, **kwargs )
def _abort_unauthorized(): body = json_encode({'faultstring': 'Unauthorized'}) headers = {} headers['Content-Type'] = 'application/json' status = httplib.UNAUTHORIZED return webob.Response(body=body, status=status, headers=headers)
def on_error(self, state, e): request_path = state.request.path if cfg.CONF.api.serve_webui_files and request_path.startswith('/webui'): # We want to return regular error response for requests to /webui return error_msg = getattr(e, 'comment', str(e)) LOG.debug('API call failed: %s', error_msg) if hasattr(e, 'body') and isinstance(e.body, dict): body = e.body else: body = {} if isinstance(e, exc.HTTPException): status_code = state.response.status message = str(e) else: status_code = httplib.INTERNAL_SERVER_ERROR message = 'Internal Server Error' body['faultstring'] = message response_body = json_encode(body) headers = state.response.headers or {} headers['Content-Type'] = 'application/json' headers['Content-Length'] = str(len(response_body)) return webob.Response(response_body, status=status_code, headers=headers)
def test_json_encode_decode_roundtrip_compatibility_between_different_libs( self): class ObjectWithJsonMethod(object): def __json__(self): return {"mah": "json", "1": 2} input_data = [ "1", 1, None, True, False, [1, "a", True, None, [1, 2], { "a": "b", "c": 3 }], { "a": "b", "d": [1, 2, 3], "e": 5 }, ObjectWithJsonMethod(), b"bytes", ObjectId("5609e91832ed356d04a93cc0"), ] expected_data = [ "1", 1, None, True, False, [1, "a", True, None, [1, 2], { "a": "b", "c": 3 }], { "a": "b", "d": [1, 2, 3], "e": 5 }, { "mah": "json", "1": 2 }, "bytes", "5609e91832ed356d04a93cc0", ] json_libraries = ["json", "orjson"] for json_library in json_libraries: jsonify.DEFAULT_JSON_LIBRARY = json_library result_encoded = jsonify.json_encode(input_data) result_decoded = jsonify.json_decode(result_encoded) result_decoded_native = json.loads(result_encoded) self.assertEqual(result_decoded, expected_data) self.assertEqual(result_decoded, result_decoded_native)
def _abort_unauthorized(self, environ, start_response): body = json_encode({ 'faultstring': 'Unauthorized' }) headers = [('Content-Type', 'application/json')] start_response('401 UNAUTHORIZED', headers) return [body]
def _abort_other_errors(self, environ, start_response): body = json_encode({ 'faultstring': 'Internal Server Error' }) headers = [('Content-Type', 'application/json')] start_response('500 INTERNAL SERVER ERROR', headers) return [body]
def _abort_other_errors(): body = json_encode({ 'faultstring': 'Internal Server Error' }) headers = {} headers['Content-Type'] = 'application/json' status = httplib.INTERNAL_SERVER_ERROR return webob.Response(body=body, status=status, headers=headers)
def _abort_unauthorized(): body = json_encode({ 'faultstring': 'Unauthorized' }) headers = {} headers['Content-Type'] = 'application/json' status = httplib.UNAUTHORIZED return webob.Response(body=body, status=status, headers=headers)
def on_error(self, state, e): if hasattr(e, 'body') and isinstance(e.body, dict): body = e.body else: body = {} if isinstance(e, exc.HTTPException): status_code = state.response.status message = str(e) elif isinstance(e, db_exceptions.StackStormDBObjectNotFoundError): status_code = httplib.NOT_FOUND message = str(e) elif isinstance(e, db_exceptions.StackStormDBObjectConflictError): status_code = httplib.CONFLICT message = str(e) body['conflict-id'] = e.conflict_id elif isinstance(e, rbac_exceptions.AccessDeniedError): status_code = httplib.FORBIDDEN message = str(e) elif isinstance(e, (ValueValidationException, ValueError)): status_code = httplib.BAD_REQUEST message = getattr(e, 'message', str(e)) else: status_code = httplib.INTERNAL_SERVER_ERROR message = 'Internal Server Error' # Log the error is_internal_server_error = status_code == httplib.INTERNAL_SERVER_ERROR error_msg = getattr(e, 'comment', str(e)) extra = { 'exception_class': e.__class__.__name__, 'exception_message': str(e), 'exception_data': e.__dict__ } if is_internal_server_error: LOG.exception('API call failed: %s', error_msg, extra=extra) LOG.exception(traceback.format_exc()) else: LOG.debug('API call failed: %s', error_msg, extra=extra) if is_debugging_enabled(): LOG.debug(traceback.format_exc()) body['faultstring'] = message response_body = json_encode(body) headers = state.response.headers or {} headers['Content-Type'] = 'application/json' headers['Content-Length'] = str(len(response_body)) return webob.Response(response_body, status=status_code, headers=headers)
def format(gen): message = '''event: %s\ndata: %s\n\n''' for pack in gen: if not pack: # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type('\n') else: (event, body) = pack # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type(message % (event, json_encode(body, indent=None)))
def format_output_object(output_db_or_api): if isinstance(output_db_or_api, ActionExecutionOutputDB): data = ActionExecutionOutputAPI.from_model(output_db_or_api) elif isinstance(output_db_or_api, ActionExecutionOutputAPI): data = output_db_or_api else: raise ValueError("Unsupported format: %s" % (type(output_db_or_api))) event = "st2.execution.output__create" result = "event: %s\ndata: %s\n\n" % (event, json_encode(data, indent=None)) return result
def format_output_object(output_db_or_api): if isinstance(output_db_or_api, ActionExecutionOutputDB): data = ActionExecutionOutputAPI.from_model(output_db_or_api) elif isinstance(output_db_or_api, ActionExecutionOutputAPI): data = output_db_or_api else: raise ValueError('Unsupported format: %s' % (type(output_db_or_api))) event = 'st2.execution.output__create' result = 'event: %s\ndata: %s\n\n' % (event, json_encode(data, indent=None)) return result
def format(gen): message = '''event: %s\ndata: %s\n\n''' for pack in gen: if not pack: # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type(b'\n') else: (event, body) = pack # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type((message % (event, json_encode(body, indent=None))).encode('utf-8'))
def format(gen): # Yield initial state so client would receive the headers the moment it connects to the stream yield '\n' message = '''event: %s\ndata: %s\n\n''' for pack in gen: if not pack: yield '\n' else: (event, body) = pack yield message % (event, json_encode(body, indent=None))
def format(gen): message = """event: %s\ndata: %s\n\n""" for pack in gen: if not pack: # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type(b"\n") else: (event, body) = pack # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type( (message % (event, json_encode(body, indent=None))).encode("utf-8"))
def format(gen): # Yield initial state so client would receive the headers the moment it connects to the stream yield '\n' message = '''event: %s\ndata: %s\n\n''' for pack in gen: if not pack: # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type('\n') else: (event, body) = pack # Note: gunicorn wsgi handler expect bytes, not unicode yield six.binary_type(message % (event, json_encode(body, indent=None)))
def _handle_error(e, status_code, body=None, headers=None): """ Encodes error into a json response and returns. This was the default response to an error, HTML page, is skipped. """ pecan.response.status = status_code if headers: pecan.response.headers = headers # re-purposing body this way has drawbacks but the other options i.e. # to envelope the body inside error_body would be awkward for clients. error_body = body if body else dict() assert isinstance(error_body, dict) error_body['faultstring'] = e.message return json_encode(error_body)
def __init__(self, body=None, status=None, headerlist=None, app_iter=None, content_type=None, *args, **kwargs): # Do some sanity checking, and turn json_body into an actual body if app_iter is None and body is None and ('json_body' in kwargs or 'json' in kwargs): if 'json_body' in kwargs: json_body = kwargs.pop('json_body') else: json_body = kwargs.pop('json') body = json_encode(json_body).encode('UTF-8') if content_type is None: content_type = 'application/json' super(Response, self).__init__(body, status, headerlist, app_iter, content_type, *args, **kwargs)
def to_json(self): """ Return JSON representation of this key which is fully compatible with keyczar JSON key file format. :rtype: ``str`` """ data = { "hmacKey": { "hmacKeyString": self.hmac_key_string, "size": self.hmac_key_size, }, "aesKeyString": self.aes_key_string, "mode": self.mode.upper(), "size": int(self.size), } return json_encode(data)
def connect(self, raise_on_any_error=False): """ Connect to hosts in hosts list. Returns status of connect as a dict. :param raise_on_any_error: Optional Raise an exception even if connecting to one of the hosts fails. :type raise_on_any_error: ``boolean`` :rtype: ``dict`` of ``str`` to ``dict`` """ results = {} for host in self._hosts: while not concurrency_lib.is_green_pool_free(self._pool): concurrency_lib.sleep(self._scan_interval) self._pool.spawn( self._connect, host=host, results=results, raise_on_any_error=raise_on_any_error, ) concurrency_lib.green_pool_wait_all(self._pool) if self._successful_connects < 1: # We definitely have to raise an exception in this case. LOG.error( "Unable to connect to any of the hosts.", extra={"connect_results": results}, ) msg = ( "Unable to connect to any one of the hosts: %s.\n\n connect_errors=%s" % ( self._hosts, json_encode(results, indent=2), ) ) raise NoHostsConnectedToException(msg) return results
def serialize_positional_argument(argument_type, argument_value): """ Serialize the provided positional argument. Note: Serialization is NOT performed recursively since it doesn't make much sense for shell script actions (only the outter / top level value is serialized). """ if argument_type in ["string", "number", "float"]: if argument_value is None: argument_value = six.text_type("") return argument_value if isinstance(argument_value, (int, float)): argument_value = str(argument_value) if not isinstance(argument_value, six.text_type): # cast string non-unicode values to unicode argument_value = argument_value.decode("utf-8") elif argument_type == "boolean": # Booleans are serialized as string "1" and "0" if argument_value is not None: argument_value = "1" if bool(argument_value) else "0" else: argument_value = "" elif argument_type in ["array", "list"]: # Lists are serialized a comma delimited string (foo,bar,baz) argument_value = ",".join(map(str, argument_value)) if argument_value else "" elif argument_type == "object": # Objects are serialized as JSON argument_value = json_encode(argument_value) if argument_value else "" elif argument_type == "null": # None / null is serialized as en empty string argument_value = "" else: # Other values are simply cast to unicode string argument_value = six.text_type(argument_value) if argument_value else "" return argument_value
def _abort_other_errors(): return webob.Response(json_encode( {'faultstring': 'Internal Server Error'}), status=500)
def _abort_unauthorized(): return webob.Response(json_encode({'faultstring': 'Unauthorized'}), status=401)
def convert(self, items_list): if not isinstance(items_list, list): raise ValueError('Items to be converted should be a list.') json_doc = json_encode(items_list) return json_doc
def __call__(self, environ, start_response): # The middleware intercepts and handles all the errors happening down the call stack by # converting them to valid HTTP responses with semantically meaningful status codes and # predefined response structure (`{"faultstring": "..."}`). The earlier in the call stack is # going to be run, the less unhandled errors could slip to the wsgi layer. Keep in mind that # the middleware doesn't receive the headers that has been set down the call stack which # means that things like CorsMiddleware and RequestIDMiddleware should be highier up the # call stack to also apply to error responses. try: try: return self.app(environ, start_response) except NotFoundException: raise exc.HTTPNotFound() except Exception as e: status = getattr(e, 'code', exc.HTTPInternalServerError.code) if hasattr(e, 'detail') and not getattr(e, 'comment'): setattr(e, 'comment', getattr(e, 'detail')) if hasattr(e, 'body') and isinstance(getattr(e, 'body', None), dict): body = getattr(e, 'body', None) else: body = {} if isinstance(e, exc.HTTPException): status_code = status message = six.text_type(e) elif isinstance(e, db_exceptions.StackStormDBObjectNotFoundError): status_code = exc.HTTPNotFound.code message = six.text_type(e) elif isinstance(e, db_exceptions.StackStormDBObjectConflictError): status_code = exc.HTTPConflict.code message = six.text_type(e) body['conflict-id'] = getattr(e, 'conflict_id', None) elif isinstance(e, rbac_exceptions.AccessDeniedError): status_code = exc.HTTPForbidden.code message = six.text_type(e) elif isinstance(e, (ValueValidationException, ValueError, ValidationError)): status_code = exc.HTTPBadRequest.code message = getattr(e, 'message', six.text_type(e)) else: status_code = exc.HTTPInternalServerError.code message = 'Internal Server Error' # Log the error is_internal_server_error = status_code == exc.HTTPInternalServerError.code error_msg = getattr(e, 'comment', six.text_type(e)) extra = { 'exception_class': e.__class__.__name__, 'exception_message': six.text_type(e), 'exception_data': e.__dict__ } if is_internal_server_error: LOG.exception('API call failed: %s', error_msg, extra=extra) else: LOG.debug('API call failed: %s', error_msg, extra=extra) if is_debugging_enabled(): LOG.debug(traceback.format_exc()) body['faultstring'] = message response_body = json_encode(body) headers = { 'Content-Type': 'application/json', 'Content-Length': str(len(response_body)) } resp = Response(response_body, status=status_code, headers=headers) return resp(environ, start_response)
def _handle_error(status, exception): LOG.error(exception) pecan.response.status = status error = {'faultstring': exception.message} return json_encode(error)
def __call__(self, environ, start_response): try: try: return self.app(environ, start_response) except NotFoundException: raise exc.HTTPNotFound() except Exception as e: status = getattr(e, 'code', exc.HTTPInternalServerError.code) if hasattr(e, 'detail') and not getattr(e, 'comment'): setattr(e, 'comment', getattr(e, 'detail')) if hasattr(e, 'body') and isinstance(getattr(e, 'body', None), dict): body = getattr(e, 'body', None) else: body = {} if isinstance(e, exc.HTTPException): status_code = status message = str(e) elif isinstance(e, db_exceptions.StackStormDBObjectNotFoundError): status_code = exc.HTTPNotFound.code message = str(e) elif isinstance(e, db_exceptions.StackStormDBObjectConflictError): status_code = exc.HTTPConflict.code message = str(e) body['conflict-id'] = getattr(e, 'conflict_id', None) elif isinstance(e, rbac_exceptions.AccessDeniedError): status_code = exc.HTTPForbidden.code message = str(e) elif isinstance(e, (ValueValidationException, ValueError, ValidationError)): status_code = exc.HTTPBadRequest.code message = getattr(e, 'message', str(e)) else: status_code = exc.HTTPInternalServerError.code message = 'Internal Server Error' # Log the error is_internal_server_error = status_code == exc.HTTPInternalServerError.code error_msg = getattr(e, 'comment', str(e)) extra = { 'exception_class': e.__class__.__name__, 'exception_message': str(e), 'exception_data': e.__dict__ } if is_internal_server_error: LOG.exception('API call failed: %s', error_msg, extra=extra) LOG.exception(traceback.format_exc()) else: LOG.debug('API call failed: %s', error_msg, extra=extra) if is_debugging_enabled(): LOG.debug(traceback.format_exc()) body['faultstring'] = message response_body = json_encode(body) headers = { 'Content-Type': 'application/json', 'Content-Length': str(len(response_body)) } resp = Response(response_body, status=status_code, headers=headers) return resp(environ, start_response)
def callfunction(*args, **kwargs): args = list(args) types = copy.copy(arg_types) more = [args.pop(0)] if types: argspec = inspect.getargspec(f) names = argspec.args[1:] for name in names: try: a = args.pop(0) more.append(types.pop(0)(a)) except IndexError: try: kwargs[name] = types.pop(0)(kwargs[name]) except IndexError: LOG.warning("Type definition for '%s' argument of '%s' " "is missing.", name, f.__name__) except KeyError: pass if body_cls: if pecan.request.body: data = pecan.request.json else: data = {} obj = body_cls(**data) try: obj.validate() except jsonschema.ValidationError as e: raise exc.HTTPBadRequest(detail=e.message, comment=traceback.format_exc()) except Exception as e: raise exc.HTTPInternalServerError(detail=e.message, comment=traceback.format_exc()) more.append(obj) args = tuple(more) + tuple(args) noop_codes = [http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) try: result = f(*args, **kwargs) except TypeError as e: message = str(e) # Invalid number of arguments passed to the function meaning invalid path was # requested # Note: The check is hacky, but it works for now. func_name = f.__name__ pattern = "%s\(\) takes exactly \d+ arguments \(\d+ given\)" % (func_name) if re.search(pattern, message): raise exc.HTTPNotFound() else: raise e if status_code: pecan.response.status = status_code if content_type == "application/json": return json_encode(result, indent=None) else: return result
def _abort_unauthorized(): return webob.Response(json_encode({ 'faultstring': 'Unauthorized' }), status=401)
def _abort_other_errors(): return webob.Response(json_encode({ 'faultstring': 'Internal Server Error' }), status=500)
def callfunction(*args, **kwargs): args = list(args) more = [args.pop(0)] def cast_value(value_type, value): if value_type == bool: def cast_func(value): return value.lower() in ['1', 'true'] else: cast_func = value_type result = cast_func(value) return result if body_cls: if pecan.request.body: data = pecan.request.json obj = body_cls(**data) try: obj = obj.validate() except (jsonschema.ValidationError, ValueError) as e: raise exc.HTTPBadRequest(detail=e.message, comment=traceback.format_exc()) except Exception as e: raise exc.HTTPInternalServerError(detail=e.message, comment=traceback.format_exc()) else: obj = None more.append(obj) if arg_types: # Cast and transform arguments based on the provided arg_types specification result_args, result_kwargs = get_controller_args_for_types(func=f, arg_types=arg_types, args=args, kwargs=kwargs) more = more + result_args kwargs.update(result_kwargs) args = tuple(more) + tuple(args) noop_codes = [http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) try: result = f(*args, **kwargs) except TypeError as e: e = get_exception_for_type_error(func=f, exc=e) raise e except Exception as e: e = get_exception_for_uncaught_api_error(func=f, exc=e) raise e if status_code: pecan.response.status = status_code if content_type == 'application/json': if is_debugging_enabled(): indent = 4 else: indent = None return json_encode(result, indent=indent) else: return result
def post(self): def_yaml = pecan.request.text result = self.validator.validate(def_yaml) return jsonify.json_encode(result)
def _fetch_and_compile_index(index_urls, logger=None, proxy_config=None): """ Go through the index list and compile results into a single object. """ status = [] index = {} proxies_dict = {} verify = True if proxy_config: https_proxy = proxy_config.get("https_proxy", None) http_proxy = proxy_config.get("http_proxy", None) ca_bundle_path = proxy_config.get("proxy_ca_bundle_path", None) if https_proxy: proxies_dict["https"] = https_proxy verify = ca_bundle_path or True if http_proxy: proxies_dict["http"] = http_proxy for index_url in index_urls: index_status = { "url": index_url, "packs": 0, "message": None, "error": None, } index_json = None try: request = requests.get(index_url, proxies=proxies_dict, verify=verify) request.raise_for_status() index_json = request.json() except ValueError as e: index_status["error"] = "malformed" index_status["message"] = repr(e) except requests.exceptions.RequestException as e: index_status["error"] = "unresponsive" index_status["message"] = repr(e) except Exception as e: index_status["error"] = "other errors" index_status["message"] = repr(e) if index_json == {}: index_status["error"] = "empty" index_status["message"] = "The index URL returned an empty object." elif type(index_json) is list: index_status["error"] = "malformed" index_status["message"] = "Expected an index object, got a list instead." elif index_json and "packs" not in index_json: index_status["error"] = "malformed" index_status["message"] = 'Index object is missing "packs" attribute.' if index_status["error"]: logger.error( "Index parsing error: %s" % json_encode(index_status, indent=4) ) else: # TODO: Notify on a duplicate pack aka pack being overwritten from a different index packs_data = index_json["packs"] index_status["message"] = "Success." index_status["packs"] = len(packs_data) index.update(packs_data) status.append(index_status) return index, status
def callfunction(*args, **kwargs): function_name = f.__name__ args = list(args) types = copy.copy(arg_types) more = [args.pop(0)] if types: argspec = inspect.getargspec(f) names = argspec.args[1:] for name in names: try: a = args.pop(0) more.append(types.pop(0)(a)) except IndexError: try: kwargs[name] = types.pop(0)(kwargs[name]) except IndexError: LOG.warning("Type definition for '%s' argument of '%s' " "is missing.", name, f.__name__) except KeyError: pass if body_cls: if pecan.request.body: data = pecan.request.json else: data = {} obj = body_cls(**data) try: obj = obj.validate() except (jsonschema.ValidationError, ValueError) as e: raise exc.HTTPBadRequest(detail=e.message, comment=traceback.format_exc()) except Exception as e: raise exc.HTTPInternalServerError(detail=e.message, comment=traceback.format_exc()) # Set default pack if one is not provided for resource create if function_name == "post" and not hasattr(obj, "pack"): extra = {"resource_api": obj, "default_pack_name": DEFAULT_PACK_NAME} LOG.debug("Pack not provided in the body, setting a default pack name", extra=extra) setattr(obj, "pack", DEFAULT_PACK_NAME) more.append(obj) args = tuple(more) + tuple(args) noop_codes = [http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) try: result = f(*args, **kwargs) except TypeError as e: message = str(e) # Invalid number of arguments passed to the function meaning invalid path was # requested # Note: The check is hacky, but it works for now. func_name = f.__name__ pattern = "%s\(\) takes exactly \d+ arguments \(\d+ given\)" % (func_name) if re.search(pattern, message): raise exc.HTTPNotFound() else: raise e if status_code: pecan.response.status = status_code if content_type == "application/json": if is_debugging_enabled(): indent = 4 else: indent = None return json_encode(result, indent=indent) else: return result
def _json_body__set(self, value): self.body = json_encode(value).encode('UTF-8')
def callfunction(*args, **kwargs): function_name = f.__name__ args = list(args) more = [args.pop(0)] def cast_value(value_type, value): if value_type == bool: def cast_func(value): return value.lower() in ['1', 'true'] else: cast_func = value_type result = cast_func(value) return result if body_cls: if pecan.request.body: data = pecan.request.json obj = body_cls(**data) try: obj = obj.validate() except (jsonschema.ValidationError, ValueError) as e: raise exc.HTTPBadRequest(detail=e.message, comment=traceback.format_exc()) except Exception as e: raise exc.HTTPInternalServerError(detail=e.message, comment=traceback.format_exc()) # Set default pack if one is not provided for resource create if function_name == 'post' and not hasattr(obj, 'pack'): extra = { 'resource_api': obj, 'default_pack_name': DEFAULT_PACK_NAME } LOG.debug('Pack not provided in the body, setting a default pack name', extra=extra) setattr(obj, 'pack', DEFAULT_PACK_NAME) else: obj = None more.append(obj) if arg_types: # Cast and transform arguments based on the provided arg_types specification result_args, result_kwargs = get_controller_args_for_types(func=f, arg_types=arg_types, args=args, kwargs=kwargs) more = more + result_args kwargs.update(result_kwargs) args = tuple(more) + tuple(args) noop_codes = [http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) try: result = f(*args, **kwargs) except TypeError as e: e = get_exception_for_type_error(func=f, exc=e) raise e if status_code: pecan.response.status = status_code if content_type == 'application/json': if is_debugging_enabled(): indent = 4 else: indent = None return json_encode(result, indent=indent) else: return result
def callfunction(*args, **kwargs): controller = args[0] if args else None # Note: We use getattr since in some places (tests) request is mocked params = getattr(pecan.request, 'params', {}) method = getattr(pecan.request, 'method', None) path = getattr(pecan.request, 'path', None) remote_addr = getattr(pecan.request, 'remote_addr', None) # Common request information included in the log context request_info = { 'method': method, 'path': path, 'remote_addr': remote_addr } # Log the incoming request values = copy.copy(request_info) values['filters'] = kwargs LOG.info('%(method)s %(path)s with filters=%(filters)s' % values, extra=values) if QUERY_PARAM_ATTRIBUTE_NAME in params and QUERY_PARAM_ATTRIBUTE_NAME in kwargs: # Remove auth token if one is provided via query params del kwargs[QUERY_PARAM_ATTRIBUTE_NAME] try: args = list(args) types = copy.copy(arg_types) more = [args.pop(0)] if types: argspec = inspect.getargspec(f) names = argspec.args[1:] for name in names: try: a = args.pop(0) more.append(types.pop(0)(a)) except IndexError: try: kwargs[name] = types.pop(0)(kwargs[name]) except IndexError: LOG.warning( "Type definition for '%s' argument of '%s' " "is missing.", name, f.__name__) except KeyError: pass if body_cls: if pecan.request.body: data = pecan.request.json else: data = {} try: obj = body_cls(**data) except jsonschema.exceptions.ValidationError as e: return _handle_error(e, http_client.BAD_REQUEST) more.append(obj) args = tuple(more) + tuple(args) noop_codes = [ http_client.NOT_IMPLEMENTED, http_client.METHOD_NOT_ALLOWED, http_client.FORBIDDEN ] if status_code and status_code in noop_codes: pecan.response.status = status_code return json_encode(None) try: result = f(*args, **kwargs) # Log the outgoing response values = copy.copy(request_info) values[ 'status_code'] = status_code or pecan.response.status function_name = f.__name__ controller_name = controller.__class__.__name__ log_result = True log_result &= function_name not in RESPONSE_LOGGING_METHOD_NAME_BLACKLIST log_result &= controller_name not in RESPONSE_LOGGING_CONTROLLER_NAME_BLACKLIST if log_result: values['result'] = result log_msg = '%(method)s %(path)s result=%(result)s' % values else: # Note: We don't want to include a result for some # methods which have a large result log_msg = '%(method)s %(path)s' % values LOG.info(log_msg, extra=values) if status_code: pecan.response.status = status_code if content_type == 'application/json': return json_encode(result) else: return result except exc.HTTPUnauthorized as e: LOG.debug('API call failed: %s' % (str(e))) return _handle_error(e, e.wsgi_response.status_code, e.wsgi_response.body, e.headers) except exc.HTTPException as e: LOG.exception('API call failed: %s' % (str(e))) # Exception contains pecan.response.header + more. This is per implementation # of the WSGIHTTPException type from WebOb. return _handle_error(e, e.wsgi_response.status_code, e.wsgi_response.body, e.headers) except Exception as e: LOG.exception('API call failed: %s' % (str(e))) return _handle_error(e, http_client.INTERNAL_SERVER_ERROR)
def convert(self, items_list): if not isinstance(items_list, list): raise ValueError("Items to be converted should be a list.") json_doc = json_encode(items_list) return json_doc