/ justapi / crates / justapi-py / test_layered_di.py
swadhinbiswas test(py): add comprehensive test suite for Python bindings · 7/8/2026
f693697
37 lines · 1.1 KB
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
import asyncio
import time
from justapi import JustAPIApp, Depends
import urllib.request
import threading

def common_auth():
    print("Executing common_auth (should only run once if cached)")
    return {"user_id": 123}

async def fetch_db(auth=Depends(common_auth, use_cache=True)):
    print("Executing fetch_db")
    return {"db_conn": True, "user_id": auth["user_id"]}

# App level dependency
app = JustAPIApp(dependencies=[Depends(common_auth)])

# Route level dependency and Handler level dependency
@app.get("/di", dependencies=[Depends(fetch_db, use_cache=True)])
def di_route(request, auth=Depends(common_auth, use_cache=True), db=Depends(fetch_db, use_cache=True)):
    return {"message": "ok", "auth": auth, "db": db}

def start_server():
    app.run("127.0.0.1:8112")

if __name__ == "__main__":
    t = threading.Thread(target=start_server, daemon=True)
    t.start()
    time.sleep(1) # wait for server
    
    req = urllib.request.Request("http://127.0.0.1:8112/di")
    try:
        res = urllib.request.urlopen(req)
        print("Response:", res.read().decode())
    except Exception as e:
        print("Error:", e)