@@ -69,9 +69,7 @@ def get_module_data_from_path(path: Path) -> ModuleData:
6969 )
7070
7171
72- def get_app_infos (
73- * , mod_data : ModuleData , app_name : str | None = None
74- ) -> tuple [str , str | None , str | None , str | None ]:
72+ def get_app_name (* , mod_data : ModuleData , app_name : str | None = None ) -> str :
7573 try :
7674 mod = importlib .import_module (mod_data .module_import_str )
7775 except (ImportError , ValueError ) as e :
@@ -80,41 +78,32 @@ def get_app_infos(
8078 "Ensure all the package directories have an [blue]__init__.py[/blue] file"
8179 )
8280 raise
83-
8481 if not FastAPI : # type: ignore[truthy-function]
8582 raise FastAPICLIException (
8683 "Could not import FastAPI, try running 'pip install fastapi'"
8784 ) from None
88-
8985 object_names = dir (mod )
9086 object_names_set = set (object_names )
91-
9287 if app_name :
9388 if app_name not in object_names_set :
9489 raise FastAPICLIException (
9590 f"Could not find app name { app_name } in { mod_data .module_import_str } "
9691 )
97-
9892 app = getattr (mod , app_name )
99-
10093 if not isinstance (app , FastAPI ):
10194 raise FastAPICLIException (
10295 f"The app name { app_name } in { mod_data .module_import_str } doesn't seem to be a FastAPI app"
10396 )
104-
105- return app_name , app .openapi_url , app .docs_url , app .redoc_url
106-
97+ return app_name
10798 for preferred_name in ["app" , "api" ]:
10899 if preferred_name in object_names_set :
109100 obj = getattr (mod , preferred_name )
110101 if isinstance (obj , FastAPI ):
111- return preferred_name , obj .openapi_url , obj .docs_url , obj .redoc_url
112-
102+ return preferred_name
113103 for name in object_names :
114104 obj = getattr (mod , name )
115105 if isinstance (obj , FastAPI ):
116- return name , obj .openapi_url , obj .docs_url , obj .redoc_url
117-
106+ return name
118107 raise FastAPICLIException ("Could not find FastAPI app in module, try using --app" )
119108
120109
@@ -123,9 +112,6 @@ class ImportData:
123112 app_name : str
124113 module_data : ModuleData
125114 import_string : str
126- openapi_url : str | None = None
127- docs_url : str | None = None
128- redoc_url : str | None = None
129115
130116
131117def get_import_data (
@@ -139,22 +125,14 @@ def get_import_data(
139125
140126 if not path .exists ():
141127 raise FastAPICLIException (f"Path does not exist { path } " )
142-
143128 mod_data = get_module_data_from_path (path )
144129 sys .path .insert (0 , str (mod_data .extra_sys_path ))
145- use_app_name , openapi_url , docs_url , redoc_url = get_app_infos (
146- mod_data = mod_data , app_name = app_name
147- )
130+ use_app_name = get_app_name (mod_data = mod_data , app_name = app_name )
148131
149132 import_string = f"{ mod_data .module_import_str } :{ use_app_name } "
150133
151134 return ImportData (
152- app_name = use_app_name ,
153- module_data = mod_data ,
154- import_string = import_string ,
155- openapi_url = openapi_url ,
156- docs_url = docs_url ,
157- redoc_url = redoc_url ,
135+ app_name = use_app_name , module_data = mod_data , import_string = import_string
158136 )
159137
160138
@@ -170,21 +148,35 @@ def get_import_data_from_import_string(import_string: str) -> ImportData:
170148
171149 sys .path .insert (0 , str (here ))
172150
173- module_data = ModuleData (
174- module_import_str = module_str ,
175- extra_sys_path = here ,
176- module_paths = [],
177- )
178-
179- _ , openapi_url , docs_url , redoc_url = get_app_infos (
180- mod_data = module_data , app_name = app_name
181- )
182-
183151 return ImportData (
184152 app_name = app_name ,
185- module_data = module_data ,
153+ module_data = ModuleData (
154+ module_import_str = module_str ,
155+ extra_sys_path = here ,
156+ module_paths = [],
157+ ),
186158 import_string = import_string ,
187- openapi_url = openapi_url ,
188- docs_url = docs_url ,
189- redoc_url = redoc_url ,
159+ )
160+
161+
162+ @dataclass
163+ class DocsURLs :
164+ openapi_url : str | None
165+ docs_url : str | None
166+ redoc_url : str | None
167+
168+
169+ def get_docs_urls (import_data : ImportData ) -> DocsURLs :
170+ module = importlib .import_module (import_data .module_data .module_import_str )
171+ app_name = import_data .app_name
172+ app = getattr (module , app_name )
173+
174+ # Just for type checking, it's already checked in get_app_name
175+ assert FastAPI is not None
176+ assert isinstance (app , FastAPI )
177+
178+ return DocsURLs (
179+ openapi_url = app .openapi_url ,
180+ docs_url = app .docs_url ,
181+ redoc_url = app .redoc_url ,
190182 )
0 commit comments