Example #1
0
File: base.py Project: dekoder/st2
        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
Example #2
0
        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)
Example #3
0
        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)
Example #4
0
        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
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
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)
Example #8
0
    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)
Example #9
0
    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)
Example #10
0
    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)
Example #11
0
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"]
Example #12
0
    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)
Example #13
0
    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
        )
Example #14
0
File: hooks.py Project: joshgre/st2
    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)
Example #15
0
    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)
Example #16
0
    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)
Example #17
0
File: auth.py Project: joshgre/st2
    def _abort_unauthorized(self, environ, start_response):
        body = json_encode({
            'faultstring': 'Unauthorized'
        })
        headers = [('Content-Type', 'application/json')]

        start_response('401 UNAUTHORIZED', headers)
        return [body]
Example #18
0
File: auth.py Project: joshgre/st2
    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]
Example #19
0
File: hooks.py Project: Pulsant/st2
    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)
Example #20
0
File: hooks.py Project: jonico/st2
    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)
Example #21
0
    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)))
Example #23
0
        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
Example #24
0
        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
Example #25
0
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'))
Example #26
0
File: hooks.py Project: Pulsant/st2
    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)
Example #27
0
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))
Example #28
0
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"))
Example #29
0
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)))
Example #30
0
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)))
Example #31
0
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)
Example #32
0
    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)
Example #33
0
    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)
Example #34
0
    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)
Example #35
0
    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
Example #36
0
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
Example #37
0
File: hooks.py Project: miqui/st2
 def _abort_other_errors():
     return webob.Response(json_encode(
         {'faultstring': 'Internal Server Error'}),
                           status=500)
Example #38
0
File: hooks.py Project: miqui/st2
 def _abort_unauthorized():
     return webob.Response(json_encode({'faultstring': 'Unauthorized'}),
                           status=401)
Example #39
0
 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
Example #40
0
    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)
Example #41
0
def _handle_error(status, exception):
    LOG.error(exception)
    pecan.response.status = status
    error = {'faultstring': exception.message}
    return json_encode(error)
Example #42
0
    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)
Example #43
0
File: base.py Project: agilee/st2
        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
Example #44
0
 def _abort_unauthorized():
     return webob.Response(json_encode({
         'faultstring': 'Unauthorized'
     }), status=401)
Example #45
0
 def _abort_other_errors():
     return webob.Response(json_encode({
         'faultstring': 'Internal Server Error'
     }), status=500)
Example #46
0
        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
Example #47
0
 def post(self):
     def_yaml = pecan.request.text
     result = self.validator.validate(def_yaml)
     return jsonify.json_encode(result)
Example #48
0
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
Example #49
0
        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
Example #50
0
 def _json_body__set(self, value):
     self.body = json_encode(value).encode('UTF-8')
Example #51
0
        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
Example #52
0
        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)
Example #53
0
        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
Example #54
0
 def _json_body__set(self, value):
     self.body = json_encode(value).encode('UTF-8')
Example #55
0
 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