Esempio n. 1
0
    def parse(self,
              argmap,
              req=None,
              locations=None,
              validate=None,
              force_all=False):
        """Coroutine variant of `webargs.core.Parser`.

        Receives the same arguments as `webargs.core.Parser.parse`.
        """
        req = req if req is not None else self.get_default_request()
        assert req is not None, 'Must pass req object'
        ret = None
        validators = core._ensure_list_of_callables(validate)
        schema = self._get_schema(argmap, req)
        try:
            parsed = yield from self._parse_request(schema=schema,
                                                    req=req,
                                                    locations=locations)
            result = self.load(parsed, schema)
            self._validate_arguments(result.data, validators)
        except ma.exceptions.ValidationError as error:
            self._on_validation_error(error)
        else:
            ret = result.data
        finally:
            self.clear_cache()
        if force_all:
            core.fill_in_missing_args(ret, argmap)
        return ret
Esempio n. 2
0
    async def parse(
        self,
        argmap: ArgMap,
        req: Request = None,
        locations: typing.Iterable = None,
        validate: Validate = None,
        error_status_code: typing.Union[int, None] = None,
        error_headers: typing.Union[typing.Mapping[str, str], None] = None,
    ) -> typing.Union[typing.Mapping, None]:
        """Coroutine variant of `webargs.core.Parser`.

        Receives the same arguments as `webargs.core.Parser.parse`.
        """
        req = req if req is not None else self.get_default_request()
        assert req is not None, "Must pass req object"
        data = None
        validators = core._ensure_list_of_callables(validate)
        schema = self._get_schema(argmap, req)
        try:
            parsed = await self._parse_request(schema=schema,
                                               req=req,
                                               locations=locations
                                               or self.locations)
            result = schema.load(parsed)
            data = result.data if core.MARSHMALLOW_VERSION_INFO[
                0] < 3 else result
            self._validate_arguments(data, validators)
        except ma.exceptions.ValidationError as error:
            await self._on_validation_error(error, req, schema,
                                            error_status_code, error_headers)
        finally:
            self.clear_cache()
        return data
Esempio n. 3
0
    async def parse(
        self,
        argmap: ArgMap,
        req: Request = None,
        location: str = None,
        validate: Validate = None,
        error_status_code: typing.Union[int, None] = None,
        error_headers: typing.Union[typing.Mapping[str, str], None] = None,
    ) -> typing.Union[typing.Mapping, None]:
        """Coroutine variant of `webargs.core.Parser`.

        Receives the same arguments as `webargs.core.Parser.parse`.
        """
        self.clear_cache()  # in case someone used `location_load_*()`
        req = req if req is not None else self.get_default_request()
        if req is None:
            raise ValueError("Must pass req object")
        data = None
        validators = core._ensure_list_of_callables(validate)
        schema = self._get_schema(argmap, req)
        try:
            location_data = await self._load_location_data(
                schema=schema, req=req, location=location or self.location
            )
            result = schema.load(location_data)
            data = result.data if core.MARSHMALLOW_VERSION_INFO[0] < 3 else result
            self._validate_arguments(data, validators)
        except ma.exceptions.ValidationError as error:
            await self._on_validation_error(
                error, req, schema, error_status_code, error_headers
            )
        return data
Esempio n. 4
0
    async def parse(
        self, argmap, req=None, locations=None, validate=None, force_all=False
    ):
        """Coroutine variant of `webargs.core.Parser`.

        Receives the same arguments as `webargs.core.Parser.parse`.
        """
        req = req if req is not None else self.get_default_request()
        assert req is not None, "Must pass req object"
        data = None
        validators = core._ensure_list_of_callables(validate)
        schema = self._get_schema(argmap, req)
        try:
            parsed = await self._parse_request(
                schema=schema, req=req, locations=locations
            )
            result = schema.load(parsed)
            data = result.data if core.MARSHMALLOW_VERSION_INFO[0] < 3 else result
            self._validate_arguments(data, validators)
        except ma.exceptions.ValidationError as error:
            self._on_validation_error(error, req, schema)
        finally:
            self.clear_cache()
        if force_all:
            core.fill_in_missing_args(data, schema)
        return data
Esempio n. 5
0
    async def parse(
        self,
        argmap: core.ArgMap,
        req: typing.Optional[core.Request] = None,
        *,
        location: typing.Optional[str] = None,
        unknown: typing.Optional[str] = core._UNKNOWN_DEFAULT_PARAM,
        validate: core.ValidateArg = None,
        error_status_code: typing.Optional[int] = None,
        error_headers: typing.Optional[typing.Mapping[str, str]] = None
    ) -> typing.Optional[typing.Mapping]:
        """Coroutine variant of `webargs.core.Parser`.

        Receives the same arguments as `webargs.core.Parser.parse`.
        """
        req = req if req is not None else self.get_default_request()
        location = location or self.location
        unknown = (
            unknown
            if unknown != core._UNKNOWN_DEFAULT_PARAM
            else (
                self.unknown
                if self.unknown != core._UNKNOWN_DEFAULT_PARAM
                else self.DEFAULT_UNKNOWN_BY_LOCATION.get(location)
            )
        )
        load_kwargs = {"unknown": unknown}
        if req is None:
            raise ValueError("Must pass req object")
        data = None
        validators = core._ensure_list_of_callables(validate)
        schema = self._get_schema(argmap, req)
        try:
            location_data = await self._load_location_data(
                schema=schema, req=req, location=location
            )
            data = schema.load(location_data, **load_kwargs)
            self._validate_arguments(data, validators)
        except ma.exceptions.ValidationError as error:
            await self._async_on_validation_error(
                error,
                req,
                schema,
                location,
                error_status_code=error_status_code,
                error_headers=error_headers,
            )
        return data
Esempio n. 6
0
    def parse(  # type: ignore  # we have added the resource_object on top of parse
        self,
        resource_object: Resource,
        argmap: ArgMap,
        req: Optional[Request] = None,
        *,
        location: Optional[str] = None,
        unknown: Optional[str] = _UNKNOWN_DEFAULT_PARAM,  # pylint: disable=unused-argument
        validate: ValidateArg = None,
        error_status_code: Optional[int] = None,
        error_headers: Optional[Mapping[str, str]] = None,
    ) -> Dict[str, Any]:
        """Main request parsing method.

        Different from core parser is that we also get the resource object and
        pass it to the schema
        """
        # __import__("pdb").set_trace()
        req = req if req is not None else self.get_default_request(
        )  # type: ignore
        location = location or self.location
        if req is None:
            raise ValueError("Must pass req object")
        data = None
        validators = _ensure_list_of_callables(validate)
        schema = self._get_schema(argmap, resource_object)
        try:
            location_data = self._load_location_data(
                schema=schema,
                req=req,
                location=location,
            )
            data = schema.load(location_data)
            self._validate_arguments(data, validators)
        except ma_exceptions.ValidationError as error:
            self._on_validation_error(
                error,
                req,
                schema,
                location,
                error_status_code=error_status_code,
                error_headers=error_headers,
            )
        return data
Esempio n. 7
0
    def parse(
        self,
        resource_object: Resource,
        argmap: Union[Schema, Dict[str, Field], Callable],
        req: Optional[Request] = None,
        *,
        location: Optional[str] = None,
        validate: Optional[Callable[[Dict[str, Any]], bool]] = None,
        error_status_code: Optional[int] = None,
        error_headers: Optional[Dict[str, Any]] = None,
    ) -> Optional[Any]:
        """Main request parsing method.

        Different from core parser is that we also get the resource object and
        pass it to the schema
        """
        req = req if req is not None else self.get_default_request()
        location = location or self.location
        if req is None:
            raise ValueError("Must pass req object")
        data = None
        validators = _ensure_list_of_callables(validate)
        schema = self._get_schema(argmap, resource_object)
        try:
            location_data = self._load_location_data(
                schema=schema,
                req=req,
                location=location,
            )
            result = schema.load(location_data)
            data = result.data if MARSHMALLOW_VERSION_INFO[0] < 3 else result
            self._validate_arguments(data, validators)
        except ma_exceptions.ValidationError as error:
            self._on_validation_error(
                error,
                req,
                schema,
                location,
                error_status_code=error_status_code,
                error_headers=error_headers,
            )
        return data
Esempio n. 8
0
    def parse(self, argmap, req=None, locations=None, validate=None, force_all=False):
        """Coroutine variant of `webargs.core.Parser`.

        Receives the same arguments as `webargs.core.Parser.parse`.
        """
        req = req if req is not None else self.get_default_request()
        assert req is not None, 'Must pass req object'
        ret = None
        validators = core._ensure_list_of_callables(validate)
        try:
            parsed = yield from self._parse_request(argmap, req, locations)
            result = self.load(parsed, argmap)
            self._validate_arguments(result.data, validators)
        except ma.exceptions.ValidationError as error:
            self._on_validation_error(error)
        else:
            ret = result.data
        finally:
            self.clear_cache()
        if force_all:
            core.fill_in_missing_args(ret, argmap)
        return ret