Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge a849083 into 9fb0497
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillies committed Jan 6, 2018
2 parents 9fb0497 + a849083 commit 13d7c86
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 53 deletions.
5 changes: 5 additions & 0 deletions docs/distance.md
@@ -1,5 +1,10 @@
# Distance

**DEPRECATED**

The `mapbox.services.distance` module will be removed in 1.0. Please switch
to the new `mapbox.services.matrix` module.

The `Distance` class from the `mapbox.services.distance` module provides
access to the Mapbox Distance API. You can also import it directly from the
`mapbox` module.
Expand Down
42 changes: 15 additions & 27 deletions mapbox/services/distance.py
@@ -1,34 +1,22 @@
from uritemplate import URITemplate
"""Distance API V1 **DEPRECATED**"""

from mapbox.encoding import encode_coordinates_json
from mapbox.errors import InvalidProfileError
from mapbox.services.base import Service
import warnings

from mapbox.services.matrix import DirectionsMatrix

class Distance(Service):
"""Access to the Distance API V1"""

api_name = 'distances'
api_version = 'v1'
class Distance(object):
"""Access to the Distance API V1 ***DEPRECATED**"""

valid_profiles = ['driving', 'cycling', 'walking']

@property
def baseuri(self):
return 'https://{0}/{1}/{2}/mapbox'.format(
self.host, self.api_name, self.api_version)

def _validate_profile(self, profile):
if profile not in self.valid_profiles:
raise InvalidProfileError(
"{0} is not a valid profile".format(profile))
return profile
def __init__(self, access_token=None, host=None, cache=None):
warnings.warn(
"The distance module will be removed in the next version. "
"Use the matrix module instead.", DeprecationWarning)
self.access_token = access_token
self.host = host
self.cache = cache

def distances(self, features, profile='driving'):
profile = self._validate_profile(profile)
coords = encode_coordinates_json(features)
uri = URITemplate(self.baseuri + '/{profile}').expand(profile=profile)
res = self.session.post(uri, data=coords,
headers={'Content-Type': 'application/json'})
self.handle_http_error(res)
return res
service = DirectionsMatrix(
access_token=self.access_token, host=self.host, cache=self.cache)
return service.matrix(features, profile=profile)
31 changes: 5 additions & 26 deletions tests/test_distances.py
Expand Up @@ -18,8 +18,7 @@
"type": "Point",
"coordinates": [
-86.577791,
36.722137
]}}, {
36.722137]}}, {
"type": "Feature",
"properties": {},
"geometry": {
Expand All @@ -29,32 +28,12 @@
36.922175]}}]


def test_class_attrs():
"""Get expected class attr values"""
serv = mapbox.Distance()
assert serv.api_name == 'distances'
assert serv.api_version == 'v1'


def test_profile_invalid():
"""'jetpack' is not a valid profile."""
with pytest.raises(ValueError):
mapbox.Distance(access_token='pk.test')._validate_profile('jetpack')


@pytest.mark.parametrize('profile', ['driving', 'cycling', 'walking'])
def test_profile_valid(profile):
"""Profiles are valid"""
assert profile == mapbox.Distance(
access_token='pk.test')._validate_profile(profile)


@responses.activate
def test_distance():

responses.add(
responses.POST,
'https://api.mapbox.com/distances/v1/mapbox/driving?access_token=pk.test',
responses.GET,
'https://api.mapbox.com/directions-matrix/v1/mapbox/driving/-87.337875,36.539156;-86.577791,36.722137;-88.247685,36.922175?access_token=pk.test',
match_querystring=True,
body='{"durations":[[0,4977,5951],[4963,0,9349],[5881,9317,0]]}',
status=200,
Expand All @@ -69,8 +48,8 @@ def test_distance():
def test_distances_matrix():

responses.add(
responses.POST,
'https://api.mapbox.com/distances/v1/mapbox/driving?access_token=pk.test',
responses.GET,
'https://api.mapbox.com/directions-matrix/v1/mapbox/driving/-87.337875,36.539156;-86.577791,36.722137;-88.247685,36.922175?access_token=pk.test',
match_querystring=True,
body='{"durations":[[0,4977,5951],[4963,0,9349],[5881,9317,0]]}',
status=200,
Expand Down

0 comments on commit 13d7c86

Please sign in to comment.