2024-12-15 10:51:32 +03:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2024-12-14 23:22:44 +03:00
|
|
|
from fastapi import APIRouter, status, Request, Response
|
2024-12-15 10:51:32 +03:00
|
|
|
from pydantic import BaseModel
|
2024-12-14 23:22:44 +03:00
|
|
|
|
2024-12-16 21:40:22 +03:00
|
|
|
from function.create_chart import create_chart
|
|
|
|
from .get_chart_period import prepare_chart_response
|
2024-12-14 23:22:44 +03:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2024-12-15 10:51:32 +03:00
|
|
|
class ChartRequestParams(BaseModel):
|
|
|
|
"""
|
|
|
|
A Pydantic model that represents the request parameters for generating a chart.
|
2024-12-15 10:55:05 +03:00
|
|
|
This model is used to validate and group the request parameters: from_currency, conv_currency, start_date, and end_date.
|
2024-12-15 10:51:32 +03:00
|
|
|
"""
|
|
|
|
from_currency: str
|
|
|
|
conv_currency: str
|
|
|
|
start_date: str
|
|
|
|
end_date: str
|
|
|
|
|
2024-12-14 23:22:44 +03:00
|
|
|
@router.get("/api/getChart/", status_code=status.HTTP_201_CREATED)
|
|
|
|
async def get_chart(
|
2024-12-14 23:36:01 +03:00
|
|
|
response: Response,
|
|
|
|
request: Request,
|
2024-12-15 10:55:05 +03:00
|
|
|
from_currency: str,
|
|
|
|
conv_currency: str,
|
|
|
|
start_date: str,
|
|
|
|
end_date: str
|
2024-12-14 23:36:01 +03:00
|
|
|
):
|
2024-12-15 10:51:32 +03:00
|
|
|
"""
|
|
|
|
Fetches a chart for a given currency pair and date range.
|
2024-12-14 23:22:44 +03:00
|
|
|
|
2024-12-15 10:51:32 +03:00
|
|
|
:param response: The response object used for returning the HTTP response.
|
|
|
|
:param request: The request object containing details about the incoming request.
|
2024-12-15 10:55:05 +03:00
|
|
|
:param from_currency: The base currency for conversion.
|
|
|
|
:param conv_currency: The target currency for conversion.
|
|
|
|
:param start_date: The start date for the chart data.
|
|
|
|
:param end_date: The end date for the chart data.
|
2024-12-15 10:51:32 +03:00
|
|
|
:return: A chart or an error message if the request is invalid.
|
|
|
|
"""
|
2024-12-15 10:55:05 +03:00
|
|
|
if not from_currency or not conv_currency:
|
2024-12-14 23:22:44 +03:00
|
|
|
response.status_code = status.HTTP_400_BAD_REQUEST
|
|
|
|
return {
|
2024-12-14 23:36:01 +03:00
|
|
|
'status': status.HTTP_400_BAD_REQUEST,
|
|
|
|
'message': 'The from_currency and conv_currency fields are required.',
|
|
|
|
}
|
|
|
|
|
2024-12-15 10:55:05 +03:00
|
|
|
if not start_date or not end_date:
|
2024-12-14 23:22:44 +03:00
|
|
|
response.status_code = status.HTTP_400_BAD_REQUEST
|
|
|
|
return {
|
2024-12-14 23:36:01 +03:00
|
|
|
'status': status.HTTP_400_BAD_REQUEST,
|
|
|
|
'message': 'The start_date and end_date fields are required.',
|
2024-12-14 23:22:44 +03:00
|
|
|
}
|
|
|
|
|
2024-12-15 10:51:32 +03:00
|
|
|
chart = await create_chart(
|
2024-12-15 10:55:05 +03:00
|
|
|
from_currency,
|
|
|
|
conv_currency,
|
|
|
|
start_date,
|
|
|
|
end_date
|
2024-12-15 10:51:32 +03:00
|
|
|
)
|
2024-12-14 23:36:01 +03:00
|
|
|
return await prepare_chart_response(response, request, chart)
|