11"""app.routers.v2"""
2+ from typing import Any
3+ from app .services .repo .metricsprovider import MetricsProvider
24import enum
35
46from fastapi import APIRouter , HTTPException , Request
79from ..models import LatestResponse , LocationResponse , LocationsResponse
810
911V2 = APIRouter ()
12+ metrics_provider = MetricsProvider ()
1013
1114
1215class Sources (str , enum .Enum ):
@@ -26,14 +29,7 @@ async def get_latest(
2629 """
2730 Getting latest amount of total confirmed cases, deaths, and recoveries.
2831 """
29- locations = await request .state .source .get_all ()
30- return {
31- "latest" : {
32- "confirmed" : sum (map (lambda location : location .confirmed , locations )),
33- "deaths" : sum (map (lambda location : location .deaths , locations )),
34- "recovered" : sum (map (lambda location : location .recovered , locations )),
35- }
36- }
32+ return await metrics_provider .get_latest_global (request .state .source )
3733
3834
3935# pylint: disable=unused-argument,too-many-arguments,redefined-builtin
@@ -56,38 +52,13 @@ async def get_locations(
5652 params .pop ("source" , None )
5753 params .pop ("timelines" , None )
5854
59- # Retrieve all the locations.
60- locations = await request .state .source .get_all ()
61-
62- # Attempt to filter out locations with properties matching the provided query params.
63- for key , value in params .items ():
64- # Clean keys for security purposes.
65- key = key .lower ()
66- value = value .lower ().strip ("__" )
67-
68- # Do filtering.
69- try :
70- locations = [
71- location
72- for location in locations
73- if str (getattr (location , key )).lower () == str (value )
74- ]
75- except AttributeError :
76- pass
77- if not locations :
78- raise HTTPException (
79- 404 , detail = f"Source `{ source } ` does not have the desired location data." ,
80- )
81-
82- # Return final serialized data.
83- return {
84- "latest" : {
85- "confirmed" : sum (map (lambda location : location .confirmed , locations )),
86- "deaths" : sum (map (lambda location : location .deaths , locations )),
87- "recovered" : sum (map (lambda location : location .recovered , locations )),
88- },
89- "locations" : [location .serialize (timelines ) for location in locations ],
90- }
55+ # # Retrieve all the locations.
56+ try :
57+ return await metrics_provider .get_available_locations (request .state .source , params , timelines )
58+ except Exception as err :
59+ raise HTTPException (404 , str (err ))
60+
61+
9162
9263
9364# pylint: disable=invalid-name
@@ -98,8 +69,7 @@ async def get_location_by_id(
9869 """
9970 Getting specific location by id.
10071 """
101- location = await request .state .source .get (id )
102- return {"location" : location .serialize (timelines )}
72+ return await metrics_provider .get_location_by_id (request .state .source , id , timelines )
10373
10474
10575@V2 .get ("/sources" )
0 commit comments