mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 20:51:25 +03:00
refactor(chart): Added docstring in each file and function. Fixed long lines
This commit is contained in:
parent
c5d3d9fe9b
commit
ed8cf91558
7 changed files with 190 additions and 30 deletions
|
@ -1,34 +1,60 @@
|
|||
"""
|
||||
This module contains the route for retrieving a chart based on a given currency pair and date range.
|
||||
It defines the `/api/getChart/` endpoint that processes requests for generating charts.
|
||||
"""
|
||||
from fastapi import APIRouter, status, Request, Response
|
||||
from pydantic import BaseModel
|
||||
|
||||
from chart.function.create_chart import create_chart
|
||||
from chart.routes.get_chart_period import prepare_chart_response
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class ChartRequestParams(BaseModel):
|
||||
"""
|
||||
A Pydantic model that represents the request parameters for generating a chart.
|
||||
|
||||
This model is used to validate and group the request parameters:
|
||||
from_currency, conv_currency, start_date, and end_date.
|
||||
"""
|
||||
from_currency: str
|
||||
conv_currency: str
|
||||
start_date: str
|
||||
end_date: str
|
||||
|
||||
@router.get("/api/getChart/", status_code=status.HTTP_201_CREATED)
|
||||
async def get_chart(
|
||||
response: Response,
|
||||
request: Request,
|
||||
from_currency: str = None,
|
||||
conv_currency: str = None,
|
||||
start_date: str = None,
|
||||
end_date: str = None,
|
||||
params: ChartRequestParams
|
||||
):
|
||||
"""
|
||||
Fetches a chart for a given currency pair and date range.
|
||||
|
||||
if not from_currency or not conv_currency:
|
||||
:param response: The response object used for returning the HTTP response.
|
||||
:param request: The request object containing details about the incoming request.
|
||||
:param params: Contains the request parameters:
|
||||
from_currency, conv_currency, start_date, and end_date.
|
||||
:return: A chart or an error message if the request is invalid.
|
||||
"""
|
||||
if not params.from_currency or not params.conv_currency:
|
||||
response.status_code = status.HTTP_400_BAD_REQUEST
|
||||
return {
|
||||
'status': status.HTTP_400_BAD_REQUEST,
|
||||
'message': 'The from_currency and conv_currency fields are required.',
|
||||
}
|
||||
|
||||
elif not start_date and not end_date:
|
||||
if not params.start_date or not params.end_date:
|
||||
response.status_code = status.HTTP_400_BAD_REQUEST
|
||||
return {
|
||||
'status': status.HTTP_400_BAD_REQUEST,
|
||||
'message': 'The start_date and end_date fields are required.',
|
||||
}
|
||||
|
||||
|
||||
chart = await create_chart(from_currency, conv_currency, start_date, end_date)
|
||||
chart = await create_chart(
|
||||
params.from_currency,
|
||||
params.conv_currency,
|
||||
params.start_date,
|
||||
params.end_date
|
||||
)
|
||||
return await prepare_chart_response(response, request, chart)
|
||||
|
|
Loading…
Add table
Reference in a new issue