339 lines
8.6 KiB
Python
339 lines
8.6 KiB
Python
|
from enum import Enum
|
||
|
from typing import Any, Callable, Optional, Sequence
|
||
|
|
||
|
from pydantic.fields import FieldInfo
|
||
|
|
||
|
|
||
|
class ParamTypes(Enum):
|
||
|
query = "query"
|
||
|
header = "header"
|
||
|
path = "path"
|
||
|
cookie = "cookie"
|
||
|
|
||
|
|
||
|
class Param(FieldInfo):
|
||
|
in_: ParamTypes
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
alias: Optional[str] = None,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
deprecated: Optional[bool] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
self.deprecated = deprecated
|
||
|
super().__init__(
|
||
|
default,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
def __repr__(self) -> str:
|
||
|
return f"{self.__class__.__name__}({self.default})"
|
||
|
|
||
|
|
||
|
class Path(Param):
|
||
|
in_ = ParamTypes.path
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
alias: Optional[str] = None,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
deprecated: Optional[bool] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
self.in_ = self.in_
|
||
|
super().__init__(
|
||
|
...,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
deprecated=deprecated,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
|
||
|
class Query(Param):
|
||
|
in_ = ParamTypes.query
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
alias: Optional[str] = None,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
deprecated: Optional[bool] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
super().__init__(
|
||
|
default,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
deprecated=deprecated,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
|
||
|
class Header(Param):
|
||
|
in_ = ParamTypes.header
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
alias: Optional[str] = None,
|
||
|
convert_underscores: bool = True,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
deprecated: Optional[bool] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
self.convert_underscores = convert_underscores
|
||
|
super().__init__(
|
||
|
default,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
deprecated=deprecated,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
|
||
|
class Cookie(Param):
|
||
|
in_ = ParamTypes.cookie
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
alias: Optional[str] = None,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
deprecated: Optional[bool] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
super().__init__(
|
||
|
default,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
deprecated=deprecated,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
|
||
|
class Body(FieldInfo):
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
embed: bool = False,
|
||
|
media_type: str = "application/json",
|
||
|
alias: Optional[str] = None,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
self.embed = embed
|
||
|
self.media_type = media_type
|
||
|
super().__init__(
|
||
|
default,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
def __repr__(self) -> str:
|
||
|
return f"{self.__class__.__name__}({self.default})"
|
||
|
|
||
|
|
||
|
class Form(Body):
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
media_type: str = "application/x-www-form-urlencoded",
|
||
|
alias: Optional[str] = None,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
super().__init__(
|
||
|
default,
|
||
|
embed=True,
|
||
|
media_type=media_type,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
|
||
|
class File(Form):
|
||
|
def __init__(
|
||
|
self,
|
||
|
default: Any,
|
||
|
*,
|
||
|
media_type: str = "multipart/form-data",
|
||
|
alias: Optional[str] = None,
|
||
|
title: Optional[str] = None,
|
||
|
description: Optional[str] = None,
|
||
|
gt: Optional[float] = None,
|
||
|
ge: Optional[float] = None,
|
||
|
lt: Optional[float] = None,
|
||
|
le: Optional[float] = None,
|
||
|
min_length: Optional[int] = None,
|
||
|
max_length: Optional[int] = None,
|
||
|
regex: Optional[str] = None,
|
||
|
**extra: Any,
|
||
|
):
|
||
|
super().__init__(
|
||
|
default,
|
||
|
media_type=media_type,
|
||
|
alias=alias,
|
||
|
title=title,
|
||
|
description=description,
|
||
|
gt=gt,
|
||
|
ge=ge,
|
||
|
lt=lt,
|
||
|
le=le,
|
||
|
min_length=min_length,
|
||
|
max_length=max_length,
|
||
|
regex=regex,
|
||
|
**extra,
|
||
|
)
|
||
|
|
||
|
|
||
|
class Depends:
|
||
|
def __init__(
|
||
|
self, dependency: Optional[Callable[..., Any]] = None, *, use_cache: bool = True
|
||
|
):
|
||
|
self.dependency = dependency
|
||
|
self.use_cache = use_cache
|
||
|
|
||
|
def __repr__(self) -> str:
|
||
|
attr = getattr(self.dependency, "__name__", type(self.dependency).__name__)
|
||
|
cache = "" if self.use_cache else ", use_cache=False"
|
||
|
return f"{self.__class__.__name__}({attr}{cache})"
|
||
|
|
||
|
|
||
|
class Security(Depends):
|
||
|
def __init__(
|
||
|
self,
|
||
|
dependency: Optional[Callable[..., Any]] = None,
|
||
|
*,
|
||
|
scopes: Optional[Sequence[str]] = None,
|
||
|
use_cache: bool = True,
|
||
|
):
|
||
|
super().__init__(dependency=dependency, use_cache=use_cache)
|
||
|
self.scopes = scopes or []
|