コード例 #1
0
    def __init__(self,
                 fqid: str,
                 fields: Dict[str, JSON],
                 list_fields: ListFieldsData = {}) -> None:
        super().__init__(fqid)
        add_list_fields = list_fields.get("add", {})
        remove_list_fields = list_fields.get("remove", {})

        fields_keys = set(fields.keys())
        add_list_fields_keys = set(add_list_fields.keys())
        remove_list_fields_keys = set(remove_list_fields.keys())

        all_keys = fields_keys.union(add_list_fields_keys,
                                     remove_list_fields_keys)
        if len(all_keys) == 0:
            raise InvalidRequest("No fields are given")
        if fields_keys.intersection(
                add_list_fields_keys) or fields_keys.intersection(
                    remove_list_fields_keys):
            raise InvalidRequest(
                "Fields cannot be given both as normal fields and as list updates"
            )
        for field in all_keys:
            assert_is_field(field)
            assert_no_special_field(field)
        self.fields = fields
        self.list_fields = list_fields
コード例 #2
0
def reserve_ids():
    if not request.is_json:
        raise InvalidRequest("Data must be json")

    reserve_ids_handler = ReserveIdsHandler()
    ids = reserve_ids_handler.reserve_ids(request.get_json())
    return JsonResponse({"ids": ids})
コード例 #3
0
def write():
    if not request.is_json:
        raise InvalidRequest("Data must be json")

    write_handler = WriteHandler()
    write_handler.write(request.get_json())
    return "", 201
コード例 #4
0
    def reserve_ids(self, data: JSON) -> List[int]:
        try:
            parsed_data = ReserveIdsRequestJSON(**reserve_ids_schema(data))
        except fastjsonschema.JsonSchemaException as e:
            raise InvalidRequest(e.message)

        writer = injector.get(Writer)
        return writer.reserve_ids(parsed_data.collection, parsed_data.amount)
コード例 #5
0
    def parse_events(self, events: List[Dict[str,
                                             Any]]) -> List[BaseRequestEvent]:
        request_events = []
        for event in events:
            type = event["type"]

            if type == "create":
                fields = event.get("fields")
                if not isinstance(fields, dict):
                    raise InvalidRequest("Fields must be a dict")

            if type == "update":
                try:
                    update_event_schema(event)
                except fastjsonschema.JsonSchemaException as e:
                    raise InvalidRequest(e.message)

            request_events.append(self.create_event(event))
        return request_events
コード例 #6
0
    def build_write_request(self, data: JSON) -> WriteRequest:
        try:
            parsed_data = cast(WriteRequestJSON, write_schema(data))
        except fastjsonschema.JsonSchemaException as e:
            raise InvalidRequest(e.message)

        user_id = parsed_data["user_id"]
        information = parsed_data["information"]
        locked_fields = parsed_data["locked_fields"]
        events = self.parse_events(parsed_data["events"])

        return WriteRequest(events, information, user_id, locked_fields)
コード例 #7
0
    def handle_request(self, route: Route, data: JSON) -> Dict:
        """
        A generic handler for all requests. Parses the request to a python object
        according to the route_setup and execute the according route_handler.
        """

        try:
            route_configuration = route_configurations[route]
        except KeyError:
            raise BadCodingError("Invalid route metadata: " + route)

        logger.info(f"{route.upper()}-request: {data}")

        try:
            request_data = route_configuration.schema(data)
        except fastjsonschema.JsonSchemaException as e:
            if route_configuration.schema_error_handler:
                route_configuration.schema_error_handler(e)
            raise InvalidRequest(e.message)

        try:
            request_object = from_dict(
                route_configuration.request_class,
                request_data,
                Config(check_types=False),
            )
        except (TypeError, MissingValueError) as e:
            raise BadCodingError("Invalid data to initialize class\n" + str(e))

        reader = injector.get(Reader)
        route_handler = getattr(reader, route)

        if route_configuration.dev_only:
            route_handler = dev_only_route(route_handler)

        with reader.get_database_context():
            return route_handler(request_object)
コード例 #8
0
def handle_filter_schema_error(e: fastjsonschema.JsonSchemaException) -> None:
    if e.rule == "anyOf":
        # we only use anyOf for filters, so an invalid filter definition was given
        raise InvalidRequest(f"Invalid filter definition: {e.value}")