-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast-api.py
More file actions
71 lines (58 loc) · 2.89 KB
/
fast-api.py
File metadata and controls
71 lines (58 loc) · 2.89 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
from fastapi import FastAPI, Query, Form
from fastapi.responses import JSONResponse
from pymongo import MongoClient
import config
import certifi
app = FastAPI()
client = MongoClient(config.MONGO_URI, tlsCAFile=certifi.where())
db = client["sample_guides"]
col = db["planets"]
@app.get('/')
async def home():
return JSONResponse(content={"message": "Welcome to the FastAPI Planets API!"})
@app.get('/planets_list')
async def get_planets():
planets = [i['name'] for i in col.find({}, {'_id': 0})]
return JSONResponse(content=planets, status_code=200)
@app.get('/planet_details')
async def get_planet_by_name(name: str = Query(..., description="The name of the planet to retrieve")):
planet = col.find_one({"name": name}, {'_id': False})
if not planet:
return JSONResponse(content={"error": "Planet not found"}, status_code=404)
return JSONResponse(content=planet, status_code=200)
@app.post('/create_planet')
async def create_planet(name: str = Form(..., description="The name of the planet to create")):
if not name:
return JSONResponse(content={"error": "Planet name is required"}, status_code=400)
new_planet = {"name": name}
col.insert_one(new_planet)
return JSONResponse(content={"message": "Planet created successfully"}, status_code=201)
@app.put('/update_planet')
async def update_planet(name: str = Form(..., description="The name of the planet to update")):
if not name:
return JSONResponse(content={"error": "Planet name is required"}, status_code=400)
# Replace the following placeholder with your actual update logic
# Example:
# col.update_one({"name": name}, {"$set": {"population": 1000000}})
updated_planet = {"name": name, "message": "Planet updated successfully"}
return JSONResponse(content=updated_planet, status_code=200)
@app.patch('/partially_update_planet')
async def partially_update_planet(name: str = Form(..., description="The name of the planet to partially update")):
if not name:
return JSONResponse(content={"error": "Planet name is required"}, status_code=400)
# Replace the following placeholder with your actual partial update logic
# Example:
# col.update_one({"name": name}, {"$set": {"population": 1000000}})
partially_updated_planet = {"name": name, "message": "Planet partially updated successfully"}
return JSONResponse(content=partially_updated_planet, status_code=200)
@app.delete('/delete_planet')
async def delete_planet(name: str = Query(..., description="The name of the planet to delete")):
if not name:
return JSONResponse(content={"error": "Planet name is required"}, status_code=400)
# Replace the following placeholder with your actual delete logic
# Example:
# col.delete_one({"name": name})
return JSONResponse(content={"message": "Planet deleted successfully"}, status_code=200)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app)