diff --git a/README.md b/README.md index fdcd444..95b538d 100644 --- a/README.md +++ b/README.md @@ -66,3 +66,21 @@ 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') +``` + +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') +``` + 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: