Exemplo n.º 1
0
    def _get_fields(
        self, path: str, handler: typing.Callable
    ) -> typing.Tuple[typing.Dict[str, Field], typing.Dict[str, Field], Field]:
        query_fields: typing.Dict[str, Field] = {}
        path_fields: typing.Dict[str, Field] = {}
        body_field: Field = None

        # Get path param names
        path_names = [item.strip("{}").lstrip("+") for item in re.findall("{[^}]*}", path)]

        # Iterate over all params
        parameters = inspect.signature(handler).parameters
        for name, param in parameters.items():
            # Matches as path param
            if name in path_names:
                schema = {
                    param.empty: None,
                    int: validators.Integer(),
                    float: validators.Number(),
                    str: validators.String(),
                }[param.annotation]
                path_fields[name] = Field(name=name, location=Location.path, schema=schema)

            # Matches as query param
            elif param.annotation in (param.empty, int, float, bool, str, http.QueryParam):
                if param.default is param.empty:
                    kwargs = {}
                elif param.default is None:
                    kwargs = {"default": None, "allow_null": True}
                else:
                    kwargs = {"default": param.default}
                schema = {
                    param.empty: None,
                    int: validators.Integer(**kwargs),
                    float: validators.Number(**kwargs),
                    bool: validators.Boolean(**kwargs),
                    str: validators.String(**kwargs),
                    http.QueryParam: validators.String(**kwargs),
                }[param.annotation]
                query_fields[name] = Field(name=name, location=Location.query, schema=schema)

            # Body params
            elif issubclass(param.annotation, types.Type):
                body_field = Field(name=name, location=Location.body, schema=param.annotation.validator)

        return query_fields, path_fields, body_field
Exemplo n.º 2
0
class User(types.Type):
    name = validators.String(max_length=10)
    age = validators.Integer(minimum=0, allow_null=True, default=None)
Exemplo n.º 3
0
class Place(types.Type):
    location = Location
    name = validators.String(max_length=100)
Exemplo n.º 4
0
class ReviewedProduct(Product):
    reviewer = validators.String(max_length=20)
Exemplo n.º 5
0
 class Something(types.Type):
     keys = validators.String()
Exemplo n.º 6
0
class Product(types.Type):
    name = validators.String(max_length=10)
    rating = validators.Integer(allow_null=True, default=None, minimum=0, maximum=100)
    created = validators.DateTime()