Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/api/fast_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from fastapi import FastAPI, HTTPException, Request
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.responses import HTMLResponse
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from . import __version__
from utils.constants import DEFAULT_GREETING
from utils.helper import normalize_name
import streamlit as st
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The streamlit import should not be in the FastAPI module. This creates an unnecessary dependency and couples the API backend with the frontend framework. Streamlit is a UI framework that should only be imported in frontend files like streamlit_app.py.

Copilot uses AI. Check for mistakes.

app = FastAPI(
title="sample API",
Expand All @@ -19,6 +22,44 @@
},
)

def apply_common_styles():
st.markdown("""
<style>
.reportview-container {
background: #f0f2f6;
}
footer {visibility: hidden;}
.main-header {
font-size: 2.5rem;
color: #4B4B4B;
text-align: center;
margin-bottom: 2rem;
}
</style>
""", unsafe_allow_html=True)

st.markdown('<div class="main-header">Sample Python App</div>', unsafe_allow_html=True)
Comment on lines +25 to +41
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The apply_common_styles function is Streamlit-specific and should not be in the FastAPI module (fast_api.py). This function should be placed in the Streamlit app file (src/sample/streamlit_app.py) instead. The FastAPI module should only contain API-related code.

Copilot uses AI. Check for mistakes.

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
content = {
"status": "error",
"message": "Invalid data provided",
"details": exc.errors(),
},
)
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing blank line between the two exception handler decorators. According to PEP 8, there should be two blank lines between top-level function definitions for better readability.

Suggested change
)
)

Copilot uses AI. Check for mistakes.
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content = {
Comment on lines +47 to +57
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing around the equals sign is inconsistent. There should be no space before the equals sign. This appears on both lines 47 and 57.

Copilot uses AI. Check for mistakes.
"status": "error",
"message": "An internal server error occurred",
"details": str(exc),
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing internal exception details (str(exc)) in the API response is a security risk. This can leak sensitive information about the application's internal workings, file paths, or database schema to potential attackers. Consider logging the full exception internally and returning only a generic error message to the client.

Copilot uses AI. Check for mistakes.
},
)

class GreetRequest(BaseModel):
name: str
Expand Down Expand Up @@ -98,4 +139,7 @@ def start():


if __name__ == "__main__":
apply_common_styles()
st.write("Welcome to the app!")
start()

Comment on lines +142 to +145
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These Streamlit function calls (apply_common_styles and st.write) should not be in the FastAPI module's main block. The FastAPI module is meant to run the API server, not the Streamlit UI. When running the API with uvicorn or through the CLI command, these calls would fail since Streamlit is not initialized in this context.

Suggested change
apply_common_styles()
st.write("Welcome to the app!")
start()
start()

Copilot uses AI. Check for mistakes.