Skip to content

Applications

Air

Air(*, debug=False, routes=None, servers=None, dependencies=None, default_response_class=AirResponse, redirect_slashes=True, middleware=None, exception_handlers=None, on_startup=None, on_shutdown=None, lifespan=None, webhooks=None, deprecated=None, **extra)

Bases: FastAPI

FastAPI wrapper class with AirResponse as the default response class.

Parameters:

Name Type Description Default
debug bool

Enables additional logging or diagnostic output.

False
dependencies Optional[Sequence[Depends]]

A list of global dependencies, they will be applied to each path operation, including in sub-routers.

None
middleware Optional[Sequence[Middleware]]

List of middleware to be added when creating the application.

None
default_response_class Type[Response]

The default response class to be used.

AirResponse
redirect_slashes bool

Whether to detect and redirect slashes in URLs when the client doesn't use the same format.

True
on_startup Optional[Sequence[Callable[[], Any]]]

A list of startup event handler functions.

None
on_shutdown Optional[Sequence[Callable[[], Any]]]

A list of shutdown event handler functions.

None
lifespan Optional[Lifespan[AppType]]

A Lifespan context manager handler. This replaces startup and shutdown functions with a single context manager.

None

Example:

import air

app = air.Air()

Initialize Air app with AirResponse as default response class.

This preserves all FastAPI initialization parameters while setting AirResponse as the default response class.

Source code in src/air/applications.py
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
def __init__(
    self: AppType,
    *,
    debug: Annotated[
        bool,
        Doc(
            """
            Boolean indicating if debug tracebacks should be returned on server
            errors.

            Read more in the
            [Starlette docs for Applications](https://www.starlette.io/applications/#instantiating-the-application).
            """
        ),
    ] = False,
    routes: Annotated[
        Optional[List[BaseRoute]],
        Doc(
            """
            **Note**: you probably shouldn't use this parameter, it is inherited
            from Starlette and supported for compatibility.

            ---

            A list of routes to serve incoming HTTP and WebSocket requests.
            """
        ),
        deprecated(
            """
            You normally wouldn't use this parameter with FastAPI, it is inherited
            from Starlette and supported for compatibility.

            In FastAPI, you normally would use the *path operation methods*,
            like `app.get()`, `app.post()`, etc.
            """
        ),
    ] = None,
    servers: Annotated[
        Optional[List[Dict[str, Union[str, Any]]]],
        Doc(
            """
            A `list` of `dict`s with connectivity information to a target server.

            You would use it, for example, if your application is served from
            different domains and you want to use the same Swagger UI in the
            browser to interact with each of them (instead of having multiple
            browser tabs open). Or if you want to leave fixed the possible URLs.

            If the servers `list` is not provided, or is an empty `list`, the
            default value would be a `dict` with a `url` value of `/`.

            Each item in the `list` is a `dict` containing:

            * `url`: A URL to the target host. This URL supports Server Variables
            and MAY be relative, to indicate that the host location is relative
            to the location where the OpenAPI document is being served. Variable
            substitutions will be made when a variable is named in `{`brackets`}`.
            * `description`: An optional string describing the host designated by
            the URL. [CommonMark syntax](https://commonmark.org/) MAY be used for
            rich text representation.
            * `variables`: A `dict` between a variable name and its value. The value
                is used for substitution in the server's URL template.

            Read more in the
            [FastAPI docs for Behind a Proxy](https://fastapi.tiangolo.com/advanced/behind-a-proxy/#additional-servers).

            **Example**

            ```python
            from fastapi import FastAPI

            app = FastAPI(
                servers=[
                    {"url": "https://stag.example.com", "description": "Staging environment"},
                    {"url": "https://prod.example.com", "description": "Production environment"},
                ]
            )
            ```
            """
        ),
    ] = None,
    dependencies: Annotated[
        Optional[Sequence[Depends]],
        Doc(
            """
            A list of global dependencies, they will be applied to each
            *path operation*, including in sub-routers.

            Read more about it in the
            [FastAPI docs for Global Dependencies](https://fastapi.tiangolo.com/tutorial/dependencies/global-dependencies/).

            **Example**

            ```python
            from fastapi import Depends, FastAPI

            from .dependencies import func_dep_1, func_dep_2

            app = FastAPI(dependencies=[Depends(func_dep_1), Depends(func_dep_2)])
            ```
            """
        ),
    ] = None,
    default_response_class: Annotated[
        Type[Response],
        Doc(
            """
            The default response class to be used.
            Read more in the
            [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class).
            **Analogy**
            ```python
            from fastapi import FastAPI
            from air import AirResponse
            app = FastAPI(default_response_class=AirResponse)
            ```
            """
        ),
    ] = AirResponse,
    redirect_slashes: Annotated[
        bool,
        Doc(
            """
            Whether to detect and redirect slashes in URLs when the client doesn't
            use the same format.

            **Example**

            ```python
            from fastapi import FastAPI

            app = FastAPI(redirect_slashes=True)  # the default

            @app.get("/items/")
            async def read_items():
                return [{"item_id": "Foo"}]
            ```

            With this app, if a client goes to `/items` (without a trailing slash),
            they will be automatically redirected with an HTTP status code of 307
            to `/items/`.
            """
        ),
    ] = True,
    middleware: Annotated[
        Optional[Sequence[Middleware]],
        Doc(
            """
            List of middleware to be added when creating the application.

            In FastAPI you would normally do this with `app.add_middleware()`
            instead.

            Read more in the
            [FastAPI docs for Middleware](https://fastapi.tiangolo.com/tutorial/middleware/).
            """
        ),
    ] = None,
    exception_handlers: Annotated[
        Optional[
            Dict[
                Union[int, Type[Exception]],
                Callable[[Request, Any], Coroutine[Any, Any, Response]],
            ]
        ],
        Doc(
            """
            A dictionary with handlers for exceptions.

            In FastAPI, you would normally use the decorator
            `@app.exception_handler()`.

            Read more in the
            [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).
            """
        ),
    ] = None,
    on_startup: Annotated[
        Optional[Sequence[Callable[[], Any]]],
        Doc(
            """
            A list of startup event handler functions.

            You should instead use the `lifespan` handlers.

            Read more in the [FastAPI docs for `lifespan`](https://fastapi.tiangolo.com/advanced/events/).
            """
        ),
    ] = None,
    on_shutdown: Annotated[
        Optional[Sequence[Callable[[], Any]]],
        Doc(
            """
            A list of shutdown event handler functions.

            You should instead use the `lifespan` handlers.

            Read more in the
            [FastAPI docs for `lifespan`](https://fastapi.tiangolo.com/advanced/events/).
            """
        ),
    ] = None,
    lifespan: Annotated[
        Optional[Lifespan[AppType]],
        Doc(
            """
            A `Lifespan` context manager handler. This replaces `startup` and
            `shutdown` functions with a single context manager.

            Read more in the
            [FastAPI docs for `lifespan`](https://fastapi.tiangolo.com/advanced/events/).
            """
        ),
    ] = None,
    webhooks: Annotated[
        Optional[routing.APIRouter],
        Doc(
            """
            Add OpenAPI webhooks. This is similar to `callbacks` but it doesn't
            depend on specific *path operations*.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            **Note**: This is available since OpenAPI 3.1.0, FastAPI 0.99.0.

            Read more about it in the
            [FastAPI docs for OpenAPI Webhooks](https://fastapi.tiangolo.com/advanced/openapi-webhooks/).
            """
        ),
    ] = None,
    deprecated: Annotated[
        Optional[bool],
        Doc(
            """
            Mark all *path operations* as deprecated. You probably don't need it,
            but it's available.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Read more about it in the
            [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Extra keyword arguments to be stored in the app, not used by FastAPI
            anywhere.
            """
        ),
    ],
) -> None:
    """Initialize Air app with AirResponse as default response class.

    This preserves all FastAPI initialization parameters while setting
    AirResponse as the default response class.
    """
    super().__init__(
        debug=debug,
        routes=routes,
        servers=servers,
        dependencies=dependencies,
        default_response_class=default_response_class,
        middleware=middleware,
        exception_handlers=exception_handlers,
        on_startup=on_startup,
        on_shutdown=on_shutdown,
        lifespan=lifespan,
        webhooks=webhooks,
        deprecated=deprecated,
        **extra,
    )

page

page(func)

Decorator that creates a GET route using the function name as the path.

If the name of the function is "index", then the route is "/".

Examples:

import air

app = Air()

@app.page
def index():
    return H1("I am the home page")
Source code in src/air/applications.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def page(self, func):
    """Decorator that creates a GET route using the function name as the path.

    If the name of the function is "index", then the route is "/".

    Examples:

        import air

        app = Air()

        @app.page
        def index():
            return H1("I am the home page")
    """
    if func.__name__ == "index":
        route_name = "/"
    else:
        route_name = f"/{func.__name__}"
    return self.get(route_name)(func)