From ee9c404d83b9fa99ae67cda171f3efcf6587e7bf Mon Sep 17 00:00:00 2001 From: chriscohoat Date: Sat, 8 Mar 2014 19:16:07 -0500 Subject: [PATCH 1/3] Added ability to set an API endpoint, in case open.mapquestapi.com should be used in place of mapquestapi.com. --- mapq/.gitignore | 1 + mapq/__init__.py | 2 +- mapq/api.py | 8 ++++++++ mapq/geo.py | 13 +++++++++++-- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 mapq/.gitignore diff --git a/mapq/.gitignore b/mapq/.gitignore new file mode 100644 index 0000000..a74b07a --- /dev/null +++ b/mapq/.gitignore @@ -0,0 +1 @@ +/*.pyc diff --git a/mapq/__init__.py b/mapq/__init__.py index d015ad6..606b9b7 100644 --- a/mapq/__init__.py +++ b/mapq/__init__.py @@ -1,2 +1,2 @@ -from .api import key, address, batch, geocode, latlng, reverse +from .api import key, endpoint, address, batch, geocode, latlng, reverse from .geo import Geo diff --git a/mapq/api.py b/mapq/api.py index 45cecd9..06cd632 100644 --- a/mapq/api.py +++ b/mapq/api.py @@ -13,6 +13,14 @@ def key(api_key=None): return os.environ['MAPQUEST_API_KEY'] +def endpoint(endpoint=None): + """Set the endpoint as an environment variable.""" + if endpoint: + os.environ['MAPQUEST_ENDPOINT'] = endpoint + return os.environ['MAPQUEST_ENDPOINT'] + + + def address(name, **kwargs): """Geocode an address.""" return Geo().address(name, **kwargs) diff --git a/mapq/geo.py b/mapq/geo.py index c766206..2eb4c7c 100644 --- a/mapq/geo.py +++ b/mapq/geo.py @@ -14,9 +14,9 @@ class Geo(object): """A simple Mapquest Geocoding API wrapper.""" - def __init__(self, api_key=None): + def __init__(self, api_key=None, endpoint=None): self._set_key(api_key) - self.endpoint = "http://www.mapquestapi.com/geocoding/v1" + self._set_endpoint(endpoint) def _set_key(self, api_key): """Configure the instance's Mapquest API key.""" @@ -27,6 +27,15 @@ def _set_key(self, api_key): api_key = '' self.api_key = unquote(api_key) + def _set_endpoint(self, endpoint): + """Configure the instance's Mapquest endpoint.""" + if not endpoint: + if 'MAPQUEST_ENDPOINT' in os.environ: + endpoint = os.environ['MAPQUEST_ENDPOINT'] + else: + endpoint = 'http://www.mapquestapi.com/geocoding/v1' + self.endpoint = endpoint + def get(self, path, **kwargs): """Perform a get request.""" if 'key' not in kwargs and 'api_key' not in kwargs: From b780995b09051aef78000a5ac2df9e47c672150f Mon Sep 17 00:00:00 2001 From: chriscohoat Date: Sat, 8 Mar 2014 19:20:43 -0500 Subject: [PATCH 2/3] Updated readme with endpoint information. --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index fdcd444..35ae110 100644 --- a/README.md +++ b/README.md @@ -66,3 +66,16 @@ class. >>> g.latlng('155 9th St San Francisco, CA') {'lat': 37.775002, 'lng': -122.418297} ``` + + +Specify a Mapquest endpoint if desired. This allows the Open Data endpoint to be used instead of the licensed endpoint. + + +```python +>>> import mapq + +>>> mapq.endpoint('http://open.mapquestapi.com/geocoding/v1') + +```python +>>> from mapq import Geo +>>> g = Geo('my_api_key', 'http://open.mapquestapi.com/geocoding/v1') From 874ccc5903fad45cc3875fe059565bc45ee3df1c Mon Sep 17 00:00:00 2001 From: chriscohoat Date: Sat, 8 Mar 2014 19:21:51 -0500 Subject: [PATCH 3/3] Fixed second python block in README. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 35ae110..95b538d 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,12 @@ Specify a Mapquest endpoint if desired. This allows the Open Data endpoint to be >>> import mapq >>> mapq.endpoint('http://open.mapquestapi.com/geocoding/v1') +``` + +Can also be set when interacting with the `Geo` class. ```python >>> from mapq import Geo >>> g = Geo('my_api_key', 'http://open.mapquestapi.com/geocoding/v1') +``` +