diff --git a/geocoder/api.py b/geocoder/api.py index 546d4cd1..7e581dfd 100755 --- a/geocoder/api.py +++ b/geocoder/api.py @@ -249,6 +249,7 @@ def yandex(location, **kwargs): """Yandex Provider :param ``location``: Your search location you want geocoded. + :param ``apikey``: YANDEX API KEY :param ``maxRows``: (default=1) Max number of results to fetch :param ``lang``: Chose the following language: > ru-RU — Russian (by default) diff --git a/geocoder/keys.py b/geocoder/keys.py index 6d42c2d9..9087109d 100644 --- a/geocoder/keys.py +++ b/geocoder/keys.py @@ -25,6 +25,7 @@ geocodefarm_key = os.environ.get('GEOCODEFARM_API_KEY') tgos_key = os.environ.get('TGOS_API_KEY') locationiq_key = os.environ.get('LOCATIONIQ_API_KEY') +yandex_key = os.environ.get('YANDEX_API_KEY') class CanadapostKeyLazySingleton(object): diff --git a/geocoder/yandex.py b/geocoder/yandex.py index c55e15fd..a09f04db 100644 --- a/geocoder/yandex.py +++ b/geocoder/yandex.py @@ -5,6 +5,7 @@ import logging from geocoder.base import OneResult, MultipleResultsQuery +from geocoder.keys import yandex_key class YandexResult(OneResult): @@ -144,7 +145,7 @@ class YandexQuery(MultipleResultsQuery): _URL = 'https://geocode-maps.yandex.ru/1.x/' _RESULT_CLASS = YandexResult - _KEY_MANDATORY = False + _KEY = yandex_key def _build_params(self, location, provider_key, **kwargs): return { @@ -152,6 +153,7 @@ def _build_params(self, location, provider_key, **kwargs): 'lang': kwargs.get('lang', 'en-US'), 'kind': kwargs.get('kind', ''), 'format': 'json', + 'apikey': provider_key, 'results': kwargs.get('maxRows', 1), } diff --git a/geocoder/yandex_reverse.py b/geocoder/yandex_reverse.py index ad7cd0aa..7656f795 100644 --- a/geocoder/yandex_reverse.py +++ b/geocoder/yandex_reverse.py @@ -58,6 +58,7 @@ def _build_params(self, location, provider_key, **kwargs): 'lang': kwargs.get('lang', 'en-US'), 'kind': kwargs.get('kind', ''), 'format': 'json', + 'apikey': provider_key, 'results': kwargs.get('maxRows', 1), } diff --git a/tests/results/yandex.json b/tests/results/yandex.json new file mode 100644 index 00000000..2da1616f --- /dev/null +++ b/tests/results/yandex.json @@ -0,0 +1,68 @@ +{ + "response": { + "GeoObjectCollection": { + "metaDataProperty": { + "GeocoderResponseMetaData": { + "request": "Ottawa", + "results": "1", + "found": "30" + } + }, + "featureMember": [ + { + "GeoObject": { + "metaDataProperty": { + "GeocoderMetaData": { + "precision": "other", + "text": "Canada, Ottawa", + "kind": "locality", + "Address": { + "country_code": "CA", + "formatted": "Canada, Ottawa", + "Components": [ + { + "kind": "country", + "name": "Canada" + }, + { + "kind": "province", + "name": "Ontario" + }, + { + "kind": "locality", + "name": "Ottawa" + } + ] + }, + "AddressDetails": { + "Country": { + "AddressLine": "Canada, Ottawa", + "CountryNameCode": "CA", + "CountryName": "Canada", + "AdministrativeArea": { + "AdministrativeAreaName": "Ontario", + "Locality": { + "LocalityName": "Ottawa" + } + } + } + } + } + }, + "name": "Ottawa", + "description": "Canada", + "boundedBy": { + "Envelope": { + "lowerCorner": "-75.803185 45.338299", + "upperCorner": "-75.608583 45.456097" + } + }, + "Point": { + "pos": "-75.699583 45.401795" + } + } + } + ] + } + } +} diff --git a/tests/results/yandex_batch.json b/tests/results/yandex_batch.json new file mode 100644 index 00000000..a3f21de8 --- /dev/null +++ b/tests/results/yandex_batch.json @@ -0,0 +1,192 @@ +{ + "response": { + "GeoObjectCollection": { + "metaDataProperty": { + "GeocoderResponseMetaData": { + "request": "Ottawa", + "results": "3", + "found": "30" + } + }, + "featureMember": [ + { + "GeoObject": { + "metaDataProperty": { + "GeocoderMetaData": { + "precision": "other", + "text": "Canada, Ottawa", + "kind": "locality", + "Address": { + "country_code": "CA", + "formatted": "Canada, Ottawa", + "Components": [ + { + "kind": "country", + "name": "Canada" + }, + { + "kind": "province", + "name": "Ontario" + }, + { + "kind": "locality", + "name": "Ottawa" + } + ] + }, + "AddressDetails": { + "Country": { + "AddressLine": "Canada, Ottawa", + "CountryNameCode": "CA", + "CountryName": "Canada", + "AdministrativeArea": { + "AdministrativeAreaName": "Ontario", + "Locality": { + "LocalityName": "Ottawa" + } + } + } + } + } + }, + "name": "Ottawa", + "description": "Canada", + "boundedBy": { + "Envelope": { + "lowerCorner": "-75.803185 45.338299", + "upperCorner": "-75.608583 45.456097" + } + }, + "Point": { + "pos": "-75.699583 45.401795" + } + } + }, + { + "GeoObject": { + "metaDataProperty": { + "GeocoderMetaData": { + "precision": "other", + "text": "United States of America, State of Oklahoma, Ottawa County, Ottawa", + "kind": "hydro", + "Address": { + "country_code": "US", + "formatted": "United States of America, State of Oklahoma, Ottawa County, Ottawa", + "Components": [ + { + "kind": "country", + "name": "United States of America" + }, + { + "kind": "province", + "name": "State of Oklahoma" + }, + { + "kind": "area", + "name": "Ottawa County" + }, + { + "kind": "hydro", + "name": "Ottawa" + } + ] + }, + "AddressDetails": { + "Country": { + "AddressLine": "United States of America, State of Oklahoma, Ottawa County, Ottawa", + "CountryNameCode": "US", + "CountryName": "United States of America", + "AdministrativeArea": { + "AdministrativeAreaName": "State of Oklahoma", + "SubAdministrativeArea": { + "SubAdministrativeAreaName": "Ottawa County", + "Locality": { + "Premise": { + "PremiseName": "Ottawa" + } + } + } + } + } + } + } + }, + "name": "Ottawa", + "description": "Ottawa County, State of Oklahoma, United States of America", + "boundedBy": { + "Envelope": { + "lowerCorner": "-95.037481 36.799996", + "upperCorner": "-94.700289 36.998394" + } + }, + "Point": { + "pos": "-94.887588 36.874997" + } + } + }, + { + "GeoObject": { + "metaDataProperty": { + "GeocoderMetaData": { + "precision": "other", + "text": "United States of America, State of Michigan, Ottawa County, Ottawa", + "kind": "hydro", + "Address": { + "country_code": "US", + "formatted": "United States of America, State of Michigan, Ottawa County, Ottawa", + "Components": [ + { + "kind": "country", + "name": "United States of America" + }, + { + "kind": "province", + "name": "State of Michigan" + }, + { + "kind": "area", + "name": "Ottawa County" + }, + { + "kind": "hydro", + "name": "Ottawa" + } + ] + }, + "AddressDetails": { + "Country": { + "AddressLine": "United States of America, State of Michigan, Ottawa County, Ottawa", + "CountryNameCode": "US", + "CountryName": "United States of America", + "AdministrativeArea": { + "AdministrativeAreaName": "State of Michigan", + "SubAdministrativeArea": { + "SubAdministrativeAreaName": "Ottawa County", + "Locality": { + "Premise": { + "PremiseName": "Ottawa" + } + } + } + } + } + } + } + }, + "name": "Ottawa", + "description": "Ottawa County, State of Michigan, United States of America", + "boundedBy": { + "Envelope": { + "lowerCorner": "-86.250583 42.768294", + "upperCorner": "-85.781788 43.118997" + } + }, + "Point": { + "pos": "-86.171585 42.910494" + } + } + } + ] + } + } +} diff --git a/tests/results/yandex_reverse.json b/tests/results/yandex_reverse.json new file mode 100644 index 00000000..fff71d34 --- /dev/null +++ b/tests/results/yandex_reverse.json @@ -0,0 +1,64 @@ +{ + "response": { + "GeoObjectCollection": { + "metaDataProperty": { + "GeocoderResponseMetaData": { + "Point": { + "pos": "41.005407 28.978349" + }, + "request": "41.005407,28.978349", + "results": "1", + "found": "3" + } + }, + "featureMember": [ + { + "GeoObject": { + "metaDataProperty": { + "GeocoderMetaData": { + "precision": "other", + "text": "Saudi Arabia, Northern Borders", + "kind": "province", + "Address": { + "country_code": "SA", + "formatted": "Saudi Arabia, Northern Borders", + "Components": [ + { + "kind": "country", + "name": "Saudi Arabia" + }, + { + "kind": "province", + "name": "Northern Borders" + } + ] + }, + "AddressDetails": { + "Country": { + "AddressLine": "Saudi Arabia, Northern Borders", + "CountryNameCode": "SA", + "CountryName": "Saudi Arabia", + "AdministrativeArea": { + "AdministrativeAreaName": "Northern Borders" + } + } + } + } + }, + "name": "Northern Borders", + "description": "Saudi Arabia", + "boundedBy": { + "Envelope": { + "lowerCorner": "37.812201 27.480507", + "upperCorner": "45.934732 32.221297" + } + }, + "Point": { + "pos": "42.666562 29.958972" + } + } + } + ] + } + } +} diff --git a/tests/test_yandex.py b/tests/test_yandex.py index 5de9f691..960e7ffc 100644 --- a/tests/test_yandex.py +++ b/tests/test_yandex.py @@ -1,21 +1,35 @@ # coding: utf8 +import requests_mock import geocoder location = 'Ottawa' coordinates = {'lat': 41.005407, 'lng': 28.978349} +location_url = (f'https://geocode-maps.yandex.ru/1.x/?apikey=mock&geocode=' + f'{location}') +coordinates_url = (f'https://geocode-maps.yandex.ru/1.x/?apikey=mock&geocode=' + f'{coordinates["lng"]}, {coordinates["lat"]}') def test_yandex(): - g = geocoder.yandex(location) - assert g.ok + data_file = 'tests/results/yandex.json' + with requests_mock.Mocker() as mocker, open(data_file, 'r') as input: + mocker.get(location_url, text=input.read()) + g = geocoder.yandex(location, key='mock') + assert g.ok def test_yandex_reverse(): - g = geocoder.yandex(coordinates, method='reverse') - assert g.ok + data_file = 'tests/results/yandex_reverse.json' + with requests_mock.Mocker() as mocker, open(data_file, 'r') as input: + mocker.get(coordinates_url, text=input.read()) + g = geocoder.yandex(coordinates, method='reverse', key='mock') + assert g.ok def test_multi_results(): - g = geocoder.yandex(location, maxRows=3) - assert len(g) == 3 + data_file = 'tests/results/yandex_batch.json' + with requests_mock.Mocker() as mocker, open(data_file, 'r') as input: + mocker.get(location_url, text=input.read()) + g = geocoder.yandex(location, maxRows=3, key='mock') + assert len(g) == 3