From 7765f30231b0142594f321e4c76735a9bcefd744 Mon Sep 17 00:00:00 2001 From: feiyu <593887326@qq.com> Date: Mon, 16 Aug 2021 10:02:14 +0800 Subject: [PATCH] Optimized the COMPOSITE PATTERN The modified code can pass related tests in the api --- app/main.py | 8 ++++---- app/services/location/__init__.py | 24 ++++++++---------------- app/services/location/csbs.py | 4 ++-- app/services/location/jhu.py | 4 ++-- app/services/location/nyt.py | 4 ++-- 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/app/main.py b/app/main.py index b9aff949..d2a0d973 100644 --- a/app/main.py +++ b/app/main.py @@ -13,10 +13,10 @@ from scout_apm.async_.starlette import ScoutMiddleware from sentry_sdk.integrations.asgi import SentryAsgiMiddleware -from .config import get_settings -from .data import data_source -from .routers import V1, V2 -from .utils.httputils import setup_client_session, teardown_client_session +from app.config import get_settings +from app.data import data_source +from app.routers import V1, V2 +from app.utils.httputils import setup_client_session, teardown_client_session # ############ # FastAPI App diff --git a/app/services/location/__init__.py b/app/services/location/__init__.py index 6d292b54..606e661a 100644 --- a/app/services/location/__init__.py +++ b/app/services/location/__init__.py @@ -6,23 +6,15 @@ class LocationService(ABC): """ Service for retrieving locations. """ - - @abstractmethod async def get_all(self): - """ - Gets and returns all of the locations. + # Get the locations. + locations = await get_locations() + return locations + + async def get(self, loc_id): # pylint: disable=arguments-differ + # Get location at the index equal to provided id. + locations = await self.get_all() + return locations[loc_id] - :returns: The locations. - :rtype: List[Location] - """ - raise NotImplementedError - @abstractmethod - async def get(self, id): # pylint: disable=redefined-builtin,invalid-name - """ - Gets and returns location with the provided id. - :returns: The location. - :rtype: Location - """ - raise NotImplementedError diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..8918dd38 100644 --- a/app/services/location/csbs.py +++ b/app/services/location/csbs.py @@ -19,7 +19,7 @@ class CSBSLocationService(LocationService): """ Service for retrieving locations from csbs """ - +''' async def get_all(self): # Get the locations. locations = await get_locations() @@ -29,7 +29,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ # Get location at the index equal to the provided id. locations = await self.get_all() return locations[loc_id] - +''' # Base URL for fetching data BASE_URL = "https://facts.csbs.org/covid-19/covid19_county.csv" diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..a3e61a4d 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -25,7 +25,7 @@ class JhuLocationService(LocationService): """ Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19). """ - +''' async def get_all(self): # Get the locations. locations = await get_locations() @@ -35,7 +35,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ # Get location at the index equal to provided id. locations = await self.get_all() return locations[loc_id] - +''' # --------------------------------------------------------------- diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..e577b4b7 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -20,7 +20,7 @@ class NYTLocationService(LocationService): """ Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data). """ - +''' async def get_all(self): # Get the locations. locations = await get_locations() @@ -30,7 +30,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ # Get location at the index equal to provided id. locations = await self.get_all() return locations[loc_id] - +''' # ---------------------------------------------------------------