diff --git a/COVID19Py/CaseByCountry.py b/COVID19Py/CaseByCountry.py new file mode 100644 index 0000000..1d14636 --- /dev/null +++ b/COVID19Py/CaseByCountry.py @@ -0,0 +1,37 @@ +from typing import Dict, List +#import COVID19Py +class CaseByCountry: + def __init__(self,Covid19Object=COVID19Py.COVID19()): + self.obj = Covid19Object + def getLocationByCountryCode(self, countryCode, timelines=False) -> List[Dict]: + """ + :param country_code: String denoting the ISO 3166-1 alpha-2 code (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) of the country + :param timelines: Whether timeline information should be returned as well. + :return: A list of areas that correspond to the country_code. If the country_code is invalid, it returns an empty list. + """ + data = None + if timelines: + data = self.obj._request("/v2/locations", {"country_code": country_code, "timelines": str(timelines).lower()}) + else: + data = self.obj._request("/v2/locations", {"country_code": country_code}) + return data["locations"] + def getLocationByCountryName(self, countryName, timelines=False) -> List[Dict]: + """ + :param country: String denoting name of the country + :param timelines: Whether timeline information should be returned as well. + :return: A list of areas that correspond to the country name. If the country is invalid, it returns an empty list. + """ + data = None + if timelines: + data = self.obj._request("/v2/locations", {"country": country, "timelines": str(timelines).lower()}) + else: + data = self.obj._request("/v2/locations", {"country": country}) + return data["locations"] + + def getLocationById(self, countryId: int): + """ + :param country_id: Country Id, an int + :return: A dictionary with case information for the specified location. + """ + data = self.obj._request("/v2/locations/" + str(country_id)) + return data["location"] diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index a68faa7..cb1ecae 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -32,12 +32,10 @@ def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jh # 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.") @@ -67,7 +65,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() @@ -122,37 +120,3 @@ def getLocations(self, timelines=False, rank_by: str = None) -> List[Dict]: data = ranked return data - - def getLocationByCountryCode(self, country_code, timelines=False) -> List[Dict]: - """ - :param country_code: String denoting the ISO 3166-1 alpha-2 code (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) of the country - :param timelines: Whether timeline information should be returned as well. - :return: A list of areas that correspond to the country_code. If the country_code is invalid, it returns an empty list. - """ - data = None - if timelines: - data = self._request("/v2/locations", {"country_code": country_code, "timelines": str(timelines).lower()}) - else: - data = self._request("/v2/locations", {"country_code": country_code}) - return data["locations"] - - def getLocationByCountry(self, country, timelines=False) -> List[Dict]: - """ - :param country: String denoting name of the country - :param timelines: Whether timeline information should be returned as well. - :return: A list of areas that correspond to the country name. If the country is invalid, it returns an empty list. - """ - data = None - if timelines: - data = self._request("/v2/locations", {"country": country, "timelines": str(timelines).lower()}) - else: - data = self._request("/v2/locations", {"country": country}) - return data["locations"] - - def getLocationById(self, country_id: int): - """ - :param country_id: Country Id, an int - :return: A dictionary with case information for the specified location. - """ - data = self._request("/v2/locations/" + str(country_id)) - return data["location"]