Skip to content

Commit

Permalink
Merge 8cd2d17 into 7a25a3a
Browse files Browse the repository at this point in the history
  • Loading branch information
iron3oxide committed Oct 10, 2022
2 parents 7a25a3a + 8cd2d17 commit c3082d8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion restcountries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# simpler import as described in the readme
from restcountries.base import RestCountryApiV2
from restcountries.base import Country, RestCountryApiV2

__version__ = "2.0.0"
14 changes: 6 additions & 8 deletions restcountries/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
import json

import requests


class RestCountryApiV2:
BASE_URI = "https://restcountries.com/v2"
Expand Down Expand Up @@ -36,7 +37,7 @@ def _get_country_list(cls, resource, term="", filters=None):

response = requests.get(uri)
if response.status_code == 200:
result_list = []
result_list: list[Country] = []
data = json.loads(response.text) # parse json to dict
if type(data) == list:
for (
Expand All @@ -47,7 +48,7 @@ def _get_country_list(cls, resource, term="", filters=None):
country = Country(country_data)
result_list.append(country)
else:
return Country(data)
result_list.append(Country(data))
return result_list
elif response.status_code == 404:
raise requests.exceptions.InvalidURL
Expand All @@ -58,7 +59,7 @@ def _get_country_list(cls, resource, term="", filters=None):
def get_all(cls, filters=None):
"""Returns all countries provided by restcountries.eu.
:param filters - a list of fields to filter the output of the request to include only the specified fields.
:param filters - a list of fields to filter the output of the request to include only the specified fields.
"""
resource = "/all"
return cls._get_country_list(resource, filters=filters)
Expand Down Expand Up @@ -106,7 +107,7 @@ def get_country_by_country_code(cls, alpha, filters=None):
You can look those up at wikipedia: https://en.wikipedia.org/wiki/ISO_3166-1
"""
resource = "/alpha"
return cls._get_country_list(resource, alpha, filters=filters)
return cls._get_country_list(resource, alpha, filters=filters)[0]

@classmethod
def get_countries_by_country_codes(cls, codes, filters=None):
Expand Down Expand Up @@ -167,9 +168,6 @@ def get_countries_by_capital(cls, capital, filters=None):


class Country:
def __str__(self):
return "{}".format(self.name)

def __init__(self, country_data):
self.top_level_domain = country_data.get("topLevelDomain")
self.alpha2_code = country_data.get("alpha2Code")
Expand Down

0 comments on commit c3082d8

Please sign in to comment.