diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..7aa1813 Binary files /dev/null and b/.DS_Store differ diff --git a/Abstract class/abstractCoronavirus.py b/Abstract class/abstractCoronavirus.py new file mode 100644 index 0000000..e012d88 --- /dev/null +++ b/Abstract class/abstractCoronavirus.py @@ -0,0 +1,34 @@ +from abc import ABCMeta, abstractmethod + +class AbstractCoronavirus(metaclass=ABCMeta): + + @abstractmethod + def _update(): + pass + @abstractmethod + def _getSources(): + pass + @abstractmethod + def _request(): + pass + @abstractmethod + def getAll(): + pass + @abstractmethod + def getLatestChanges(): + pass + @abstractmethod + def getLatest(): + pass + @abstractmethod + def getLocations(): + pass + @abstractmethod + def getLocationByCountryCode(): + pass + @abstractmethod + def getLocationByCountry(): + pass + @abstractmethod + def getLocationById(): + pass diff --git a/COVID19Py/.DS_Store b/COVID19Py/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/COVID19Py/.DS_Store differ diff --git a/COVID19Py/ObjectBuilder.py b/COVID19Py/ObjectBuilder.py new file mode 100644 index 0000000..3d0d457 --- /dev/null +++ b/COVID19Py/ObjectBuilder.py @@ -0,0 +1,91 @@ +from abc import ABCMeta,abstractmethod +from typing import Dict, List +import covid19 +import requests +import json + +"The Builder Interface" +class InterfaceBuilder(metaclass=ABCMeta): + @staticmethod + @abstractmethod + def setURL(): + "build url" + + @staticmethod + @abstractmethod + def setDataSource(): + "build data source" + + @staticmethod + @abstractmethod + def getProduct(): + "return the final product" + +"The concrete Builder" +class ObjectBuilder(InterfaceBuilder): + + def __init__(self): + self.product = covid19.COVID19() + + def setURL(self,url): + if url == self.product.default_url: + + # Load mirrors + response = requests.get(self.product.mirrors_source) + response.raise_for_status() + self.product.mirrors = response.json() + # Try to get sources as a test + for mirror in self.product.mirrors: + # Set URL of mirror + self.product.url = mirror["url"] + result = None + + try: + result = self.product._getSources() + + except Exception as e: + + # URL did not work, reset it and move on + self.product.url = "" + continue + + # TODO: Should have a better health-check, this is way too hacky... + if "jhu" in result: + # We found a mirror that worked just fine, let's stick with it + break + + # None of the mirrors worked. Raise an error to inform the user. + raise RuntimeError("No available API mirror was found.") + + else: + self.product.url = url + + return self + + def setDataSource(self,data_source): + + self.product._valid_data_sources = self.product._getSources() + + if data_source not in self.product._valid_data_sources: + raise ValueError("Invalid data source. Expected one of: %s" % self._valid_data_sources) + self.product.data_source = data_source + + return self + + def getProduct(self): + return self.product + + +class Director: + + @staticmethod + def constructURL(obj=ObjectBuilder(),url="https://covid-tracker-us.herokuapp.com"): + return obj.setURL(url) + @staticmethod + def constructDataSource(obj=ObjectBuilder(),dataSource='jhu'): + return obj.setDataSource(dataSource) + @staticmethod + def contructWholeProduct(obj=ObjectBuilder(),url="https://covid-tracker-us.herokuapp.com", dataSource='jhu'): + return obj.setURL(url).setDataSource(dataSource).getProduct() + + diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index a68faa7..558fd0c 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -1,53 +1,18 @@ from typing import Dict, List import requests import json +from abstractCoronavirus import AbstractCoronavirus -class COVID19(object): - default_url = "https://covid-tracker-us.herokuapp.com" - url = "" - data_source = "" +class COVID19(AbstractCoronavirus): previousData = None latestData = None - _valid_data_sources = [] - - mirrors_source = "https://raw.github.com/Kamaropoulos/COVID19Py/master/mirrors.json" - mirrors = None - - def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jhu'): - # Skip mirror checking if custom url was passed - if url == self.default_url: - # Load mirrors - response = requests.get(self.mirrors_source) - response.raise_for_status() - self.mirrors = response.json() - - # Try to get sources as a test - for mirror in self.mirrors: - # Set URL of mirror - self.url = mirror["url"] - result = None - try: - result = self._getSources() - except Exception as e: - # URL did not work, reset it and move on - self.url = "" - continue - - # TODO: Should have a better health-check, this is way too hacky... - if "jhu" in result: - # We found a mirror that worked just fine, let's stick with it - break - - # None of the mirrors worked. Raise an error to inform the user. - raise RuntimeError("No available API mirror was found.") - - else: - self.url = url - - self._valid_data_sources = self._getSources() - if data_source not in self._valid_data_sources: - raise ValueError("Invalid data source. Expected one of: %s" % self._valid_data_sources) - self.data_source = data_source + def __init__(self): + self.default_url = "https://covid-tracker-us.herokuapp.com" + self.mirrors_source = "https://raw.github.com/Kamaropoulos/COVID19Py/master/mirrors.json" + self._valid_data_sources = [] + self.url = "https://covid-tracker-us.herokuapp.com" + self.data_source = "jhu" + self.mirrors = None def _update(self, timelines): latest = self.getLatest() @@ -67,7 +32,7 @@ def _getSources(self): def _request(self, endpoint, params=None): if params is None: params = {} - response = requests.get(self.url + endpoint, {**params, "source":self.data_source}) + response = requests.get(self.url + endpoint, {**params, "source":self.data_source}) #syntax error fixed response.raise_for_status() return response.json()