From a79c69843e99b4e3166ce9cce6166ccdc0fd42ad Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sat, 22 Mar 2025 01:12:17 +0200 Subject: [PATCH 01/11] docs_src/middleware/tutorial001.py: source code updated --- docs_src/middleware/tutorial001.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs_src/middleware/tutorial001.py b/docs_src/middleware/tutorial001.py index e65a7dade..1266772c5 100644 --- a/docs_src/middleware/tutorial001.py +++ b/docs_src/middleware/tutorial001.py @@ -12,3 +12,8 @@ async def add_process_time_header(request: Request, call_next): process_time = time.perf_counter() - start_time response.headers["X-Process-Time"] = str(process_time) return response + + +@app.get("/") +def hello(): + return {"hello": "world"} From dcda9412560ced5a227d95d7f5bc2395260ffad3 Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sat, 22 Mar 2025 01:13:47 +0200 Subject: [PATCH 02/11] tests/test_tutorial/test_middleware/test_tutorial001.py: new test added --- tests/test_tutorial/test_middleware/__init__.py | 0 .../test_middleware/test_tutorial001.py | 12 ++++++++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/test_tutorial/test_middleware/__init__.py create mode 100644 tests/test_tutorial/test_middleware/test_tutorial001.py diff --git a/tests/test_tutorial/test_middleware/__init__.py b/tests/test_tutorial/test_middleware/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_tutorial/test_middleware/test_tutorial001.py b/tests/test_tutorial/test_middleware/test_tutorial001.py new file mode 100644 index 000000000..8131f182c --- /dev/null +++ b/tests/test_tutorial/test_middleware/test_tutorial001.py @@ -0,0 +1,12 @@ +from docs_src.middleware.tutorial001 import app +from fastapi.testclient import TestClient + + +def test_add_process_time_header_middleware(): + client = TestClient(app) + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"hello": "world"} + assert "X-Process-Time" in response.headers + assert len(response.headers["X-Process-Time"]) > 0 + From a41c4d7940de976f6d849cdc7bfcf37137590815 Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sat, 22 Mar 2025 01:19:11 +0200 Subject: [PATCH 03/11] docs_src/middleware/tutorial002.py: new tutorial source code added --- docs_src/middleware/tutorial002.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 docs_src/middleware/tutorial002.py diff --git a/docs_src/middleware/tutorial002.py b/docs_src/middleware/tutorial002.py new file mode 100644 index 000000000..8c0526cc1 --- /dev/null +++ b/docs_src/middleware/tutorial002.py @@ -0,0 +1,22 @@ +import time + +from fastapi import FastAPI, Request, Response +from starlette.middleware.base import BaseHTTPMiddleware + + +class ProcessTimeHeaderMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + start_time = time.perf_counter() + response: Response = await call_next(request) + process_time = time.perf_counter() - start_time + response.headers["X-Process-Time"] = str(process_time) + return response + + +app = FastAPI() +app.add_middleware(ProcessTimeHeaderMiddleware) + + +@app.get("/") +def hello(): + return {"hello": "world"} \ No newline at end of file From 378f7e431c429450699cc6cb4223676fdc717feb Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sat, 22 Mar 2025 03:12:13 +0200 Subject: [PATCH 04/11] tests/test_tutorial/test_middleware/test_tutorial002.py: new test added --- .../test_tutorial/test_middleware/test_tutorial002.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/test_tutorial/test_middleware/test_tutorial002.py diff --git a/tests/test_tutorial/test_middleware/test_tutorial002.py b/tests/test_tutorial/test_middleware/test_tutorial002.py new file mode 100644 index 000000000..6d8b975f0 --- /dev/null +++ b/tests/test_tutorial/test_middleware/test_tutorial002.py @@ -0,0 +1,11 @@ +from docs_src.middleware.tutorial002 import app +from fastapi.testclient import TestClient + + +def test_add_process_time_header_middleware(): + client = TestClient(app) + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"hello": "world"} + assert "X-Process-Time" in response.headers + assert len(response.headers["X-Process-Time"]) > 0 From 3b20474efd13e9a198edfda939b2fb832650d048 Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sat, 22 Mar 2025 03:19:44 +0200 Subject: [PATCH 05/11] docs/en/docs/tutorial/middleware.md: new section added: Class-Based Middleware --- docs/en/docs/tutorial/middleware.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/en/docs/tutorial/middleware.md b/docs/en/docs/tutorial/middleware.md index 4f7980165..1b7ec5805 100644 --- a/docs/en/docs/tutorial/middleware.md +++ b/docs/en/docs/tutorial/middleware.md @@ -65,6 +65,28 @@ Here we use Date: Sat, 22 Mar 2025 10:41:06 +0000 Subject: [PATCH 06/11] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/tutorial/middleware.md | 4 ++-- docs_src/middleware/tutorial002.py | 2 +- tests/test_tutorial/test_middleware/test_tutorial001.py | 4 ++-- tests/test_tutorial/test_middleware/test_tutorial002.py | 3 ++- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/en/docs/tutorial/middleware.md b/docs/en/docs/tutorial/middleware.md index 1b7ec5805..64125dc74 100644 --- a/docs/en/docs/tutorial/middleware.md +++ b/docs/en/docs/tutorial/middleware.md @@ -67,7 +67,7 @@ Here we use 0 - diff --git a/tests/test_tutorial/test_middleware/test_tutorial002.py b/tests/test_tutorial/test_middleware/test_tutorial002.py index 6d8b975f0..3eaa8049c 100644 --- a/tests/test_tutorial/test_middleware/test_tutorial002.py +++ b/tests/test_tutorial/test_middleware/test_tutorial002.py @@ -1,6 +1,7 @@ -from docs_src.middleware.tutorial002 import app from fastapi.testclient import TestClient +from docs_src.middleware.tutorial002 import app + def test_add_process_time_header_middleware(): client = TestClient(app) From 96dc79e84dd5fd999fc4bf9a78c9e8ec3e469cfe Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sat, 22 Mar 2025 13:25:46 +0200 Subject: [PATCH 07/11] docs/tutorial/middleware.md: new section added: HTTP Middleware --- docs/en/docs/tutorial/middleware.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/en/docs/tutorial/middleware.md b/docs/en/docs/tutorial/middleware.md index 64125dc74..2124954d9 100644 --- a/docs/en/docs/tutorial/middleware.md +++ b/docs/en/docs/tutorial/middleware.md @@ -65,11 +65,22 @@ Here we use 0 + + # request/response process time + process_time = response.headers["X-Process-Time"] + assert float(process_time) > 0 + diff --git a/tests/test_tutorial/test_middleware/test_tutorial002.py b/tests/test_tutorial/test_middleware/test_tutorial002.py index 3eaa8049c..43b2c1121 100644 --- a/tests/test_tutorial/test_middleware/test_tutorial002.py +++ b/tests/test_tutorial/test_middleware/test_tutorial002.py @@ -10,3 +10,6 @@ def test_add_process_time_header_middleware(): assert response.json() == {"hello": "world"} assert "X-Process-Time" in response.headers assert len(response.headers["X-Process-Time"]) > 0 + # request/response process time + process_time = response.headers["X-Process-Time"] + assert float(process_time) > 0 From 9d0442f2bd96ae614660e2ce0f3d45f3b34b61d0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Mar 2025 11:34:39 +0000 Subject: [PATCH 09/11] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20f?= =?UTF-8?q?ormat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/docs/tutorial/middleware.md | 4 ++-- tests/test_tutorial/test_middleware/test_tutorial001.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/en/docs/tutorial/middleware.md b/docs/en/docs/tutorial/middleware.md index 2124954d9..5f67eaf8c 100644 --- a/docs/en/docs/tutorial/middleware.md +++ b/docs/en/docs/tutorial/middleware.md @@ -67,9 +67,9 @@ Here we use 0 - From f8e9d690f9960497dc43edb756252285d23f1a1d Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sat, 22 Mar 2025 13:36:22 +0200 Subject: [PATCH 10/11] tests/test_tutorial/test_middleware/test_tutorial001.py: fixing a typo --- tests/test_tutorial/test_middleware/test_tutorial001.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_tutorial/test_middleware/test_tutorial001.py b/tests/test_tutorial/test_middleware/test_tutorial001.py index 8c60a02c3..c4e561fa4 100644 --- a/tests/test_tutorial/test_middleware/test_tutorial001.py +++ b/tests/test_tutorial/test_middleware/test_tutorial001.py @@ -10,7 +10,6 @@ def test_add_process_time_header_middleware(): assert response.json() == {"hello": "world"} assert "X-Process-Time" in response.headers assert len(response.headers["X-Process-Time"]) > 0 - # request/response process time process_time = response.headers["X-Process-Time"] assert float(process_time) > 0 From b8ba81b698cf782752f16ccebbc388e53baca387 Mon Sep 17 00:00:00 2001 From: alv2017 Date: Sun, 23 Mar 2025 00:36:05 +0200 Subject: [PATCH 11/11] tutorial/middleware.md: updates and corrections added. --- docs/en/docs/tutorial/middleware.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/docs/en/docs/tutorial/middleware.md b/docs/en/docs/tutorial/middleware.md index 5f67eaf8c..02daaaa64 100644 --- a/docs/en/docs/tutorial/middleware.md +++ b/docs/en/docs/tutorial/middleware.md @@ -2,7 +2,13 @@ You can add middleware to **FastAPI** applications. -A "middleware" is a function that works with every **request** before it is processed by any specific *path operation*. And also with every **response** before returning it. +In this tutorial we will discuss how to add a custom `http` middleware to your FastAPI applications. + +The `http` middleware is designed to work with HTTP protocol, and it is responsible for handling of HTTP requests and responses. + +## How `http` middleware works? + +The `http` middleware works with every **request** before it is processed by any specific *path operation*. And also with every **response** before returning it. * It takes each **request** that comes to your application. * It can then do something to that **request** or run any needed code. @@ -65,16 +71,13 @@ Here we use