|
| 1 | +from typing import Dict, Any, List |
| 2 | +from fastapi import FastAPI |
| 3 | +from osbot_utils.type_safe.Type_Safe import Type_Safe |
| 4 | + |
| 5 | + |
| 6 | +class Fast_API__Routes__Paths(Type_Safe): |
| 7 | + app: FastAPI |
| 8 | + |
| 9 | + def routes_tree(self) -> Dict[str, Any]: # Returns a hierarchical view of all routes and mounts |
| 10 | + routes_data = { 'title' : self.app.title , |
| 11 | + 'version' : self.app.version , |
| 12 | + 'description': self.app.description , |
| 13 | + 'routes' : [] , |
| 14 | + 'mounts' : [] } |
| 15 | + |
| 16 | + for route in self.app.routes: # Process regular routes |
| 17 | + if hasattr(route, 'path') and hasattr(route, 'methods'): |
| 18 | + route_info = { |
| 19 | + 'path': route.path, |
| 20 | + 'methods': list(route.methods) if route.methods else [], |
| 21 | + 'name': route.name, |
| 22 | + #'tags': getattr(route, 'tags', []) |
| 23 | + } |
| 24 | + routes_data['routes'].append(route_info) |
| 25 | + |
| 26 | + |
| 27 | + elif hasattr(route, 'path') and hasattr(route, 'app'): # Handle mounts |
| 28 | + mount_info = { |
| 29 | + 'path': route.path, |
| 30 | + 'type': type(route.app).__name__, |
| 31 | + 'routes': self.get_mount_routes(route.app) |
| 32 | + } |
| 33 | + routes_data['mounts'].append(mount_info) |
| 34 | + |
| 35 | + return routes_data |
| 36 | + |
| 37 | + def get_mount_routes(self, mounted_app) -> List[Dict]: # Extract routes from a mounted application |
| 38 | + routes = [] |
| 39 | + if hasattr(mounted_app, 'routes'): |
| 40 | + for route in mounted_app.routes: |
| 41 | + if hasattr(route, 'path'): |
| 42 | + routes.append({ |
| 43 | + 'path': route.path, |
| 44 | + 'methods': list(getattr(route, 'methods', [])) |
| 45 | + }) |
| 46 | + return routes |
| 47 | + |
| 48 | + def routes_html(self) -> str: # Returns an HTML page with all routes |
| 49 | + routes_data = self.routes_tree() |
| 50 | + |
| 51 | + html_content = f""" |
| 52 | + <!DOCTYPE html> |
| 53 | + <html> |
| 54 | + <head> |
| 55 | + <title>{routes_data['title']} - Routes Overview</title> |
| 56 | + <style> |
| 57 | + body {{ font-family: Arial, sans-serif; margin: 20px; }} |
| 58 | + h1 {{ color: #333; }} |
| 59 | + .route {{ margin: 10px 0; padding: 10px; border-left: 3px solid #4CAF50; }} |
| 60 | + .mount {{ margin: 10px 0; padding: 10px; border-left: 3px solid #2196F3; }} |
| 61 | + .method {{ |
| 62 | + display: inline-block; |
| 63 | + padding: 2px 8px; |
| 64 | + margin-right: 5px; |
| 65 | + border-radius: 3px; |
| 66 | + font-size: 12px; |
| 67 | + font-weight: bold; |
| 68 | + }} |
| 69 | + .GET {{ background: #61affe; color: white; }} |
| 70 | + .POST {{ background: #49cc90; color: white; }} |
| 71 | + .PUT {{ background: #fca130; color: white; }} |
| 72 | + .DELETE {{ background: #f93e3e; color: white; }} |
| 73 | + .path {{ font-family: monospace; }} |
| 74 | + </style> |
| 75 | + </head> |
| 76 | + <body> |
| 77 | + <h1>{routes_data['title']} API Routes</h1> |
| 78 | + <p>Version: {routes_data['version']}</p> |
| 79 | + |
| 80 | + <h2>Routes</h2> |
| 81 | + {"".join(self.format_route_html(r) for r in routes_data['routes'])} |
| 82 | + |
| 83 | + <h2>Mounted Applications</h2> |
| 84 | + {"".join(self.format_mount_html(m) for m in routes_data['mounts'])} |
| 85 | + </body> |
| 86 | + </html> |
| 87 | + """ |
| 88 | + return html_content |
| 89 | + |
| 90 | + def format_route_html(self, route): |
| 91 | + methods_html = "".join(f'<span class="method {m}">{m}</span>' for m in route['methods']) |
| 92 | + return f'<div class="route">{methods_html} <span class="path">{route["path"]}</span></div>' |
| 93 | + |
| 94 | + def format_mount_html(self, mount): |
| 95 | + return f'<div class="mount"><strong>{mount["path"]}</strong> ({mount["type"]})</div>' |
| 96 | + |
0 commit comments