Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggreate some methods to class CaseByCountry #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions COVID19Py/CaseByCountry.py
Original file line number Diff line number Diff line change
@@ -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"]
38 changes: 1 addition & 37 deletions COVID19Py/covid19.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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"]