1 - | import naming_test_ops.input
|
2 - | import naming_test_ops.middleware
|
3 - | import naming_test_ops.output
|
4 - | import naming_test_ops.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 naming_test_ops import input
|
20 - | from naming_test_ops import output
|
21 - | from naming_test_ops import error
|
22 - | from naming_test_ops import middleware
|
23 - | from naming_test_ops 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.err_collisions
|
38 - | def err_collisions(input: input::ErrCollisionsInput, ctx: Context) -> output::ErrCollisionsOutput:
|
39 - | raise NotImplementedError
|
40 - |
|
41 - | @app.match
|
42 - | def match(input: input::MatchInput, ctx: Context) -> output::MatchOutput:
|
43 - | raise NotImplementedError
|
44 - |
|
45 - | @app.option
|
46 - | def option(input: input::OptionInput, ctx: Context) -> output::OptionOutput:
|
47 - | raise NotImplementedError
|
48 - |
|
49 - | @app.reserved_words_as_members
|
50 - | def reserved_words_as_members(input: input::ReservedWordsAsMembersInput, ctx: Context) -> output::ReservedWordsAsMembersOutput:
|
51 - | raise NotImplementedError
|
52 - |
|
53 - | @app.result
|
54 - | def result(input: input::ResultInput, ctx: Context) -> output::ResultOutput:
|
55 - | raise NotImplementedError
|
56 - |
|
57 - | @app.rpc_echo
|
58 - | def rpc_echo(input: input::RpcEchoInput, ctx: Context) -> output::RpcEchoOutput:
|
59 - | raise NotImplementedError
|
60 - |
|
61 - | @app.structure_name_punning
|
62 - | def structure_name_punning(input: input::StructureNamePunningInput, ctx: Context) -> output::StructureNamePunningOutput:
|
63 - | raise NotImplementedError
|
64 - |
|
65 - | app.run()
|
66 - | ```
|
67 - |
|
68 - | Any of operations above can be written as well prepending the `async` keyword and
|
69 - | the Python application will automatically handle it and schedule it on the event loop for you.
|
70 - | """
|
71 - |
|
72 - | def context(self, context: Ctx) -> None:
|
73 - | """
|
74 - | Register a context object that will be shared between handlers.
|
75 - | """
|
76 - | ...
|
77 - |
|
78 - |
|
79 - | def err_collisions(self, func: typing.Union[typing.Callable[[naming_test_ops.input.ErrCollisionsInput, Ctx], typing.Union[naming_test_ops.output.ErrCollisionsOutput, typing.Awaitable[naming_test_ops.output.ErrCollisionsOutput]]], typing.Callable[[naming_test_ops.input.ErrCollisionsInput], typing.Union[naming_test_ops.output.ErrCollisionsOutput, typing.Awaitable[naming_test_ops.output.ErrCollisionsOutput]]]]) -> None:
|
80 - | """
|
81 - | Method to register `err_collisions` Python implementation inside the handlers map.
|
82 - | It can be used as a function decorator in Python.
|
83 - | """
|
84 - | ...
|
85 - |
|
86 - |
|
87 - | def match(self, func: typing.Union[typing.Callable[[naming_test_ops.input.MatchInput, Ctx], typing.Union[naming_test_ops.output.MatchOutput, typing.Awaitable[naming_test_ops.output.MatchOutput]]], typing.Callable[[naming_test_ops.input.MatchInput], typing.Union[naming_test_ops.output.MatchOutput, typing.Awaitable[naming_test_ops.output.MatchOutput]]]]) -> None:
|
88 - | """
|
89 - | Method to register `r#match` Python implementation inside the handlers map.
|
90 - | It can be used as a function decorator in Python.
|
91 - | """
|
92 - | ...
|
93 - |
|
94 - |
|
95 - | def middleware(self, func: typing.Callable[[naming_test_ops.middleware.Request, typing.Callable[[naming_test_ops.middleware.Request], typing.Awaitable[naming_test_ops.middleware.Response]]], typing.Awaitable[naming_test_ops.middleware.Response]]) -> None:
|
96 - | """
|
97 - | Register a Python function to be executed inside a Tower middleware layer.
|
98 - | """
|
99 - | ...
|
100 - |
|
101 - |
|
102 - | def option(self, func: typing.Union[typing.Callable[[naming_test_ops.input.OptionInput, Ctx], typing.Union[naming_test_ops.output.OptionOutput, typing.Awaitable[naming_test_ops.output.OptionOutput]]], typing.Callable[[naming_test_ops.input.OptionInput], typing.Union[naming_test_ops.output.OptionOutput, typing.Awaitable[naming_test_ops.output.OptionOutput]]]]) -> None:
|
103 - | """
|
104 - | Method to register `option` Python implementation inside the handlers map.
|
105 - | It can be used as a function decorator in Python.
|
106 - | """
|
107 - | ...
|
108 - |
|
109 - |
|
110 - | def reserved_words_as_members(self, func: typing.Union[typing.Callable[[naming_test_ops.input.ReservedWordsAsMembersInput, Ctx], typing.Union[naming_test_ops.output.ReservedWordsAsMembersOutput, typing.Awaitable[naming_test_ops.output.ReservedWordsAsMembersOutput]]], typing.Callable[[naming_test_ops.input.ReservedWordsAsMembersInput], typing.Union[naming_test_ops.output.ReservedWordsAsMembersOutput, typing.Awaitable[naming_test_ops.output.ReservedWordsAsMembersOutput]]]]) -> None:
|
111 - | """
|
112 - | Method to register `reserved_words_as_members` Python implementation inside the handlers map.
|
113 - | It can be used as a function decorator in Python.
|
114 - | """
|
115 - | ...
|
116 - |
|
117 - |
|
118 - | def result(self, func: typing.Union[typing.Callable[[naming_test_ops.input.ResultInput, Ctx], typing.Union[naming_test_ops.output.ResultOutput, typing.Awaitable[naming_test_ops.output.ResultOutput]]], typing.Callable[[naming_test_ops.input.ResultInput], typing.Union[naming_test_ops.output.ResultOutput, typing.Awaitable[naming_test_ops.output.ResultOutput]]]]) -> None:
|
119 - | """
|
120 - | Method to register `result` Python implementation inside the handlers map.
|
121 - | It can be used as a function decorator in Python.
|
122 - | """
|
123 - | ...
|
124 - |
|
125 - |
|
126 - | def rpc_echo(self, func: typing.Union[typing.Callable[[naming_test_ops.input.RPCEchoInput, Ctx], typing.Union[naming_test_ops.output.RPCEchoOutput, typing.Awaitable[naming_test_ops.output.RPCEchoOutput]]], typing.Callable[[naming_test_ops.input.RPCEchoInput], typing.Union[naming_test_ops.output.RPCEchoOutput, typing.Awaitable[naming_test_ops.output.RPCEchoOutput]]]]) -> None:
|
127 - | """
|
128 - | Method to register `rpc_echo` Python implementation inside the handlers map.
|
129 - | It can be used as a function decorator in Python.
|
130 - | """
|
131 - | ...
|
132 - |
|
133 - |
|
134 - | def run(self, address: typing.Optional[str] = ..., port: typing.Optional[int] = ..., backlog: typing.Optional[int] = ..., workers: typing.Optional[int] = ..., tls: typing.Optional[naming_test_ops.tls.TlsConfig] = ...) -> None:
|
135 - | """
|
136 - | Main entrypoint: start the server on multiple workers.
|
137 - | """
|
138 - | ...
|
139 - |
|
140 - |
|
141 - | def run_lambda(self) -> None:
|
142 - | """
|
143 - | Lambda entrypoint: start the server on Lambda.
|
144 - | """
|
145 - | ...
|
146 - |
|
147 - |
|
148 - | def start_worker(self) -> None:
|
149 - | """
|
150 - | Build the service and start a single worker.
|
151 - | """
|
152 - | ...
|
153 - |
|
154 - |
|
155 - | def structure_name_punning(self, func: typing.Union[typing.Callable[[naming_test_ops.input.StructureNamePunningInput, Ctx], typing.Union[naming_test_ops.output.StructureNamePunningOutput, typing.Awaitable[naming_test_ops.output.StructureNamePunningOutput]]], typing.Callable[[naming_test_ops.input.StructureNamePunningInput], typing.Union[naming_test_ops.output.StructureNamePunningOutput, typing.Awaitable[naming_test_ops.output.StructureNamePunningOutput]]]]) -> None:
|
156 - | """
|
157 - | Method to register `structure_name_punning` Python implementation inside the handlers map.
|
158 - | It can be used as a function decorator in Python.
|
159 - | """
|
160 - | ...
|
161 - |
|
162 - |
|
163 - | def __init__(self) -> None:
|
164 - | ...
|
165 - |
|
166 - |
|
167 - | CODEGEN_VERSION: str = ...
|