Пример #1
0
        async def new_func(self: View, **kwargs):

            if schema:
                schema_name, response_data = None, await original_function(
                    self, **kwargs)
            else:
                schema_name, response_data = await original_function(
                    self, **kwargs)

            try:
                matched_schema, matched_return_code = named_schema[schema_name]
                if matched_schema is not None:
                    return web.json_response(
                        matched_schema.dump(response_data),
                        status=matched_return_code.status_code)
                else:
                    raise matched_return_code
            except (ValidationError, KeyError) as err:
                response_schema = JSendSchema()
                response_data = response_schema.dump({
                    "status":
                    JSendStatus.ERROR,
                    "data":
                    err.messages
                    if isinstance(err, ValidationError) else err.args,
                    "message":
                    "We tried to send you data back, but it came out wrong."
                })
                return web.json_response(
                    response_data, status=HTTPStatus.INTERNAL_SERVER_ERROR)
Пример #2
0
        async def new_func(self: View, **kwargs):

            # if the request is not JSON or missing, return a warning and the valid schema
            if not self.request.body_exists or not self.request.content_type == "application/json":
                response_schema = JSendSchema()
                response_data = response_schema.dump({
                    "status": JSendStatus.FAIL,
                    "data": {
                        "message":
                        f"This route ({self.request.method}: {self.request.rel_url}) only accepts JSON.",
                        "schema": json_schema
                    }
                })
                return web.json_response(response_data,
                                         status=HTTPStatus.BAD_REQUEST)

            try:
                self.request[into] = schema.load(await self.request.json())
            except JSONDecodeError as err:
                # if the data is not valid json, return a warning
                response_schema = JSendSchema()
                response_data = response_schema.dump({
                    "status": JSendStatus.FAIL,
                    "data": {
                        "message": f"Could not parse supplied JSON.",
                        "errors": err.args
                    }
                })
                return web.json_response(response_data,
                                         status=HTTPStatus.BAD_REQUEST)
            except ValidationError as err:
                # if the json data does not match the schema, return the errors and the valid schema
                response_schema = JSendSchema()
                response_data = response_schema.dump({
                    "status": JSendStatus.FAIL,
                    "data": {
                        "message": "The request did not validate properly.",
                        "errors": err.messages,
                        "schema": json_schema
                    }
                })
                return web.json_response(response_data,
                                         status=HTTPStatus.BAD_REQUEST)

            # if everything passes, execute the original function
            return await original_function(self, **kwargs)