Created
October 18, 2021 19:03
-
-
Save adriangb/2b017960a4fcdf3bbcc4fa27a67187e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typing | |
from dataclasses import dataclass | |
@dataclass | |
class BodyBase: | |
content_type: str | |
@dataclass | |
class JsonBody(BodyBase): | |
content_type = "application/json" | |
@dataclass | |
class FormBody(BodyBase): | |
content_type = "x-www-form-urlencoded" | |
@dataclass | |
class MultipartFormDataBody(BodyBase): | |
content_type = "multipart/form-data" | |
def Body(content_type: str) -> typing.Any: | |
return BodyBase(content_type=content_type) # type: ignore | |
def Json() -> typing.Any: | |
return JsonBody() # type: ignore | |
def MultipartFormData() -> typing.Any: | |
return MultipartFormDataBody() # type: ignore | |
def Form() -> typing.Any: | |
return FormBody() # type: ignore | |
@dataclass | |
class JsonModel: | |
a: int | |
b: typing.Dict[str, typing.Any] | |
@dataclass | |
class FormModel: | |
abc: str | |
defg: int | |
@dataclass | |
class InnerMultipart: | |
json: JsonModel = Json() | |
@dataclass | |
class CustomMultipart: | |
# when introspection is run, all of these are verified | |
# to be instances of BodyBase | |
json: JsonModel = Json() | |
form: FormModel = Form() | |
nested: InnerMultipart = MultipartFormData() | |
def endpoint(body: CustomMultipart = MultipartFormData()): | |
body.json.a | |
body.nested.json.a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment