diff --git a/docs/source/providers/OpenCage.rst b/docs/source/providers/OpenCage.rst index 2a48d111..829daec2 100644 --- a/docs/source/providers/OpenCage.rst +++ b/docs/source/providers/OpenCage.rst @@ -51,6 +51,7 @@ Parameters - `location`: Your search location you want geocoded. - `key`: (optional) use your own API Key from OpenCage. - `maxRows`: (default=1) Max number of results to fetch +- `countrycode`: (default=None) A string representing the country codes to restrict the search to (e.g. 'ca,us') - `method`: (default=geocode) Use the following: - geocode diff --git a/geocoder/opencage.py b/geocoder/opencage.py index 98d14e50..3573bd19 100644 --- a/geocoder/opencage.py +++ b/geocoder/opencage.py @@ -11,7 +11,6 @@ class OpenCageResult(OneResult): - def __init__(self, json_content): # create safe shortcuts self._geometry = json_content.get('geometry', {}) @@ -404,6 +403,10 @@ def _build_params(self, location, provider_key, **kwargs): if language: base_params['language'] = language + countrycode = kwargs.get('countrycode', None) + if countrycode: + base_params['countrycode'] = countrycode + return base_params def _catch_errors(self, json_response): diff --git a/tests/test_opencage.py b/tests/test_opencage.py index ed1fab4b..e21e82aa 100644 --- a/tests/test_opencage.py +++ b/tests/test_opencage.py @@ -28,9 +28,14 @@ def test_opencage(): def test_issue_292(): - g = geocoder.opencage('AirportClinic M - MediCare Flughafen München Medizinisches Zentrum', countrycode='DE', language='de', no_annotations=1) + g = geocoder.opencage( + 'AirportClinic M - MediCare Flughafen München Medizinisches Zentrum', + countrycode='DE', + language='de', + no_annotations=1) assert g.ok + def test_opencage_no_language_param(): """ Expected result : https://api.opencagedata.com/geocode/v1/json=Ottawa,Ontario&key=YOUR-API-KEY @@ -38,6 +43,7 @@ def test_opencage_no_language_param(): g = geocoder.opencage(location) assert 'language' not in g.url + def test_opencage_language_param(): """ Expected result : https://api.opencagedata.com/geocode/v1/json=Ottawa,Ontario&key=YOUR-API-KEY&language=de @@ -45,6 +51,15 @@ def test_opencage_language_param(): g = geocoder.opencage(location, language='de') assert 'language=de' in g.url.split('&') + +def test_opencage_countrycode_param(): + """ Expected result: + https://api.opencagedata.com/geocode/v1/json?q=Ottawa,Ontario&key=YOUR-API-KEY&countrycode=ca" + """ + g = geocoder.opencage(location, countrycode='ca') + assert 'countrycode=ca' in g.url.split('&') + + def test_opencage_multi_result(): g = geocoder.opencage(location, maxRows=5) assert len(g) > 1 @@ -66,9 +81,11 @@ def test_opencage_address(): assert (g.remaining_api_calls > 0 and g.remaining_api_calls != 999999) assert (g.limit_api_calls > 0 and g.remaining_api_calls != 999999) + def test_opencage_paid(): # Paid API keys can be set to unlimited and have rate limit information ommitted from the response - url = 'http://api.opencagedata.com/geocode/v1/json?query=The+Happy+Goat%2C+Ottawa&limit=1&key=' + os.environ.get('OPENCAGE_API_KEY') + url = 'http://api.opencagedata.com/geocode/v1/json?query=The+Happy+Goat%2C+Ottawa&limit=1&key=' + os.environ.get( + 'OPENCAGE_API_KEY') data_file = 'tests/results/opencagedata_paid.json' with requests_mock.Mocker() as mocker, open(data_file, 'r') as input: mocker.get(url, text=input.read()) @@ -81,8 +98,6 @@ def test_opencage_paid(): assert result.limit_api_calls == 999999 - - def test_opencage_reverse(): """ Expected result : https://api.opencagedata.com/geocode/v1/json?q=45.4215296,-75.6971930&key=YOUR-API-KEY