fastapi-cbx 1.0.0__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastapi-cbx
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: Minimal class-based routing extension for FastAPI
5
5
  Author-email: HeHongye <18348574371@139.com>
6
6
  Project-URL: Homepage, https://github.com/HeHongyeFY/fastapi-cbx
@@ -22,10 +22,14 @@ One scenario, one route.
22
22
 
23
23
  A precise complement to the FastAPI ecosystem, unifying layered routing patterns and strict resource lifecycle management into a clean, production‑ready solution.
24
24
 
25
- Originally proposed and contributed to FastAPI as PR #15392.
25
+ Originally proposed and contributed to FastAPI as PR #15447.
26
26
 
27
27
  ![CI](https://github.com/HeHongyeFY/fastapi-cbx/actions/workflows/ci.yml/badge.svg)
28
28
  ![Codecov](https://codecov.io/github/HeHongyeFY/fastapi-cbx/graph/badge.svg)
29
+
30
+ 🔥 **Featured Case Study:**
31
+ > How we refactored vLLM’s API server and cut structural boilerplate by ~50%
32
+ > 👉 [Read: Refactoring vLLM API Architecture](https://github.com/HeHongyeFY/fastapi-cbx/wiki/refactoring-vllm-api-architecture)
29
33
  ---
30
34
 
31
35
  ## Overview
@@ -33,7 +37,8 @@ fastapi-cbx introduces three **orthogonal, scenario‑driven routing patterns**
33
37
 
34
38
  ## Features
35
39
  - Adds two scenario-driven class-based routing patterns to native FastAPI
36
- - Ultra-lightweight: ~110 lines of clean code
40
+ - HTTP and WebSocket endpoints bound by the same decorator style
41
+ - Ultra-lightweight: ~130 lines of clean code
37
42
  - 100% test coverage
38
43
  - No monkey patching, no metaprogramming, no hidden magic
39
44
  - Fully backward compatible, no breaking changes
@@ -42,11 +47,6 @@ fastapi-cbx introduces three **orthogonal, scenario‑driven routing patterns**
42
47
  - Predictable, transparent behavior
43
48
  - Minimal API surface, easy to maintain
44
49
 
45
- ## Installation
46
- ```bash
47
- pip install fastapi-cbx
48
- ```
49
-
50
50
  ## Quick Start (30 Seconds)
51
51
  fastapi-cbx provides CBR (Class-Based Route), an original, intuitive, high-performance class-based routing utility for FastAPI.
52
52
 
@@ -95,8 +95,8 @@ class MyCBR:
95
95
  return self.CBRModel(key=key, value=self.heavies.get(key, "One scenario, one route"))
96
96
 
97
97
 
98
- app = FastAPI()
99
98
  MyCBR(version="1.0.0")
99
+ app = FastAPI()
100
100
  app.include_router(MyCBR.router)
101
101
  ```
102
102
 
@@ -177,8 +177,8 @@ class MyCBV:
177
177
  cls.CBVModel.model_fields["value"].default = body.value
178
178
 
179
179
 
180
- app = FastAPI()
181
180
  MyCBV(version="1.0.0")
181
+ app = FastAPI()
182
182
  app.include_router(MyCBV.router)
183
183
  ```
184
184
 
@@ -188,7 +188,7 @@ import logging
188
188
  import asyncio
189
189
  import time
190
190
  from typing import Dict
191
- from fastapi import FastAPI, APIRouter, Response, status, Cookie, Depends, HTTPException
191
+ from fastapi import FastAPI, APIRouter, Response, status, Cookie, Depends, HTTPException, WebSocket
192
192
  from cbx import cbr
193
193
  from pydantic import BaseModel
194
194
 
@@ -246,9 +246,16 @@ class MyCBR:
246
246
  time.sleep(1)
247
247
  return cls.CBRModel(key="sync", value="ok")
248
248
 
249
+ @cbr.websocket("/heavies", name="websocket heavies") # WebSocket support
250
+ async def websocket_heavies(self, websocket: WebSocket) -> None:
251
+ await websocket.accept()
252
+ key = await websocket.receive_text()
253
+ await websocket.send_text(self.heavies.get(key, "One scenario, one route"))
254
+ await websocket.close()
255
+
249
256
 
250
- app = FastAPI()
251
257
  MyCBR(version="1.0.0")
258
+ app = FastAPI()
252
259
  app.include_router(MyCBR.router)
253
260
  ```
254
261
 
@@ -5,10 +5,14 @@ One scenario, one route.
5
5
 
6
6
  A precise complement to the FastAPI ecosystem, unifying layered routing patterns and strict resource lifecycle management into a clean, production‑ready solution.
7
7
 
8
- Originally proposed and contributed to FastAPI as PR #15392.
8
+ Originally proposed and contributed to FastAPI as PR #15447.
9
9
 
10
10
  ![CI](https://github.com/HeHongyeFY/fastapi-cbx/actions/workflows/ci.yml/badge.svg)
11
11
  ![Codecov](https://codecov.io/github/HeHongyeFY/fastapi-cbx/graph/badge.svg)
12
+
13
+ 🔥 **Featured Case Study:**
14
+ > How we refactored vLLM’s API server and cut structural boilerplate by ~50%
15
+ > 👉 [Read: Refactoring vLLM API Architecture](https://github.com/HeHongyeFY/fastapi-cbx/wiki/refactoring-vllm-api-architecture)
12
16
  ---
13
17
 
14
18
  ## Overview
@@ -16,7 +20,8 @@ fastapi-cbx introduces three **orthogonal, scenario‑driven routing patterns**
16
20
 
17
21
  ## Features
18
22
  - Adds two scenario-driven class-based routing patterns to native FastAPI
19
- - Ultra-lightweight: ~110 lines of clean code
23
+ - HTTP and WebSocket endpoints bound by the same decorator style
24
+ - Ultra-lightweight: ~130 lines of clean code
20
25
  - 100% test coverage
21
26
  - No monkey patching, no metaprogramming, no hidden magic
22
27
  - Fully backward compatible, no breaking changes
@@ -25,11 +30,6 @@ fastapi-cbx introduces three **orthogonal, scenario‑driven routing patterns**
25
30
  - Predictable, transparent behavior
26
31
  - Minimal API surface, easy to maintain
27
32
 
28
- ## Installation
29
- ```bash
30
- pip install fastapi-cbx
31
- ```
32
-
33
33
  ## Quick Start (30 Seconds)
34
34
  fastapi-cbx provides CBR (Class-Based Route), an original, intuitive, high-performance class-based routing utility for FastAPI.
35
35
 
@@ -78,8 +78,8 @@ class MyCBR:
78
78
  return self.CBRModel(key=key, value=self.heavies.get(key, "One scenario, one route"))
79
79
 
80
80
 
81
- app = FastAPI()
82
81
  MyCBR(version="1.0.0")
82
+ app = FastAPI()
83
83
  app.include_router(MyCBR.router)
84
84
  ```
85
85
 
@@ -160,8 +160,8 @@ class MyCBV:
160
160
  cls.CBVModel.model_fields["value"].default = body.value
161
161
 
162
162
 
163
- app = FastAPI()
164
163
  MyCBV(version="1.0.0")
164
+ app = FastAPI()
165
165
  app.include_router(MyCBV.router)
166
166
  ```
167
167
 
@@ -171,7 +171,7 @@ import logging
171
171
  import asyncio
172
172
  import time
173
173
  from typing import Dict
174
- from fastapi import FastAPI, APIRouter, Response, status, Cookie, Depends, HTTPException
174
+ from fastapi import FastAPI, APIRouter, Response, status, Cookie, Depends, HTTPException, WebSocket
175
175
  from cbx import cbr
176
176
  from pydantic import BaseModel
177
177
 
@@ -229,9 +229,16 @@ class MyCBR:
229
229
  time.sleep(1)
230
230
  return cls.CBRModel(key="sync", value="ok")
231
231
 
232
+ @cbr.websocket("/heavies", name="websocket heavies") # WebSocket support
233
+ async def websocket_heavies(self, websocket: WebSocket) -> None:
234
+ await websocket.accept()
235
+ key = await websocket.receive_text()
236
+ await websocket.send_text(self.heavies.get(key, "One scenario, one route"))
237
+ await websocket.close()
238
+
232
239
 
233
- app = FastAPI()
234
240
  MyCBR(version="1.0.0")
241
+ app = FastAPI()
235
242
  app.include_router(MyCBR.router)
236
243
  ```
237
244
 
@@ -1,8 +1,8 @@
1
1
  import inspect
2
2
  import logging
3
3
  from collections.abc import Callable
4
- from functools import partial, wraps
5
- from typing import Any, TypeVar, Generic
4
+ from functools import partial
5
+ from typing import Any, Generic, TypeVar
6
6
  from fastapi import APIRouter
7
7
 
8
8
 
@@ -10,6 +10,7 @@ T = TypeVar("T")
10
10
 
11
11
 
12
12
  class CBV(Generic[T]):
13
+
13
14
  def __init__(self, cls: type[T], router: APIRouter):
14
15
  self.logger = logging.getLogger(self.__class__.__name__)
15
16
  self.router = router
@@ -28,7 +29,6 @@ class CBV(Generic[T]):
28
29
  "trace": 200,
29
30
  "connect": 200,
30
31
  }.items():
31
-
32
32
  if hasattr(self.instance, name):
33
33
  method = getattr(self.instance, name)
34
34
  self.router.add_api_route(
@@ -64,12 +64,19 @@ class CBR(Generic[T]):
64
64
  ):
65
65
  if cbx_router := endpoint.__annotations__.get("cbx_router"):
66
66
  for router in cbx_router:
67
- self.router.add_api_route(
68
- path=router["path"],
69
- endpoint=endpoint,
70
- methods=[router["method"]],
71
- **router["kwargs"]
72
- )
67
+ if router["method"] == "WEBSOCKET":
68
+ self.router.add_api_websocket_route(
69
+ path=router["path"],
70
+ endpoint=endpoint,
71
+ **router["kwargs"],
72
+ )
73
+ else:
74
+ self.router.add_api_route(
75
+ path=router["path"],
76
+ endpoint=endpoint,
77
+ methods=[router["method"]],
78
+ **router["kwargs"],
79
+ )
73
80
  return self.instance
74
81
 
75
82
  def __dir__(self) -> list[str]:
@@ -80,16 +87,18 @@ class CBR(Generic[T]):
80
87
 
81
88
 
82
89
  class cbv(Generic[T]):
90
+
83
91
  def __init__(self, router: APIRouter):
84
92
  self.router = router
85
93
 
86
94
  def __call__(self, cls: type[T]) -> CBV[T]:
87
- return wraps(cls)(CBV(cls, self.router))
95
+ return CBV(cls, self.router)
88
96
 
89
97
 
90
98
  class cbr(Generic[T]):
91
99
 
92
100
  class method:
101
+
93
102
  def __init__(self, method: str, path: str, **kwargs: Any):
94
103
  self.method = method
95
104
  self.path = path
@@ -97,25 +106,13 @@ class cbr(Generic[T]):
97
106
 
98
107
  def __call__(self, endpoint: Callable[..., Any]) -> Callable[..., Any]:
99
108
  if "cbx_router" in endpoint.__annotations__:
100
- cbx_router: list = endpoint.__annotations__["cbx_router"]
101
- cbx_router.append(
102
- {
103
- "method": self.method,
104
- "path": self.path,
105
- "kwargs": self.kwargs
106
- }
109
+ endpoint.__annotations__["cbx_router"].append(
110
+ {"method": self.method, "path": self.path, "kwargs": self.kwargs}
107
111
  )
108
-
109
112
  else:
110
- cbx_router: list = [
111
- {
112
- "method": self.method,
113
- "path": self.path,
114
- "kwargs": self.kwargs
115
- }
116
- ]
117
113
  endpoint.__annotations__.setdefault(
118
- "cbx_router", cbx_router
114
+ "cbx_router",
115
+ [{"method": self.method, "path": self.path, "kwargs": self.kwargs}]
119
116
  )
120
117
  return endpoint
121
118
 
@@ -128,9 +125,10 @@ class cbr(Generic[T]):
128
125
  options = partial(method, "OPTIONS")
129
126
  trace = partial(method, "TRACE")
130
127
  connect = partial(method, "CONNECT")
128
+ websocket = partial(method, "WEBSOCKET")
131
129
 
132
130
  def __init__(self, router: APIRouter):
133
131
  self.router = router
134
132
 
135
133
  def __call__(self, cls: type[T]) -> CBR[T]:
136
- return wraps(cls)(CBR(cls, self.router))
134
+ return CBR(cls, self.router)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastapi-cbx
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: Minimal class-based routing extension for FastAPI
5
5
  Author-email: HeHongye <18348574371@139.com>
6
6
  Project-URL: Homepage, https://github.com/HeHongyeFY/fastapi-cbx
@@ -22,10 +22,14 @@ One scenario, one route.
22
22
 
23
23
  A precise complement to the FastAPI ecosystem, unifying layered routing patterns and strict resource lifecycle management into a clean, production‑ready solution.
24
24
 
25
- Originally proposed and contributed to FastAPI as PR #15392.
25
+ Originally proposed and contributed to FastAPI as PR #15447.
26
26
 
27
27
  ![CI](https://github.com/HeHongyeFY/fastapi-cbx/actions/workflows/ci.yml/badge.svg)
28
28
  ![Codecov](https://codecov.io/github/HeHongyeFY/fastapi-cbx/graph/badge.svg)
29
+
30
+ 🔥 **Featured Case Study:**
31
+ > How we refactored vLLM’s API server and cut structural boilerplate by ~50%
32
+ > 👉 [Read: Refactoring vLLM API Architecture](https://github.com/HeHongyeFY/fastapi-cbx/wiki/refactoring-vllm-api-architecture)
29
33
  ---
30
34
 
31
35
  ## Overview
@@ -33,7 +37,8 @@ fastapi-cbx introduces three **orthogonal, scenario‑driven routing patterns**
33
37
 
34
38
  ## Features
35
39
  - Adds two scenario-driven class-based routing patterns to native FastAPI
36
- - Ultra-lightweight: ~110 lines of clean code
40
+ - HTTP and WebSocket endpoints bound by the same decorator style
41
+ - Ultra-lightweight: ~130 lines of clean code
37
42
  - 100% test coverage
38
43
  - No monkey patching, no metaprogramming, no hidden magic
39
44
  - Fully backward compatible, no breaking changes
@@ -42,11 +47,6 @@ fastapi-cbx introduces three **orthogonal, scenario‑driven routing patterns**
42
47
  - Predictable, transparent behavior
43
48
  - Minimal API surface, easy to maintain
44
49
 
45
- ## Installation
46
- ```bash
47
- pip install fastapi-cbx
48
- ```
49
-
50
50
  ## Quick Start (30 Seconds)
51
51
  fastapi-cbx provides CBR (Class-Based Route), an original, intuitive, high-performance class-based routing utility for FastAPI.
52
52
 
@@ -95,8 +95,8 @@ class MyCBR:
95
95
  return self.CBRModel(key=key, value=self.heavies.get(key, "One scenario, one route"))
96
96
 
97
97
 
98
- app = FastAPI()
99
98
  MyCBR(version="1.0.0")
99
+ app = FastAPI()
100
100
  app.include_router(MyCBR.router)
101
101
  ```
102
102
 
@@ -177,8 +177,8 @@ class MyCBV:
177
177
  cls.CBVModel.model_fields["value"].default = body.value
178
178
 
179
179
 
180
- app = FastAPI()
181
180
  MyCBV(version="1.0.0")
181
+ app = FastAPI()
182
182
  app.include_router(MyCBV.router)
183
183
  ```
184
184
 
@@ -188,7 +188,7 @@ import logging
188
188
  import asyncio
189
189
  import time
190
190
  from typing import Dict
191
- from fastapi import FastAPI, APIRouter, Response, status, Cookie, Depends, HTTPException
191
+ from fastapi import FastAPI, APIRouter, Response, status, Cookie, Depends, HTTPException, WebSocket
192
192
  from cbx import cbr
193
193
  from pydantic import BaseModel
194
194
 
@@ -246,9 +246,16 @@ class MyCBR:
246
246
  time.sleep(1)
247
247
  return cls.CBRModel(key="sync", value="ok")
248
248
 
249
+ @cbr.websocket("/heavies", name="websocket heavies") # WebSocket support
250
+ async def websocket_heavies(self, websocket: WebSocket) -> None:
251
+ await websocket.accept()
252
+ key = await websocket.receive_text()
253
+ await websocket.send_text(self.heavies.get(key, "One scenario, one route"))
254
+ await websocket.close()
255
+
249
256
 
250
- app = FastAPI()
251
257
  MyCBR(version="1.0.0")
258
+ app = FastAPI()
252
259
  app.include_router(MyCBR.router)
253
260
  ```
254
261
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "fastapi-cbx"
7
- version = "1.0.0"
7
+ version = "1.0.2"
8
8
  description = "Minimal class-based routing extension for FastAPI"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
File without changes
File without changes