Skip to content

Commit

Permalink
Merge pull request #106 from index-py/develop
Browse files Browse the repository at this point in the history
Add framework baize
  • Loading branch information
klen committed Oct 21, 2021
2 parents f089d04 + 2bcab46 commit 36a67ea
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
77 changes: 77 additions & 0 deletions frameworks/baize/app.py
@@ -0,0 +1,77 @@
import time
from uuid import uuid4

from baize.asgi import (
Router,
Response,
HTMLResponse,
PlainTextResponse,
JSONResponse,
Request,
request_response,
HTTPException,
)

routes = []

# first add ten more routes to load routing system
# ------------------------------------------------
for n in range(5):
routes.append((f"/route-{n}", HTMLResponse("ok")))
routes.append((f"/route-dyn-{n}/{{part}}", HTMLResponse("ok")))


# then prepare endpoints for the benchmark
# ----------------------------------------
@request_response
async def html(request: Request) -> Response:
"""Return HTML content and a custom header."""
content = "<b>HTML OK</b>"
headers = {"x-time": f"{time.time()}"}
return HTMLResponse(content, headers=headers)


@request_response
async def upload(request: Request) -> Response:
"""Load multipart data and store it as a file."""
if request.method != "POST":
return Response(405)

try:
formdata = await request.form
except HTTPException as exc:
return Response(exc.status_code, exc.headers)

if "file" not in formdata:
return PlainTextResponse("ERROR", status_code=400)

filepath = f"/tmp/{uuid4().hex}"
await formdata["file"].asave(filepath)

return PlainTextResponse(filepath)


@request_response
async def api(request: Request) -> Response:
"""Check headers for authorization, load JSON/query data and return as JSON."""
if request.method != "PUT":
return Response(405)

if request.headers.get("authorization") is None:
return PlainTextResponse("ERROR", status_code=401)

return JSONResponse(
{
"params": request.path_params,
"query": dict(request.query_params),
"data": await request.json,
}
)


app = Router(
("/html", html),
("/upload", upload),
("/api/users/{user:int}/records/{record:int}", api),
*routes,
)
1 change: 1 addition & 0 deletions frameworks/baize/requirements.txt
@@ -0,0 +1 @@
baize==0.12
3 changes: 3 additions & 0 deletions frameworks/baize/start.sh
@@ -0,0 +1,3 @@
#!/bin/sh

/usr/local/bin/gunicorn -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8080 app:app
3 changes: 2 additions & 1 deletion frameworks/tests.py
Expand Up @@ -8,6 +8,7 @@
@pytest.fixture(scope='session', params=[
'quart', # has to be first
# 'aiohttp', # doesnt support ASGI
'baize',
'blacksheep',
'django',
'emmett',
Expand Down Expand Up @@ -39,7 +40,7 @@ async def test_upload(client):
assert res.status_code in {404, 405} # blacksheep returns 404 for "method not allowed"

res = await client.post("/upload")
assert res.status_code == 400
assert res.status_code in {400, 415} # baize returns 415 for empty content-type

res = await client.post("/upload", data={'file': open(__file__)})
assert res.status_code == 200
Expand Down

0 comments on commit 36a67ea

Please sign in to comment.