Ejemplo n.º 1
0
def __set_field_attrs(self, fields_dict):
    """
    Handle `PrefixedNested` fields binding.
    """
    for key, field in fields_dict.copy().items():
        if isinstance(field, PrefixedNested):
            field.bind(self, key, fields_dict)

    BaseSchema._BaseSchema__set_field_attrs(self, fields_dict)
Ejemplo n.º 2
0
def get_file_content_as_wrapped_schema(filename: str, schema: Schema,
                                       wrap_in: str) -> Any:
    """Read fixture text file as specified schema."""
    with open(filename, "r") as file:
        content = file.read()
    content = f'{{"{wrap_in}": {content}}}'
    return schema.loads(content)
Ejemplo n.º 3
0
    def parse(self, schema: Schema) -> object:
        json = self.request.get_json()
        json_data, errors = schema.load(json)

        if errors:
            raise ParserError(str(errors))

        return json_data
Ejemplo n.º 4
0
def use_kwargs(argmap: Union[Schema, dict[str, Union[Field, type]]],
               location: str = "query",
               **kwargs: Any) -> Callable:
    """Wrap the webargs @use_kwargs decorator with preferred default modifications.

    Primarily, we want the location argument to default to "query" so that the data
    comes from the query string. As of version 6.0, webargs defaults to "json", which is
    almost never correct for Tildes.

    We also need to set every schema's behavior for unknown fields to "exclude", so that
    it just ignores them, instead of erroring when there's unexpected data (as there
    almost always is, especially because of Intercooler).
    """
    # convert a dict argmap to a Schema (the same way webargs would on its own)
    if isinstance(argmap, dict):
        argmap = Schema.from_dict(argmap)()

    assert isinstance(argmap,
                      Schema)  # tell mypy the type is more restricted now

    argmap.unknown = EXCLUDE

    return pyramidparser.use_kwargs(argmap, location=location, **kwargs)
Ejemplo n.º 5
0
async def request(
    websession: aiohttp.ClientSession,
    method: str,
    url: str,
    data: Dict[str, Any],
    schema: Schema,
) -> models.GigyaResponse:
    """Send request to Gigya."""
    async with websession.request(method, url, data=data) as http_response:
        response_text = await http_response.text()
        _LOGGER.debug(
            "Received Gigya response %s on %s: %s",
            http_response.status,
            url,
            response_text,
        )
        gigya_response: models.GigyaResponse = schema.loads(response_text)
        # Check for Gigya error
        gigya_response.raise_for_error_code()
        # Check for HTTP error
        http_response.raise_for_status()

        return gigya_response
Ejemplo n.º 6
0
async def request(
    websession: aiohttp.ClientSession,
    method: str,
    url: str,
    data: Dict[str, Any],
    schema: Schema,
) -> models.GigyaResponse:
    """Send request to Gigya."""
    async with websession.request(method, url, data=data) as http_response:
        response_text = await http_response.text()
        # Disable logging on Gigya, to avoid unnecessary exposure.
        # _LOGGER.debug(
        #    "Received Gigya response %s on %s: %s",
        #    http_response.status,
        #    url,
        #    response_text,
        # )
        gigya_response: models.GigyaResponse = schema.loads(response_text)
        # Check for Gigya error
        gigya_response.raise_for_error_code()
        # Check for HTTP error
        http_response.raise_for_status()

        return gigya_response
Ejemplo n.º 7
0
 async def _get_by_schema(self, schema: Schema, url: str) -> Any:
     return schema.load(await self._get_json(url), unknown=self._unknown)
Ejemplo n.º 8
0
 async def _get_list_by_schema(self, schema: Schema, url: str) -> List[Any]:
     return schema.load(await self._get_json_list(url),
                        unknown=self._unknown,
                        many=True)
Ejemplo n.º 9
0
 def get_attributes(
         self, schema: Schema) -> Optional[KamereonVehicleDataAttributes]:
     """Return jwt token."""
     return (cast(KamereonVehicleDataAttributes,
                  schema.load(self.data.attributes))
             if self.data and self.data.attributes is not None else None)
Ejemplo n.º 10
0
def get_file_content_as_schema(filename: str, schema: Schema) -> Any:
    """Read fixture text file as specified schema."""
    with open(filename, "r", encoding="utf-8") as file:
        content = file.read()
    return schema.loads(content)
Ejemplo n.º 11
0
 def serialise(self, schema: Schema, person, many=None) -> str:
     return jsonify(schema.dump(person, many=many).data)
Ejemplo n.º 12
0
def get_response_content(path: str, schema: Schema) -> Any:
    """Read fixture text file as specified schema."""
    with open(path, "r") as file:
        json_data = file.read()
    return schema.loads(json_data)