-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickapi.py
More file actions
55 lines (47 loc) · 1.71 KB
/
quickapi.py
File metadata and controls
55 lines (47 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
from fastapi import FastAPI
from app.logging_config import setup_logging
from app.data_model import UserColorEntry
# Setup logging
logger = setup_logging()
# Initialize logger
# logger = get_logger("myapp")
logger.info("Logger initialized successfully")
# Fast API app initialization
app = FastAPI(
title="Favorite Colors API",
description="Submit your name and favorite color",
version="1.0.0"
)
# In-memory database (for demonstration purposes)
user_colour = []
@app.get("/")
async def root():
"""Default end point.Serves welcome message for the API"""
logger.info("Root endpoint accessed")
logger.info("Root endpoint was accessed!")
return {"message": "Hello World"}
@app.get("/healthcheck")
async def healthcheck():
"""
Healthcheck endpoint to verify if the service is running.
"""
logger.info("Healthcheck endpoint accessed")
return {"status": "ok"}
# Create an user_colour mapping
@app.post("/usercolour/", response_model=UserColorEntry)
async def create_item(item: UserColorEntry):
"""Create an item with a username and users favourite colour and return it."""
user_colour.append(item)
print(user_colour)
# Sanitize log message to prevent log injection
sanitized_username = item.name.replace('\r\n', '').replace('\n', '')
sanitized_color = item.favorite_color.replace('\r\n', '').replace('\n', '')
logger.info("New user-color entry added: name=%s, favorite_colour=%s", sanitized_username, sanitized_color)
return item
# List all user_colour mappings
@app.get("/allusercolour/")
async def get_all_user_colour():
"""Get all user_colour mappings."""
logger.info("All user_colour mappings accessed")
logger.info(user_colour)
return user_colour