Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
211 lines (170 loc) · 9.18 KB
/
Copy pathapp.py
File metadata and controls
211 lines (170 loc) · 9.18 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright 2026 Cellar Project Authors. Licensed under Apache-2.0.
"""FastAPI order service hosted by Cellar's buffered Python HTTP adapter."""
import asyncio
import os
import sys
from typing import Annotated, Literal
from urllib.parse import unquote, urlsplit
import fastapi
from fastapi import Depends, FastAPI, Path, Request
from fastapi.responses import JSONResponse, Response
from pydantic import BaseModel, ConfigDict, Field
from cellar import Conflict, Context, Request as CellRequest, Response as CellResponse, SDKError
async def buffered_http(application, request, ctx, *, max_response_bytes=1024 * 1024):
"""Example-only ASGI HTTP transport. Cellar owns I/O and invocation lifetime.
Complete finite responses are buffered; no lifespan, WebSockets, socket
extensions or client-address forwarding. Cancellation propagates unchanged.
"""
url = urlsplit(request.url)
scope = {
"type": "http", "asgi": {"version": "3.0", "spec_version": "2.3"},
"http_version": "1.1", "method": request.method, "scheme": url.scheme,
"path": unquote(url.path), "raw_path": url.path.encode("ascii"),
"query_string": url.query.encode("ascii"), "root_path": "",
"headers": [(key.lower().encode("ascii"), value.encode("latin-1"))
for key, value in request.headers],
"server": (url.hostname, url.port or (443 if url.scheme == "https" else 80)),
"client": None, "state": {"cellar": ctx}, "extensions": {},
}
status, headers, body = None, [], bytearray()
received, complete = False, False
async def receive():
nonlocal received
if not received:
received = True
return {"type": "http.request", "body": request.body, "more_body": False}
# There is no socket receive loop here. Further receives wait for native
# invocation cancellation rather than fabricating a client disconnect.
await asyncio.Future()
async def send(message):
nonlocal status, headers, complete
if complete:
raise ValueError("ASGI response already complete")
if message["type"] == "http.response.start":
if status is not None or message.get("trailers", False):
raise ValueError("Unsupported ASGI response start")
status = message["status"]
if not isinstance(status, int) or not 200 <= status <= 599:
raise ValueError("Invalid ASGI response status")
raw = message.get("headers", [])
if len(raw) > 128 or sum(len(k) + len(v) for k, v in raw) > 32768:
raise ValueError("ASGI response headers exceed limit")
headers = [(key.decode("ascii"), value.decode("latin-1")) for key, value in raw]
elif message["type"] == "http.response.body" and status is not None:
chunk = message.get("body", b"")
if len(body) + len(chunk) > max_response_bytes:
raise ValueError("ASGI response exceeds byte limit")
body.extend(chunk)
complete = not message.get("more_body", False)
else:
raise ValueError("Unsupported ASGI HTTP event")
await application(scope, receive, send)
if not complete:
raise ValueError("ASGI application returned an incomplete response")
return CellResponse(bytes(body), status, headers)
app = FastAPI(title="Cellar Orders", version="1.0.0")
async def cellar_context(request: Request) -> Context:
return request.state.cellar
Cellar = Annotated[Context, Depends(cellar_context)]
OrderID = Annotated[str, Path(pattern=r"^[a-zA-Z0-9_-]{1,64}$")]
class Item(BaseModel):
model_config = ConfigDict(extra="forbid")
sku: str = Field(pattern=r"^[a-zA-Z0-9_-]{1,64}$")
quantity: int = Field(strict=True, ge=1, le=1000)
unit_price_cents: int = Field(strict=True, ge=1, le=1_000_000_000)
class CreateOrder(BaseModel):
model_config = ConfigDict(extra="forbid")
customer_id: str = Field(pattern=r"^[a-zA-Z0-9_-]{1,64}$")
items: list[Item] = Field(min_length=1, max_length=20)
class ChangeStatus(BaseModel):
model_config = ConfigDict(extra="forbid")
expected_version: int = Field(strict=True, ge=1)
status: Literal["confirmed", "cancelled"]
class OrderView(CreateOrder):
order_id: str
total_cents: int
status: Literal["pending", "confirmed", "cancelled"]
version: int
created_at_ms: int
@app.exception_handler(SDKError)
async def sdk_failure(request: Request, error: SDKError):
# No automatic mutation retries or claim that an unknown result rolled back.
return JSONResponse({"detail": "Cellar operation failed", "code": error.code,
"outcome": error.outcome}, status_code=503)
@app.get("/health")
async def health():
headers = {}
if instance := os.environ.get("CELLAR_SUPERVISOR_INSTANCE"):
headers["x-cellar-supervisor-instance"] = instance
return JSONResponse({"framework": "fastapi", "version": fastapi.__version__,
"free_threaded": "t" in sys.abiflags,
"gil_enabled": sys._is_gil_enabled()}, headers=headers)
async def forward(ctx, order_id, method, payload=None):
request = CellRequest("https://orders/" + order_id, method,
b"" if payload is None else CellResponse.json(payload).body)
if ctx.identity is None:
result = await ctx.namespace("ORDERS").get_by_name(order_id).fetch(request)
else:
if ctx.identity.class_name != "Order" or ctx.identity.name != order_id:
raise RuntimeError("Order route does not match its cell")
result = await Order.handle(request, ctx)
response = Response(result.body, status_code=result.status)
response.raw_headers = [(k.encode("ascii"), v.encode("latin-1")) for k, v in result.headers]
return response
@app.post("/orders/{order_id}", response_model=OrderView, status_code=201)
async def create_order(order_id: OrderID, order: CreateOrder, ctx: Cellar):
return await forward(ctx, order_id, "POST", order.model_dump())
@app.get("/orders/{order_id}", response_model=OrderView)
async def read_order(order_id: OrderID, ctx: Cellar):
return await forward(ctx, order_id, "GET")
@app.patch("/orders/{order_id}", response_model=OrderView)
async def change_status(order_id: OrderID, change: ChangeStatus, ctx: Cellar):
return await forward(ctx, order_id, "PATCH", change.model_dump())
async def fetch(request, ctx):
return await buffered_http(app, request, ctx)
def render(order, status=200):
response = CellResponse.json(order, status)
response.headers.append(("etag", '"' + str(order["version"]) + '"'))
return response
class Order:
async def fetch(self, request, ctx):
if request.path.startswith("/orders/"):
return await buffered_http(app, request, ctx)
return await self.handle(request, ctx)
@staticmethod
async def handle(request, ctx):
# The compiled host captures this invocation's state before Python
# dispatch. Checked writes still detect any intervening writer.
current = ctx.initial_storage["order"]
if request.method == "GET":
return render(current.value) if current.present else CellResponse.json({"detail": "Order not found"}, 404)
if request.method == "POST":
payload = CreateOrder.model_validate(await request.json()).model_dump()
receipt = await ctx.storage.read_json("creation")
if receipt.present:
if receipt.value["payload"] != payload:
return CellResponse.json({"detail": "Order ID already used for a different order"}, 409)
saved = receipt.value["response"]
return CellResponse(saved["body"], saved["status"], saved["headers"])
if current.present:
return CellResponse.json({"detail": "Order already exists"}, 409)
order = dict(payload, order_id=request.path.lstrip("/"), status="pending", version=1,
created_at_ms=await ctx.now_ms(),
total_cents=sum(item["quantity"] * item["unit_price_cents"] for item in payload["items"]))
response = render(order, 201)
puts = {"order": order, "creation": {"payload": payload, "response": {
"body": response.body.decode("ascii"), "status": response.status, "headers": response.headers}}}
checks = {"order": current, "creation": receipt}
elif request.method == "PATCH" and current.present:
change = ChangeStatus.model_validate(await request.json())
if current.value["version"] != change.expected_version or current.value["status"] != "pending":
return CellResponse.json({"detail": "Order version or status conflict"}, 409)
order = dict(current.value, status=change.status, version=change.expected_version + 1)
response, checks, puts = render(order), {"order": current}, {"order": order}
else:
return CellResponse.json({"detail": "Order not found"}, 404)
try:
await ctx.storage.apply(checks=checks, puts=puts)
except Conflict:
return CellResponse.json({"detail": "Concurrent update; read state and reconcile"}, 409)
return response