| 1 |  |  | from contextlib import contextmanager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from typing import Iterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from fastapi import FastAPI, Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from lagom import Container, dependency_definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from lagom.integrations.fast_api import FastApiIntegration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Inner: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     def __init__(self, msg=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         self.msg = msg | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class RequestInjectedSingleton: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     def __init__(self, request: Request) -> None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         self.request = request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Outer: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     def __init__(self, inner: Inner): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         self.inner = inner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class ContextLoaded: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     cleaned_up = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     def __str__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return f"{self.cleaned_up}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class UnusedDepOne: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class UnusedDepTwo: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | app = FastAPI() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | container = Container() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | deps = FastApiIntegration( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     container, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     request_singletons=[UnusedDepOne, Inner, RequestInjectedSingleton, UnusedDepTwo], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     request_context_singletons=[ContextLoaded], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | @dependency_definition(container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | @contextmanager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | def _load_then_clean() -> Iterator[ContextLoaded]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         yield ContextLoaded() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     finally: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ContextLoaded.cleaned_up = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | @app.get("/") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | async def read_main(outer_one=deps.depends(Outer), outer_two=deps.depends(Outer)): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     return {"outer_one": hash(outer_one.inner), "outer_two": hash(outer_two.inner)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | @app.get("/inner") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | async def another_route(dep_one=deps.depends(Inner)): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     return {"data": dep_one.msg} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | @app.get("/request_injected_request_singleton") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | async def request_injected_request_singleton( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     dep_one=deps.depends(RequestInjectedSingleton), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     return {"data": dep_one.request.url.path} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  | @app.get("/with_some_context") | 
            
                                                        
            
                                    
            
            
                | 76 |  |  | async def a_route_with_context(dep_one=deps.depends(ContextLoaded)): | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |     return {"cleaned_up": str(dep_one)} | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |