-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathpynest_factory.py
71 lines (58 loc) · 1.93 KB
/
pynest_factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import Type, TypeVar, overload, Union, Optional
from nest.core.pynest_application import PyNestApp
from nest.core.pynest_container import PyNestContainer
from nest.engine.proto import App
from nest.engine.fastapi import FastAPIApp
ModuleType = TypeVar("ModuleType")
class PyNestFactory:
"""Factory class for creating PyNest applications."""
@staticmethod
@overload
def create(
main_module: Type[ModuleType],
app_cls: Type[FastAPIApp],
title: str = "",
description: str = "",
version: Optional[Union[str, int, float]] = None,
debug: bool = False,
) -> PyNestApp:
"""
Create a PyNest application of FastAPIApp kind.
"""
@staticmethod
@overload
def create(
main_module: Type[ModuleType],
app_cls: Type[App],
) -> PyNestApp:
"""
Create a PyNest application of FastAPIApp kind.
"""
@staticmethod
def create(
main_module: Type[ModuleType],
app_cls: Type[App] = FastAPIApp,
**kwargs
) -> PyNestApp:
"""
Create a PyNest application with the specified main module class.
Args:
main_module (ModuleType): The main module for the PyNest application.
**kwargs: Additional keyword arguments for the FastAPI server.
Returns:
PyNestApp: The created PyNest application.
"""
container = PyNestContainer()
container.add_module(main_module)
http_server = PyNestFactory._create_server(app_cls, **kwargs)
return PyNestApp(container, http_server)
@staticmethod
def _create_server(app_cls: Type[App], **kwargs) -> App:
"""
Create a FastAPI server.
Args:
**kwargs: Additional keyword arguments for the FastAPI server.
Returns:
FastAPI: The created FastAPI server.
"""
return app_cls(**kwargs)