44import webbrowser
55import uvicorn
66import threading
7- from typing import Union , Optional
87
9- from fastapi import FastAPI , Request , Query , HTTPException
8+ from fastapi import FastAPI , Request , HTTPException
109from fastapi .staticfiles import StaticFiles
11- from fastapi .responses import JSONResponse , RedirectResponse
12-
13- from uiviewer ._device import (
14- list_serials ,
15- init_device ,
16- cached_devices ,
17- AndroidDevice ,
18- IosDevice ,
19- HarmonyDevice
20- )
10+ from fastapi .responses import JSONResponse
11+
12+ from uiviewer .routers import api
2113from uiviewer ._models import ApiResponse
2214
2315
2921
3022app .mount ("/static" , StaticFiles (directory = static_dir ), name = "static" )
3123
24+ app .include_router (api .router )
25+
3226
3327@app .exception_handler (Exception )
3428def global_exception_handler (request : Request , exc : Exception ):
@@ -50,42 +44,6 @@ def open_browser(port):
5044 webbrowser .open_new (f"http://127.0.0.1:{ port } " )
5145
5246
53- @app .get ("/" )
54- def root ():
55- return RedirectResponse (url = "/static/index.html" )
56-
57-
58- @app .get ("/health" )
59- def health ():
60- return "ok"
61-
62-
63- @app .get ("/{platform}/serials" , response_model = ApiResponse )
64- def get_serials (platform : str ):
65- serials = list_serials (platform )
66- return ApiResponse .doSuccess (serials )
67-
68-
69- @app .post ("/{platform}/{serial}/connect" , response_model = ApiResponse )
70- def connect (platform : str , serial : str , wdaUrl : Optional [str ] = Query (None ), maxDepth : Optional [int ] = Query (None )):
71- ret = init_device (platform , serial , wdaUrl , maxDepth )
72- return ApiResponse .doSuccess (ret )
73-
74-
75- @app .get ("/{platform}/{serial}/screenshot" , response_model = ApiResponse )
76- def screenshot (platform : str , serial : str ):
77- device : Union [AndroidDevice , IosDevice , HarmonyDevice ] = cached_devices .get ((platform , serial ))
78- data = device .take_screenshot ()
79- return ApiResponse .doSuccess (data )
80-
81-
82- @app .get ("/{platform}/{serial}/hierarchy" , response_model = ApiResponse )
83- def dump_hierarchy (platform : str , serial : str ):
84- device : Union [AndroidDevice , IosDevice , HarmonyDevice ] = cached_devices .get ((platform , serial ))
85- data = device .dump_hierarchy ()
86- return ApiResponse .doSuccess (data )
87-
88-
8947def run (port = 8000 ):
9048 timer = threading .Timer (1.0 , open_browser , args = [port ])
9149 timer .daemon = True
0 commit comments