1 - | import pokemon_service_server_sdk.input
|
2 - | import pokemon_service_server_sdk.middleware
|
3 - | import pokemon_service_server_sdk.output
|
4 - | import pokemon_service_server_sdk.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 pokemon_service_server_sdk import input
|
20 - | from pokemon_service_server_sdk import output
|
21 - | from pokemon_service_server_sdk import error
|
22 - | from pokemon_service_server_sdk import middleware
|
23 - | from pokemon_service_server_sdk 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 - | # Capture Pokémons via event streams.
|
38 - | @app.capture_pokemon
|
39 - | def capture_pokemon(input: input::CapturePokemonInput, ctx: Context) -> output::CapturePokemonOutput:
|
40 - | raise NotImplementedError
|
41 - |
|
42 - | # Health check operation, to check the service is up
|
43 - | # Not yet a deep check
|
44 - | @app.check_health
|
45 - | def check_health(input: input::CheckHealthInput, ctx: Context) -> output::CheckHealthOutput:
|
46 - | raise NotImplementedError
|
47 - |
|
48 - | # DoNothing operation, used to stress test the framework.
|
49 - | @app.do_nothing
|
50 - | def do_nothing(input: input::DoNothingInput, ctx: Context) -> output::DoNothingOutput:
|
51 - | raise NotImplementedError
|
52 - |
|
53 - | # Retrieve information about a Pokémon species.
|
54 - | @app.get_pokemon_species
|
55 - | def get_pokemon_species(input: input::GetPokemonSpeciesInput, ctx: Context) -> output::GetPokemonSpeciesOutput:
|
56 - | raise NotImplementedError
|
57 - |
|
58 - | # Retrieve HTTP server statistiscs, such as calls count.
|
59 - | @app.get_server_statistics
|
60 - | def get_server_statistics(input: input::GetServerStatisticsInput, ctx: Context) -> output::GetServerStatisticsOutput:
|
61 - | raise NotImplementedError
|
62 - |
|
63 - | # Retrieve information about your Pokédex.
|
64 - | @app.get_storage
|
65 - | def get_storage(input: input::GetStorageInput, ctx: Context) -> output::GetStorageOutput:
|
66 - | raise NotImplementedError
|
67 - |
|
68 - | # Fetch a radio song from the database and stream it back as a playable audio.
|
69 - | @app.stream_pokemon_radio
|
70 - | def stream_pokemon_radio(input: input::StreamPokemonRadioInput, ctx: Context) -> output::StreamPokemonRadioOutput:
|
71 - | raise NotImplementedError
|
72 - |
|
73 - | app.run()
|
74 - | ```
|
75 - |
|
76 - | Any of operations above can be written as well prepending the `async` keyword and
|
77 - | the Python application will automatically handle it and schedule it on the event loop for you.
|
78 - | """
|
79 - |
|
80 - | def capture_pokemon(self, func: typing.Union[typing.Callable[[pokemon_service_server_sdk.input.CapturePokemonInput, Ctx], typing.Union[pokemon_service_server_sdk.output.CapturePokemonOutput, typing.Awaitable[pokemon_service_server_sdk.output.CapturePokemonOutput]]], typing.Callable[[pokemon_service_server_sdk.input.CapturePokemonInput], typing.Union[pokemon_service_server_sdk.output.CapturePokemonOutput, typing.Awaitable[pokemon_service_server_sdk.output.CapturePokemonOutput]]]]) -> None:
|
81 - | """
|
82 - | Method to register `capture_pokemon` Python implementation inside the handlers map.
|
83 - | It can be used as a function decorator in Python.
|
84 - | """
|
85 - | ...
|
86 - |
|
87 - |
|
88 - | def check_health(self, func: typing.Union[typing.Callable[[pokemon_service_server_sdk.input.CheckHealthInput, Ctx], typing.Union[pokemon_service_server_sdk.output.CheckHealthOutput, typing.Awaitable[pokemon_service_server_sdk.output.CheckHealthOutput]]], typing.Callable[[pokemon_service_server_sdk.input.CheckHealthInput], typing.Union[pokemon_service_server_sdk.output.CheckHealthOutput, typing.Awaitable[pokemon_service_server_sdk.output.CheckHealthOutput]]]]) -> None:
|
89 - | """
|
90 - | Method to register `check_health` 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 do_nothing(self, func: typing.Union[typing.Callable[[pokemon_service_server_sdk.input.DoNothingInput, Ctx], typing.Union[pokemon_service_server_sdk.output.DoNothingOutput, typing.Awaitable[pokemon_service_server_sdk.output.DoNothingOutput]]], typing.Callable[[pokemon_service_server_sdk.input.DoNothingInput], typing.Union[pokemon_service_server_sdk.output.DoNothingOutput, typing.Awaitable[pokemon_service_server_sdk.output.DoNothingOutput]]]]) -> None:
|
104 - | """
|
105 - | Method to register `do_nothing` Python implementation inside the handlers map.
|
106 - | It can be used as a function decorator in Python.
|
107 - | """
|
108 - | ...
|
109 - |
|
110 - |
|
111 - | def get_pokemon_species(self, func: typing.Union[typing.Callable[[pokemon_service_server_sdk.input.GetPokemonSpeciesInput, Ctx], typing.Union[pokemon_service_server_sdk.output.GetPokemonSpeciesOutput, typing.Awaitable[pokemon_service_server_sdk.output.GetPokemonSpeciesOutput]]], typing.Callable[[pokemon_service_server_sdk.input.GetPokemonSpeciesInput], typing.Union[pokemon_service_server_sdk.output.GetPokemonSpeciesOutput, typing.Awaitable[pokemon_service_server_sdk.output.GetPokemonSpeciesOutput]]]]) -> None:
|
112 - | """
|
113 - | Method to register `get_pokemon_species` Python implementation inside the handlers map.
|
114 - | It can be used as a function decorator in Python.
|
115 - | """
|
116 - | ...
|
117 - |
|
118 - |
|
119 - | def get_server_statistics(self, func: typing.Union[typing.Callable[[pokemon_service_server_sdk.input.GetServerStatisticsInput, Ctx], typing.Union[pokemon_service_server_sdk.output.GetServerStatisticsOutput, typing.Awaitable[pokemon_service_server_sdk.output.GetServerStatisticsOutput]]], typing.Callable[[pokemon_service_server_sdk.input.GetServerStatisticsInput], typing.Union[pokemon_service_server_sdk.output.GetServerStatisticsOutput, typing.Awaitable[pokemon_service_server_sdk.output.GetServerStatisticsOutput]]]]) -> None:
|
120 - | """
|
121 - | Method to register `get_server_statistics` Python implementation inside the handlers map.
|
122 - | It can be used as a function decorator in Python.
|
123 - | """
|
124 - | ...
|
125 - |
|
126 - |
|
127 - | def get_storage(self, func: typing.Union[typing.Callable[[pokemon_service_server_sdk.input.GetStorageInput, Ctx], typing.Union[pokemon_service_server_sdk.output.GetStorageOutput, typing.Awaitable[pokemon_service_server_sdk.output.GetStorageOutput]]], typing.Callable[[pokemon_service_server_sdk.input.GetStorageInput], typing.Union[pokemon_service_server_sdk.output.GetStorageOutput, typing.Awaitable[pokemon_service_server_sdk.output.GetStorageOutput]]]]) -> None:
|
128 - | """
|
129 - | Method to register `get_storage` 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[[pokemon_service_server_sdk.middleware.Request, typing.Callable[[pokemon_service_server_sdk.middleware.Request], typing.Awaitable[pokemon_service_server_sdk.middleware.Response]]], typing.Awaitable[pokemon_service_server_sdk.middleware.Response]]) -> None:
|
136 - | """
|
137 - | Register a Python function to be executed inside a Tower middleware layer.
|
138 - | """
|
139 - | ...
|
140 - |
|
141 - |
|
142 - | def run(self, address: typing.Optional[str] = ..., port: typing.Optional[int] = ..., backlog: typing.Optional[int] = ..., workers: typing.Optional[int] = ..., tls: typing.Optional[pokemon_service_server_sdk.tls.TlsConfig] = ...) -> None:
|
143 - | """
|
144 - | Main entrypoint: start the server on multiple workers.
|
145 - | """
|
146 - | ...
|
147 - |
|
148 - |
|
149 - | def run_lambda(self) -> None:
|
150 - | """
|
151 - | Lambda entrypoint: start the server on Lambda.
|
152 - | """
|
153 - | ...
|
154 - |
|
155 - |
|
156 - | def start_worker(self) -> None:
|
157 - | """
|
158 - | Build the service and start a single worker.
|
159 - | """
|
160 - | ...
|
161 - |
|
162 - |
|
163 - | def stream_pokemon_radio(self, func: typing.Union[typing.Callable[[pokemon_service_server_sdk.input.StreamPokemonRadioInput, Ctx], typing.Union[pokemon_service_server_sdk.output.StreamPokemonRadioOutput, typing.Awaitable[pokemon_service_server_sdk.output.StreamPokemonRadioOutput]]], typing.Callable[[pokemon_service_server_sdk.input.StreamPokemonRadioInput], typing.Union[pokemon_service_server_sdk.output.StreamPokemonRadioOutput, typing.Awaitable[pokemon_service_server_sdk.output.StreamPokemonRadioOutput]]]]) -> None:
|
164 - | """
|
165 - | Method to register `stream_pokemon_radio` Python implementation inside the handlers map.
|
166 - | It can be used as a function decorator in Python.
|
167 - | """
|
168 - | ...
|
169 - |
|
170 - |
|
171 - | def __init__(self) -> None:
|
172 - | ...
|
173 - |
|
174 - |
|
175 - | CODEGEN_VERSION: str = ...
|