Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
67 lines (57 loc) · 2.25 KB
/
Copy pathexample.py
File metadata and controls
67 lines (57 loc) · 2.25 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
"""Example usage of fastapi-lifecycle package."""
if __name__ == "__main__":
from fastapi import FastAPI
import uvicorn
from fastapi_lifecycle import deprecated, sunset, versioned, setup_versioning
# Create FastAPI app
app = FastAPI(title="FastAPI Lifecycle Example")
# Setup lifecycle management (using middleware approach)
setup_versioning(app)
@app.get("/")
async def root():
"""Root endpoint with no lifecycle information."""
return {"message": "Hello World"}
@app.get("/deprecated-endpoint")
@deprecated(
{
"deprecated_at": "2024-01-15T00:00:00Z",
"sunset_at": "2024-06-15T00:00:00Z",
"migration_url": "https://api.example.com/docs/migration#users-v2",
"replacement": "GET /v2/users",
"reason": "Moving to v2 API with enhanced user profiles",
}
)
async def deprecated_endpoint():
"""Example of a deprecated endpoint."""
return {"message": "This endpoint is deprecated"}
@app.get("/sunset-endpoint")
@sunset(
{
"sunset_at": "2024-06-15T00:00:00Z",
"migration_url": "https://api.example.com/docs/migration",
"replacement": "GET /v2/endpoint",
"reason": "Consolidating endpoints for better performance",
}
)
async def sunset_endpoint():
"""Example of an endpoint with sunset date."""
return {"message": "This endpoint will be removed"}
@app.get("/versioned-endpoint")
@versioned(
{
"version": "1.0",
"deprecated_at": "2024-01-15T00:00:00Z",
"sunset_at": "2024-06-15T00:00:00Z",
"migration_url": "https://api.example.com/docs/migration",
"replacement": "GET /v2/endpoint",
"reason": "Enhanced functionality in v2",
}
)
async def versioned_endpoint():
"""Example of a comprehensively versioned endpoint."""
return {"message": "This is a versioned endpoint", "version": "1.0"}
print("Starting server...")
print("Test endpoints:")
print(" curl -I http://localhost:8000/deprecated-endpoint")
print(" curl -I http://localhost:8000/versioned-endpoint")
uvicorn.run(app, host="0.0.0.0", port=8000)