-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (49 loc) · 1.71 KB
/
main.py
File metadata and controls
56 lines (49 loc) · 1.71 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
"""
main.py
This file contains the main FastAPI application for the To-Do list API.
"""
from fastapi import FastAPI, HTTPException, Depends
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from pymongo import MongoClient
from bson import ObjectId
from pydantic import BaseModel
from config import Settings
from models import Todo
from utils import get_db
app = FastAPI()
security = HTTPBasic()
def get_db_client():
"""
Function to get a connection to the MongoDB database.
"""
return MongoClient(Settings.mongodb_url)
@app.post("/todos/", response_model=Todo)
async def create_todo(todo: Todo, credentials: HTTPBasicCredentials = Depends(security)):
"""
Endpoint to create a new todo.
"""
if not authenticate(credentials):
raise HTTPException(status_code=401, detail="Invalid credentials")
db = get_db()
result = db.todos.insert_one(todo.dict())
todo.id = str(result.inserted_id)
return todo
@app.get("/todos/{todo_id}", response_model=Todo)
async def read_todo(todo_id: str, credentials: HTTPBasicCredentials = Depends(security)):
"""
Endpoint to retrieve a todo by ID.
"""
if not authenticate(credentials):
raise HTTPException(status_code=401, detail="Invalid credentials")
db = get_db()
todo = db.todos.find_one({"_id": ObjectId(todo_id)})
if todo:
return Todo(**todo)
raise HTTPException(status_code=404, detail="Todo not found")
def authenticate(credentials: HTTPBasicCredentials):
"""
Function to authenticate user credentials.
"""
correct_username = Settings.username
correct_password = Settings.password
return credentials.username == correct_username and credentials.password == correct_password