1 - | import rest_json_extras.input
|
2 - | import rest_json_extras.middleware
|
3 - | import rest_json_extras.output
|
4 - | import rest_json_extras.tls
|
5 - | import typing
|
6 - |
|
7 - | Ctx = typing.TypeVar('Ctx')
|
8 - |
|
9 - | class App(typing.Generic[Ctx]):
|
10 - | """
|
11 - | Main Python application, used to register operations and context and start multiple
|
12 - | workers on the same shared socket.
|
13 - |
|
14 - | Operations can be registered using the application object as a decorator (`@app.operation_name`).
|
15 - |
|
16 - | Here's a full example to get you started:
|
17 - |
|
18 - | ```python
|
19 - | from rest_json_extras import input
|
20 - | from rest_json_extras import output
|
21 - | from rest_json_extras import error
|
22 - | from rest_json_extras import middleware
|
23 - | from rest_json_extras import App
|
24 - |
|
25 - | @dataclass
|
26 - | class Context:
|
27 - | counter: int = 0
|
28 - |
|
29 - | app = App()
|
30 - | app.context(Context())
|
31 - |
|
32 - | @app.request_middleware
|
33 - | def request_middleware(request: middleware::Request):
|
34 - | if request.get_header("x-amzn-id") != "secret":
|
35 - | raise middleware.MiddlewareException("Unsupported `x-amz-id` header", 401)
|
36 - |
|
37 - | @app.case_insensitive_error_operation
|
38 - | def case_insensitive_error_operation(input: input::CaseInsensitiveErrorOperationInput, ctx: Context) -> output::CaseInsensitiveErrorOperationOutput:
|
39 - | raise NotImplementedError
|
40 - |
|
41 - | @app.empty_struct_with_content_on_wire_op
|
42 - | def empty_struct_with_content_on_wire_op(input: input::EmptyStructWithContentOnWireOpInput, ctx: Context) -> output::EmptyStructWithContentOnWireOpOutput:
|
43 - | raise NotImplementedError
|
44 - |
|
45 - | @app.enum_query
|
46 - | def enum_query(input: input::EnumQueryInput, ctx: Context) -> output::EnumQueryOutput:
|
47 - | raise NotImplementedError
|
48 - |
|
49 - | @app.escaped_string_values
|
50 - | def escaped_string_values(input: input::EscapedStringValuesInput, ctx: Context) -> output::EscapedStringValuesOutput:
|
51 - | raise NotImplementedError
|
52 - |
|
53 - | @app.map_with_enum_key_op
|
54 - | def map_with_enum_key_op(input: input::MapWithEnumKeyOpInput, ctx: Context) -> output::MapWithEnumKeyOpOutput:
|
55 - | raise NotImplementedError
|
56 - |
|
57 - | @app.null_in_non_sparse
|
58 - | def null_in_non_sparse(input: input::NullInNonSparseInput, ctx: Context) -> output::NullInNonSparseOutput:
|
59 - | raise NotImplementedError
|
60 - |
|
61 - | @app.primitive_int_header
|
62 - | def primitive_int_header(input: input::PrimitiveIntHeaderInput, ctx: Context) -> output::PrimitiveIntHeaderOutput:
|
63 - | raise NotImplementedError
|
64 - |
|
65 - | @app.primitive_int_op
|
66 - | def primitive_int_op(input: input::PrimitiveIntOpInput, ctx: Context) -> output::PrimitiveIntOpOutput:
|
67 - | raise NotImplementedError
|
68 - |
|
69 - | @app.query_precedence
|
70 - | def query_precedence(input: input::QueryPrecedenceInput, ctx: Context) -> output::QueryPrecedenceOutput:
|
71 - | raise NotImplementedError
|
72 - |
|
73 - | @app.status_response
|
74 - | def status_response(input: input::StatusResponseInput, ctx: Context) -> output::StatusResponseOutput:
|
75 - | raise NotImplementedError
|
76 - |
|
77 - | @app.string_payload
|
78 - | def string_payload(input: input::StringPayloadInput, ctx: Context) -> output::StringPayloadOutput:
|
79 - | raise NotImplementedError
|
80 - |
|
81 - | app.run()
|
82 - | ```
|
83 - |
|
84 - | Any of operations above can be written as well prepending the `async` keyword and
|
85 - | the Python application will automatically handle it and schedule it on the event loop for you.
|
86 - | """
|
87 - |
|
88 - | def case_insensitive_error_operation(self, func: typing.Union[typing.Callable[[rest_json_extras.input.CaseInsensitiveErrorOperationInput, Ctx], typing.Union[rest_json_extras.output.CaseInsensitiveErrorOperationOutput, typing.Awaitable[rest_json_extras.output.CaseInsensitiveErrorOperationOutput]]], typing.Callable[[rest_json_extras.input.CaseInsensitiveErrorOperationInput], typing.Union[rest_json_extras.output.CaseInsensitiveErrorOperationOutput, typing.Awaitable[rest_json_extras.output.CaseInsensitiveErrorOperationOutput]]]]) -> None:
|
89 - | """
|
90 - | Method to register `case_insensitive_error_operation` Python implementation inside the handlers map.
|
91 - | It can be used as a function decorator in Python.
|
92 - | """
|
93 - | ...
|
94 - |
|
95 - |
|
96 - | def context(self, context: Ctx) -> None:
|
97 - | """
|
98 - | Register a context object that will be shared between handlers.
|
99 - | """
|
100 - | ...
|
101 - |
|
102 - |
|
103 - | def empty_struct_with_content_on_wire_op(self, func: typing.Union[typing.Callable[[rest_json_extras.input.EmptyStructWithContentOnWireOpInput, Ctx], typing.Union[rest_json_extras.output.EmptyStructWithContentOnWireOpOutput, typing.Awaitable[rest_json_extras.output.EmptyStructWithContentOnWireOpOutput]]], typing.Callable[[rest_json_extras.input.EmptyStructWithContentOnWireOpInput], typing.Union[rest_json_extras.output.EmptyStructWithContentOnWireOpOutput, typing.Awaitable[rest_json_extras.output.EmptyStructWithContentOnWireOpOutput]]]]) -> None:
|
104 - | """
|
105 - | Method to register `empty_struct_with_content_on_wire_op` Python implementation inside the handlers map.
|
106 - | It can be used as a function decorator in Python.
|
107 - | """
|
108 - | ...
|
109 - |
|
110 - |
|
111 - | def enum_query(self, func: typing.Union[typing.Callable[[rest_json_extras.input.EnumQueryInput, Ctx], typing.Union[rest_json_extras.output.EnumQueryOutput, typing.Awaitable[rest_json_extras.output.EnumQueryOutput]]], typing.Callable[[rest_json_extras.input.EnumQueryInput], typing.Union[rest_json_extras.output.EnumQueryOutput, typing.Awaitable[rest_json_extras.output.EnumQueryOutput]]]]) -> None:
|
112 - | """
|
113 - | Method to register `enum_query` Python implementation inside the handlers map.
|
114 - | It can be used as a function decorator in Python.
|
115 - | """
|
116 - | ...
|
117 - |
|
118 - |
|
119 - | def escaped_string_values(self, func: typing.Union[typing.Callable[[rest_json_extras.input.EscapedStringValuesInput, Ctx], typing.Union[rest_json_extras.output.EscapedStringValuesOutput, typing.Awaitable[rest_json_extras.output.EscapedStringValuesOutput]]], typing.Callable[[rest_json_extras.input.EscapedStringValuesInput], typing.Union[rest_json_extras.output.EscapedStringValuesOutput, typing.Awaitable[rest_json_extras.output.EscapedStringValuesOutput]]]]) -> None:
|
120 - | """
|
121 - | Method to register `escaped_string_values` Python implementation inside the handlers map.
|
122 - | It can be used as a function decorator in Python.
|
123 - | """
|
124 - | ...
|
125 - |
|
126 - |
|
127 - | def map_with_enum_key_op(self, func: typing.Union[typing.Callable[[rest_json_extras.input.MapWithEnumKeyOpInput, Ctx], typing.Union[rest_json_extras.output.MapWithEnumKeyOpOutput, typing.Awaitable[rest_json_extras.output.MapWithEnumKeyOpOutput]]], typing.Callable[[rest_json_extras.input.MapWithEnumKeyOpInput], typing.Union[rest_json_extras.output.MapWithEnumKeyOpOutput, typing.Awaitable[rest_json_extras.output.MapWithEnumKeyOpOutput]]]]) -> None:
|
128 - | """
|
129 - | Method to register `map_with_enum_key_op` Python implementation inside the handlers map.
|
130 - | It can be used as a function decorator in Python.
|
131 - | """
|
132 - | ...
|
133 - |
|
134 - |
|
135 - | def middleware(self, func: typing.Callable[[rest_json_extras.middleware.Request, typing.Callable[[rest_json_extras.middleware.Request], typing.Awaitable[rest_json_extras.middleware.Response]]], typing.Awaitable[rest_json_extras.middleware.Response]]) -> None:
|
136 - | """
|
137 - | Register a Python function to be executed inside a Tower middleware layer.
|
138 - | """
|
139 - | ...
|
140 - |
|
141 - |
|
142 - | def null_in_non_sparse(self, func: typing.Union[typing.Callable[[rest_json_extras.input.NullInNonSparseInput, Ctx], typing.Union[rest_json_extras.output.NullInNonSparseOutput, typing.Awaitable[rest_json_extras.output.NullInNonSparseOutput]]], typing.Callable[[rest_json_extras.input.NullInNonSparseInput], typing.Union[rest_json_extras.output.NullInNonSparseOutput, typing.Awaitable[rest_json_extras.output.NullInNonSparseOutput]]]]) -> None:
|
143 - | """
|
144 - | Method to register `null_in_non_sparse` Python implementation inside the handlers map.
|
145 - | It can be used as a function decorator in Python.
|
146 - | """
|
147 - | ...
|
148 - |
|
149 - |
|
150 - | def primitive_int_header(self, func: typing.Union[typing.Callable[[rest_json_extras.input.PrimitiveIntHeaderInput, Ctx], typing.Union[rest_json_extras.output.PrimitiveIntHeaderOutput, typing.Awaitable[rest_json_extras.output.PrimitiveIntHeaderOutput]]], typing.Callable[[rest_json_extras.input.PrimitiveIntHeaderInput], typing.Union[rest_json_extras.output.PrimitiveIntHeaderOutput, typing.Awaitable[rest_json_extras.output.PrimitiveIntHeaderOutput]]]]) -> None:
|
151 - | """
|
152 - | Method to register `primitive_int_header` Python implementation inside the handlers map.
|
153 - | It can be used as a function decorator in Python.
|
154 - | """
|
155 - | ...
|
156 - |
|
157 - |
|
158 - | def primitive_int_op(self, func: typing.Union[typing.Callable[[rest_json_extras.input.PrimitiveIntOpInput, Ctx], typing.Union[rest_json_extras.output.PrimitiveIntOpOutput, typing.Awaitable[rest_json_extras.output.PrimitiveIntOpOutput]]], typing.Callable[[rest_json_extras.input.PrimitiveIntOpInput], typing.Union[rest_json_extras.output.PrimitiveIntOpOutput, typing.Awaitable[rest_json_extras.output.PrimitiveIntOpOutput]]]]) -> None:
|
159 - | """
|
160 - | Method to register `primitive_int_op` Python implementation inside the handlers map.
|
161 - | It can be used as a function decorator in Python.
|
162 - | """
|
163 - | ...
|
164 - |
|
165 - |
|
166 - | def query_precedence(self, func: typing.Union[typing.Callable[[rest_json_extras.input.QueryPrecedenceInput, Ctx], typing.Union[rest_json_extras.output.QueryPrecedenceOutput, typing.Awaitable[rest_json_extras.output.QueryPrecedenceOutput]]], typing.Callable[[rest_json_extras.input.QueryPrecedenceInput], typing.Union[rest_json_extras.output.QueryPrecedenceOutput, typing.Awaitable[rest_json_extras.output.QueryPrecedenceOutput]]]]) -> None:
|
167 - | """
|
168 - | Method to register `query_precedence` Python implementation inside the handlers map.
|
169 - | It can be used as a function decorator in Python.
|
170 - | """
|
171 - | ...
|
172 - |
|
173 - |
|
174 - | def run(self, address: typing.Optional[str] = ..., port: typing.Optional[int] = ..., backlog: typing.Optional[int] = ..., workers: typing.Optional[int] = ..., tls: typing.Optional[rest_json_extras.tls.TlsConfig] = ...) -> None:
|
175 - | """
|
176 - | Main entrypoint: start the server on multiple workers.
|
177 - | """
|
178 - | ...
|
179 - |
|
180 - |
|
181 - | def run_lambda(self) -> None:
|
182 - | """
|
183 - | Lambda entrypoint: start the server on Lambda.
|
184 - | """
|
185 - | ...
|
186 - |
|
187 - |
|
188 - | def start_worker(self) -> None:
|
189 - | """
|
190 - | Build the service and start a single worker.
|
191 - | """
|
192 - | ...
|
193 - |
|
194 - |
|
195 - | def status_response(self, func: typing.Union[typing.Callable[[rest_json_extras.input.StatusResponseInput, Ctx], typing.Union[rest_json_extras.output.StatusResponseOutput, typing.Awaitable[rest_json_extras.output.StatusResponseOutput]]], typing.Callable[[rest_json_extras.input.StatusResponseInput], typing.Union[rest_json_extras.output.StatusResponseOutput, typing.Awaitable[rest_json_extras.output.StatusResponseOutput]]]]) -> None:
|
196 - | """
|
197 - | Method to register `status_response` Python implementation inside the handlers map.
|
198 - | It can be used as a function decorator in Python.
|
199 - | """
|
200 - | ...
|
201 - |
|
202 - |
|
203 - | def string_payload(self, func: typing.Union[typing.Callable[[rest_json_extras.input.StringPayloadInput, Ctx], typing.Union[rest_json_extras.output.StringPayloadOutput, typing.Awaitable[rest_json_extras.output.StringPayloadOutput]]], typing.Callable[[rest_json_extras.input.StringPayloadInput], typing.Union[rest_json_extras.output.StringPayloadOutput, typing.Awaitable[rest_json_extras.output.StringPayloadOutput]]]]) -> None:
|
204 - | """
|
205 - | Method to register `string_payload` Python implementation inside the handlers map.
|
206 - | It can be used as a function decorator in Python.
|
207 - | """
|
208 - | ...
|
209 - |
|
210 - |
|
211 - | def __init__(self) -> None:
|
212 - | ...
|
213 - |
|
214 - |
|
215 - | CODEGEN_VERSION: str = ...
|