Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
```

1 change: 1 addition & 0 deletions mapq/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*.pyc
2 changes: 1 addition & 1 deletion mapq/__init__.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions mapq/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions mapq/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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:
Expand Down