Skip to content

about middleware, 2 questions #6

@ockan

Description

@ockan

first, when code

from turboapi import TurboAPI

app=TurboAPI()

import time

@app.middleware("http")
async def add_process_time_header(request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    # response.headers["X-Process-Time"] = str(process_time)
    print(process_time)
    return response

@app.get("/")
def home():
    # Validate credentials
    return {"access_token": 'username', "token_type": "bearer"}

app.run()

it got an error

File "D:\py\turbo\work\main.py", line 7, in <module>
    @app.middleware("http")
     ^^^^^^^^^^^^^^
AttributeError: 'RustIntegratedTurboAPI' object has no attribute 'middleware'. Did you mean: 'add_middleware'?

then, modify code as:

from turboapi import TurboAPI

app=TurboAPI()

import time

async def add_process_time_header(request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    # response.headers["X-Process-Time"] = str(process_time)
    print(process_time)
    return response

app.add_middleware(add_process_time_header)

@app.get("/")
def home():
    # Validate credentials
    return {"access_token": 'username', "token_type": "bearer"}

app.run()

it got {"access_token": 'username', "token_type": "bearer"} in the bowser but nothing in console(normally print process_time) and the breakpoint in the function 'add_process_time_header(...)' was never reached.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions