Last active
August 8, 2022 06:16
-
-
Save Kludex/1ef4a782fd9294a088c98df4bf10954f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
from typing import Any | |
import anyio | |
import ijson | |
from anyio.streams.buffered import BufferedByteReceiveStream | |
from anyio.streams.memory import MemoryObjectReceiveStream | |
from fastapi import FastAPI, Request, UploadFile | |
app = FastAPI() | |
@app.post("/file") | |
async def from_file(file: UploadFile) -> Any: | |
async for item in ijson.items(file, "quiz.item"): | |
print(item) | |
@app.post("/json") | |
async def from_json(request: Request) -> Any: | |
file = anyio.wrap_file(io.BytesIO()) | |
async for chunk in request.stream(): | |
await file.write(chunk) | |
await file.seek(0) | |
async for item in ijson.items(file, "quiz.item"): | |
print(item) | |
BufferedByteReceiveStream.read = BufferedByteReceiveStream.receive | |
async def process_json(receive_stream: MemoryObjectReceiveStream) -> None: | |
buffered = BufferedByteReceiveStream(receive_stream) | |
async for item in ijson.items(buffered, "quiz.item"): | |
print(item) | |
@app.post("/json-stream") | |
async def from_json_stream(request: Request) -> Any: | |
send_stream, receive_stream = anyio.create_memory_object_stream() | |
async with anyio.create_task_group() as tg: | |
tg.start_soon(process_json, receive_stream) | |
async with send_stream: | |
async for chunk in request.stream(): | |
await send_stream.send(chunk) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uvicorn==0.18.2 | |
fastapi==0.79.0 | |
ijson==3.1.4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"quiz": [ | |
{ | |
"q1": { | |
"question": "Which one is correct team name in NBA?", | |
"options": [ | |
"New York Bulls", | |
"Los Angeles Kings", | |
"Golden State Warriros", | |
"Huston Rocket" | |
], | |
"answer": "Huston Rocket" | |
} | |
}, | |
{ | |
"q1": { | |
"question": "5 + 7 = ?", | |
"options": ["10", "11", "12", "13"], | |
"answer": "12" | |
}, | |
"q2": { | |
"question": "12 - 8 = ?", | |
"options": ["1", "2", "3", "4"], | |
"answer": "4" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using HTTPie
http -f POST :8000 [email protected]
.