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
9 changes: 9 additions & 0 deletions mapq/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Simplified methods for calling Mapquest's Geocoding API.
"""
from __future__ import unicode_literals

import os
from .geo import Geo
Expand All @@ -13,6 +14,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
19 changes: 16 additions & 3 deletions mapq/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

Documentation: http://www.mapquestapi.com/geocoding/
"""
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import object

import os
from urllib import unquote
from urllib.parse import unquote

import requests as req
from simplejson import loads
Expand All @@ -14,9 +18,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 +31,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
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'mapq'
],
install_requires=[
'future',
'requests',
'simplejson',
],
Expand All @@ -57,5 +58,7 @@
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
)