-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from fastapi import FastAPI, Response, HTTPException, File, UploadFile | ||
from fastapi.responses import PlainTextResponse, HTMLResponse, StreamingResponse | ||
from typing import Generator, Dict, Any | ||
from pydantic import BaseModel | ||
import json | ||
|
||
app = FastAPI(title="HTTP Test Server") | ||
|
||
|
||
@app.get("/") | ||
async def root() -> Dict[str, str]: | ||
return {"message": "Welcome to the HTTP test server"} | ||
|
||
|
||
# Basic REST endpoints | ||
class Item(BaseModel): | ||
item_id: int | ||
name: str | ||
|
||
|
||
class ItemResponse(BaseModel): | ||
item: Item | ||
message: str | ||
|
||
|
||
@app.get("/items/{item_id}") | ||
async def get_item(item_id: int) -> Item: | ||
return Item.model_validate({"item_id": item_id, "name": f"Test Item {item_id}"}) | ||
|
||
|
||
@app.post("/items") | ||
async def create_item(item: Item) -> ItemResponse: | ||
return ItemResponse.model_validate({"message": "Item created", "item": item}) | ||
|
||
|
||
@app.put("/items/{item_id}") | ||
async def update_item(item_id: int, item: Item) -> ItemResponse: | ||
return ItemResponse.model_validate( | ||
{"message": f"Item {item_id} updated", "item": item} | ||
) | ||
|
||
|
||
@app.delete("/items/{item_id}") | ||
async def delete_item(item_id: int) -> Dict[str, str]: | ||
return {"message": f"Item {item_id} deleted"} | ||
|
||
|
||
# Different content types | ||
@app.get("/text", response_class=PlainTextResponse) | ||
async def get_text() -> str: | ||
return "This is a plain text response" | ||
|
||
|
||
@app.get("/html", response_class=HTMLResponse) | ||
async def get_html() -> str: | ||
return """ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Test HTML</title> | ||
</head> | ||
<body> | ||
<h1>HTML Response</h1> | ||
<p>This is a test HTML response</p> | ||
</body> | ||
</html> | ||
""" | ||
|
||
|
||
@app.get("/binary") | ||
async def get_binary() -> Response: | ||
content = b"Binary data response" | ||
return Response(content=content, media_type="application/octet-stream") | ||
|
||
|
||
# Status codes | ||
@app.get("/error/404") | ||
async def error_404() -> HTTPException: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
|
||
|
||
@app.get("/error/500") | ||
async def error_500() -> HTTPException: | ||
raise HTTPException(status_code=500, detail="Internal server error") | ||
|
||
|
||
class FileResponse(BaseModel): | ||
filename: str | ||
content_type: str | ||
size: int | ||
|
||
|
||
# File upload | ||
@app.post("/upload") | ||
async def upload_file(file: UploadFile = File(...)) -> FileResponse: | ||
contents = await file.read() | ||
return FileResponse.model_validate( | ||
{ | ||
"filename": file.filename, | ||
"content_type": file.content_type, | ||
"size": len(contents), | ||
} | ||
) | ||
|
||
|
||
# Streaming response | ||
@app.get("/stream") | ||
async def stream_data() -> StreamingResponse: | ||
def generate() -> Generator[str, None, None]: | ||
for i in range(5): | ||
yield json.dumps({"chunk": i}) + "\n" | ||
|
||
return StreamingResponse(generate(), media_type="application/x-ndjson") | ||
|
||
|
||
# Echo endpoint | ||
@app.post("/echo") | ||
async def echo(request_data: Dict[str, Any]) -> Dict[str, Any]: | ||
return request_data | ||
|
||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
|
||
uvicorn.run(app, host="0.0.0.0", port=8000) |
Empty file.