From bc2064e5f2eaae8f8257f96e8480fa0879b62394 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:21:30 -0400 Subject: [PATCH 01/16] Update __init__.py --- mapbox/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mapbox/__init__.py b/mapbox/__init__.py index 5431645..ed5aa6b 100644 --- a/mapbox/__init__.py +++ b/mapbox/__init__.py @@ -1,16 +1,17 @@ # mapbox __version__ = "0.18.0" +from .services.analytics import Analytics from .services.datasets import Datasets from .services.directions import Directions +from .services.matrix import DirectionsMatrix from .services.geocoding import ( Geocoder, InvalidCountryCodeError, InvalidPlaceTypeError) +from .services.maps import Maps from .services.mapmatching import MapMatcher -from .services.matrix import DirectionsMatrix -from .services.surface import Surface from .services.static import Static from .services.static_style import StaticStyle -from .services.uploads import Uploader -from .services.analytics import Analytics +from .services.surface import Surface from .services.tilequery import Tilequery -from .services.maps import Maps +from .services.tilesets import Tilesets +from .services.uploads import Uploader From 7cdb1f8fb8076e8479a4028b69c04e1cc619ffe8 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:30:00 -0400 Subject: [PATCH 02/16] Create tilesets.py --- mapbox/services/tilesets.py | 173 ++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 mapbox/services/tilesets.py diff --git a/mapbox/services/tilesets.py b/mapbox/services/tilesets.py new file mode 100644 index 0000000..4b4a604 --- /dev/null +++ b/mapbox/services/tilesets.py @@ -0,0 +1,173 @@ +from mapbox.errors import ( + InvalidTilesetTypeError, + InvalidVisibilityError, + InvalidSortbyError, + InvalidLimitError +) + +from mapbox.services.base import Service + +from uritemplate import URITemplate + +class Tilesets(Service): + """Access to Tilesets API V1 + + Attributes + ---------- + api_name : str + The API's name. + + api_version : str + The API's version number. + + valid_tileset_types : list + The possible values for tileset_type. + + valid_visibilities : list + The possible values for visibility. + + valid_sortbys : list + The possible values for sortby. + + base_uri : str + The API's base URI, currently https://api.mapbox.com/tilesets/v1 + """ + + api_name = "tilesets" + + api_version = "v1" + + valid_tileset_types = [ + "raster", + "vector" + ] + + valid_visibilities = [ + "private", + "public" + ] + + valid_sortbys = [ + "created", + "modified" + ] + + @property + def base_uri(self): + """Forms base URI.""" + + return "https://{}/{}/{}".format( + self.host, + self.api_name, + self.api_version + ) + + def _validate_tileset_type(self, tileset_type): + """Validates tileset type, raising error if invalid.""" + + if tileset_type not in self.valid_tileset_types: + raise InvalidTilesetTypeError( + "{} is not a valid tileset type".format(tileset_type) + ) + + return tileset_type + + def _validate_visibility(self, visibility): + """Validates visibility, raising error if invalid.""" + + if visibility not in self.valid_visibilities: + raise InvalidVisibilityError( + "{} is not a valid value for visibility".format(visibility) + ) + + return visibility + + def _validate_sortby(self, sortby): + """Validates sortby, raising error if invalid.""" + + if sortby not in self.valid_sortbys: + raise InvalidSortbyError( + "{} is not a valid value for sortby".format(sortby) + ) + + return sortby + + def _validate_limit(self, limit): + """Validates limit, raising error if invalid.""" + + if (limit < 1) or (limit > 500): + raise InvalidLimitError( + "{} is not a valid value for limit".format(limit) + ) + + return limit + + def list_tilesets(self, tileset_type=None, visibility=None, + sortby=None, limit=None): + """Lists all tilesets for an account. + + tileset_type : str, optional + Filter results by tileset type. + + Valid values are raster or vector. + + visibility : str, optional + Filter results by visibility. + + Valid values are private or public. + + Private tilesets require an access token + belonging to the owner, while public + tilesets may be requested with an access + token belonging to any user. + + sortby : str, optional + Sort results by timestamp. + + Valid values are created or modified + + limit : int, optional + The maximum number of objects to return + (pagination), where 1 is the minimum value + and 500 is the maxium value. + + The default value is 100. + + Returns + ------- + request.Response + The response object with a tileset object. + """ + + # Build URI resource path. + + path_part = "/{username}" + path_values = dict(username=self.username) + uri = URITemplate(self.base_uri + path_part).expand(**path_values) + + # Validate tileset_type, visibility, sortby, and limit + # and build URI query parameters. + + query_parameters = dict() + + if tileset_type: + tileset_type = self._validate_tileset_type(tileset_type) + query_parameters["type"] = tileset_type + + if visibility: + visibility = self._validate_visibility(visibility) + query_parameters["visibility"] = visibility + + if sortby: + sortby = self._validate_sortby(sortby) + query_parameters["sortby"] = sortby + + if limit: + limit = self._validate_limit(limit) + query_parameters["limit"] = str(limit) + + # Send HTTP GET request. + + response = self.session.get(uri, params=query_parameters) + + return response From bfae697e6ce92e7f9e8345647964c53751894f5b Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:38:17 -0400 Subject: [PATCH 03/16] Create test_tilesets.py --- tests/test_tilesets.py | 302 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 302 insertions(+) create mode 100644 tests/test_tilesets.py diff --git a/tests/test_tilesets.py b/tests/test_tilesets.py new file mode 100644 index 0000000..bcd0e14 --- /dev/null +++ b/tests/test_tilesets.py @@ -0,0 +1,302 @@ +from mapbox.errors import ( + InvalidTilesetTypeError, + InvalidVisibilityError, + InvalidSortbyError, + InvalidLimitError +) + +from mapbox.services.tilesets import Tilesets + +from base64 import b64encode + +from pytest import raises + +from responses import ( + activate, + add, + GET +) + +USERNAME = b64encode(b"{\"u\":\"user\"}").decode("utf-8") +ACCESS_TOKEN = "pk.{}.test".format(USERNAME) + +def test_object_attributes(): + tilesets = Tilesets() + + assert tilesets.api_name + assert tilesets.api_version + assert tilesets.valid_tileset_types + assert tilesets.valid_visibilities + assert tilesets.valid_sortbys + assert tilesets.base_uri + +def test_validate_tileset_type(): + tilesets = Tilesets() + + # invalid value + + with raises(InvalidTilesetTypeError) as exception: + tileset_type = "invalid" + result = tilesets._validate_tileset_type(tileset_type) + + # valid values + + tileset_types = [ + "raster", + "vector" + ] + + for tileset_type in tileset_types: + result = tilesets._validate_tileset_type(tileset_type) + assert result == tileset_type + +def test_validate_visibility(): + tilesets = Tilesets() + + # invalid value + + with raises(InvalidVisibilityError) as exception: + visibility = "invalid" + result = tilesets._validate_visibility(visibility) + + # valid values + + visibilities = [ + "private", + "public" + ] + + for visibility in visibilities: + result = tilesets._validate_visibility(visibility) + assert result == visibility + +def test_validate_sortby(): + tilesets = Tilesets() + + # invalid value + + with raises(InvalidSortbyError) as exception: + sortby = "invalid" + result = tilesets._validate_sortby(sortby) + + # valid values + + sortbys = [ + "created", + "modified" + ] + + for sortby in sortbys: + result = tilesets._validate_sortby(sortby) + assert result == sortby + +def test_validate_limit(): + tilesets = Tilesets() + + # invalid value + + with raises(InvalidLimitError) as exception: + limit = 0 + result = tilesets._validate_limit(limit) + + # invalid value + + with raises(InvalidLimitError) as exception: + limit = 501 + result = tilesets._validate_limit(limit) + + # valid value + + limit = 100 + result = tilesets._validate_limit(limit) + assert result == limit + +@activate +def test_tilesets(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets() + assert response.status_code == 200 + +@activate +def test_tilesets_with_tileset_type(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=raster".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(tileset_type="raster") + assert response.status_code == 200 + + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(tileset_type="vector") + assert response.status_code == 200 + +@activate +def test_tilesets_with_visibility(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=private".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(visibility="private") + assert response.status_code == 200 + + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=public".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(visibility="public") + assert response.status_code == 200 + +@activate +def test_tilesets_with_sortby(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&sortby=created".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(sortby="created") + assert response.status_code == 200 + + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&sortby=modified".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(sortby="modified") + assert response.status_code == 200 + +@activate +def test_tilesets_with_limit(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&limit=500".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(limit=500) + assert response.status_code == 200 + +@activate +def test_tilesets_with_tileset_type_and_visibility(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector&visibility=public".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(tileset_type="vector", visibility="public") + assert response.status_code == 200 + +@activate +def test_tilesets_with_tileset_type_and_sortby(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector&sortby=created".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(tileset_type="vector", sortby="created") + assert response.status_code == 200 + +@activate +def test_tilesets_with_tileset_type_and_limit(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector&limit=500".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(tileset_type="vector", limit=500) + assert response.status_code == 200 + +@activate +def test_tilesets_with_visibility_and_sortby(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=public&sortby=created".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(visibility="public", sortby="created") + assert response.status_code == 200 + +@activate +def test_tilesets_with_visibility_and_limit(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=public&limit=500".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(visibility="public", limit=500) + assert response.status_code == 200 + +@activate +def test_tilesets_with_sortby_and_limit(): + add( + method=GET, + url="https://api.mapbox.com/tilesets/v1/user?access_token={}&sortby=created&limit=500".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + tilesets = Tilesets(access_token=ACCESS_TOKEN) + response = tilesets.list_tilesets(sortby="created", limit=500) + assert response.status_code == 200 From 61b92ce3aea933f08bffdd2b6febb333ad4bb418 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:40:47 -0400 Subject: [PATCH 04/16] Update README.rst --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index 9307e90..efc69db 100644 --- a/README.rst +++ b/README.rst @@ -56,6 +56,9 @@ Services - **Datasets V1** `examples <./docs/datasets.md#datasets>`__, `website `__ +- **Tilesets V1** `examples <./docs/tilesets.md#tilesets>`__, `website `__ + - Read metadata for raster and vector tilesets + - Manage editable collections of GeoJSON features - Persistent storage for custom geographic data From db012a02423efc6225fe4a6c077af458c26b2d11 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:42:12 -0400 Subject: [PATCH 05/16] Update index.rst --- docs/index.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/index.rst b/docs/index.rst index 76c0a0b..0f76db1 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -54,6 +54,9 @@ Services - **Datasets V1** `examples <./datasets.html#datasets>`__, `website `__ +- **Tilesets V1** `examples <./docs/tilesets.md#tilesets>`__, `website `__ + - Read metadata for raster and vector tilesets + - Manage editable collections of GeoJSON features - Persistent storage for custom geographic data From 906315712a8c93bc7096fd29453aa44e078beb98 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:42:59 -0400 Subject: [PATCH 06/16] Update index.rst --- docs/index.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 0f76db1..dd21cb6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -54,9 +54,6 @@ Services - **Datasets V1** `examples <./datasets.html#datasets>`__, `website `__ -- **Tilesets V1** `examples <./docs/tilesets.md#tilesets>`__, `website `__ - - Read metadata for raster and vector tilesets - - Manage editable collections of GeoJSON features - Persistent storage for custom geographic data @@ -71,6 +68,10 @@ Services - Retrieve TileJSON metadata for a tileset - Retrieve a single marker image without any background map +- **Tilesets V1** `examples <./docs/tilesets.md#tilesets>`__, `website `__ + + - Read metadata for raster and vector tilesets + Please note that there may be some lag between the release of new Mapbox web services and releases of this package. From 2917a85303e10325089bbe14054cd1cf4f400983 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:44:28 -0400 Subject: [PATCH 07/16] Update README.rst --- README.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index efc69db..fedd616 100644 --- a/README.rst +++ b/README.rst @@ -56,9 +56,6 @@ Services - **Datasets V1** `examples <./docs/datasets.md#datasets>`__, `website `__ -- **Tilesets V1** `examples <./docs/tilesets.md#tilesets>`__, `website `__ - - Read metadata for raster and vector tilesets - - Manage editable collections of GeoJSON features - Persistent storage for custom geographic data @@ -73,6 +70,10 @@ Services - Retrieve TileJSON metadata for a tileset - Retrieve a single marker image without any background map +- **Tilesets V1** `examples <./docs/tilesets.md#tilesets>`__, `website `__ + + - Read metadata for raster and vector tilesets + Please note that there may be some lag between the release of new Mapbox web services and releases of this package. From 41f86081c1de3c87f90f395c87b6886fa8f8285e Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Thu, 24 May 2018 22:47:33 -0400 Subject: [PATCH 08/16] Create tilesets.md --- docs/tilesets.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 docs/tilesets.md diff --git a/docs/tilesets.md b/docs/tilesets.md new file mode 100644 index 0000000..0dc1393 --- /dev/null +++ b/docs/tilesets.md @@ -0,0 +1,43 @@ +# Tilesets + +The `Tilesets` class provides access to the Mapbox Tilesets API. You can import it from either the `mapbox` module or the `mapbox.services.tilesets` module. + +__mapbox__: + +```python +>>> from mapbox import Tilesets +``` + +__mapbox.services.tilesets__: + +```python +>>> from mapbox.services.tilesets import Tilesets +``` + +See https://www.mapbox.com/api-documentation/#tilesets for general documentation of the API. + +Use of the Tilesets API requires an access token, which you should set in your environment. For more information, see the [access tokens](access_tokens.md) documentation. + +## Tilesets Methods + +The public method of the `Tilesets` class provides access to the Tilesets API and returns an instance of [`requests.Response`](http://docs.python-requests.org/en/latest/api/#requests.Response). + +## Usage: Listing Tilesets + +Instantiate `Tilesets`. + +```python +>>> tilesets = Tilesets() +``` + +Call the `list_tilesets` method, passing in values for optional arguments as necessary - `tileset_type`, `visibility`, `sortby`, and `limit`. + +```python +>>> response = tilesets.list_tilesets() +``` + +Evaluate whether the request succeeded, and retrieve the tileset object from the response object. + +```python +>>> if response.status_code == 200: +... tileset_object = response.get_json() From 4c34db08e1f1dd2c52548e825d33c7361f7d8819 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Fri, 25 May 2018 17:45:54 -0400 Subject: [PATCH 09/16] Update tilesets.md --- docs/tilesets.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tilesets.md b/docs/tilesets.md index 0dc1393..a275a2c 100644 --- a/docs/tilesets.md +++ b/docs/tilesets.md @@ -18,7 +18,7 @@ See https://www.mapbox.com/api-documentation/#tilesets for general documentation Use of the Tilesets API requires an access token, which you should set in your environment. For more information, see the [access tokens](access_tokens.md) documentation. -## Tilesets Methods +## Tilesets Method The public method of the `Tilesets` class provides access to the Tilesets API and returns an instance of [`requests.Response`](http://docs.python-requests.org/en/latest/api/#requests.Response). From 166a57f638983872c987500b7bb060db21314961 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Tue, 12 Jun 2018 21:29:33 -0400 Subject: [PATCH 10/16] Update test_tilesets.py --- tests/test_tilesets.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/test_tilesets.py b/tests/test_tilesets.py index bcd0e14..08ba5ee 100644 --- a/tests/test_tilesets.py +++ b/tests/test_tilesets.py @@ -122,7 +122,7 @@ def test_tilesets(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets() + response = tilesets.tilesets() assert response.status_code == 200 @activate @@ -136,7 +136,7 @@ def test_tilesets_with_tileset_type(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(tileset_type="raster") + response = tilesets.tilesets(tileset_type="raster") assert response.status_code == 200 add( @@ -148,7 +148,7 @@ def test_tilesets_with_tileset_type(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(tileset_type="vector") + response = tilesets.tilesets(tileset_type="vector") assert response.status_code == 200 @activate @@ -162,7 +162,7 @@ def test_tilesets_with_visibility(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(visibility="private") + response = tilesets.tilesets(visibility="private") assert response.status_code == 200 add( @@ -174,7 +174,7 @@ def test_tilesets_with_visibility(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(visibility="public") + response = tilesets.tilesets(visibility="public") assert response.status_code == 200 @activate @@ -188,7 +188,7 @@ def test_tilesets_with_sortby(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(sortby="created") + response = tilesets.tilesets(sortby="created") assert response.status_code == 200 add( @@ -200,7 +200,7 @@ def test_tilesets_with_sortby(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(sortby="modified") + response = tilesets.tilesets(sortby="modified") assert response.status_code == 200 @activate @@ -214,7 +214,7 @@ def test_tilesets_with_limit(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(limit=500) + response = tilesets.tilesets(limit=500) assert response.status_code == 200 @activate @@ -228,7 +228,7 @@ def test_tilesets_with_tileset_type_and_visibility(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(tileset_type="vector", visibility="public") + response = tilesets.tilesets(tileset_type="vector", visibility="public") assert response.status_code == 200 @activate @@ -242,7 +242,7 @@ def test_tilesets_with_tileset_type_and_sortby(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(tileset_type="vector", sortby="created") + response = tilesets.tilesets(tileset_type="vector", sortby="created") assert response.status_code == 200 @activate @@ -256,7 +256,7 @@ def test_tilesets_with_tileset_type_and_limit(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(tileset_type="vector", limit=500) + response = tilesets.tilesets(tileset_type="vector", limit=500) assert response.status_code == 200 @activate @@ -270,7 +270,7 @@ def test_tilesets_with_visibility_and_sortby(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(visibility="public", sortby="created") + response = tilesets.tilesets(visibility="public", sortby="created") assert response.status_code == 200 @activate @@ -284,7 +284,7 @@ def test_tilesets_with_visibility_and_limit(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(visibility="public", limit=500) + response = tilesets.tilesets(visibility="public", limit=500) assert response.status_code == 200 @activate @@ -298,5 +298,5 @@ def test_tilesets_with_sortby_and_limit(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.list_tilesets(sortby="created", limit=500) + response = tilesets.tilesets(sortby="created", limit=500) assert response.status_code == 200 From 4ce0b40281a27310f900abf23227bd325170035d Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Tue, 12 Jun 2018 21:30:26 -0400 Subject: [PATCH 11/16] Update tilesets.py --- mapbox/services/tilesets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mapbox/services/tilesets.py b/mapbox/services/tilesets.py index 4b4a604..c40ef04 100644 --- a/mapbox/services/tilesets.py +++ b/mapbox/services/tilesets.py @@ -102,8 +102,8 @@ def _validate_limit(self, limit): return limit - def list_tilesets(self, tileset_type=None, visibility=None, - sortby=None, limit=None): + def tilesets(self, tileset_type=None, visibility=None, + sortby=None, limit=None): """Lists all tilesets for an account. tileset_type : str, optional From d67ce0a203e0e6d297881541a01f9550dff7f9f8 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Tue, 10 Jul 2018 19:18:36 -0400 Subject: [PATCH 12/16] Update index.rst --- docs/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/index.rst b/docs/index.rst index dd21cb6..707ca0e 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -120,6 +120,7 @@ Documentation mapmatching.md static_style.md tilequery.md + tilesets.md maps.md api/mapbox.rst api/mapbox.services.rst From 1d0756c791a4b5df1dd053fda02e24bc257f4ea3 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Tue, 10 Jul 2018 19:19:47 -0400 Subject: [PATCH 13/16] Update errors.py --- mapbox/errors.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/mapbox/errors.py b/mapbox/errors.py index 4c18eae..e481f08 100644 --- a/mapbox/errors.py +++ b/mapbox/errors.py @@ -81,6 +81,50 @@ class InvalidRowError(ValidationError): class InvalidFileFormatError(ValidationError): pass +class InvalidTilesetTypeError(ValidationError): + """InvalidTilesetTypeError + + Parameters + ---------- + message : str, optional + A human-readable string describing the error. + """ + + pass + + +class InvalidVisibilityError(ValidationError): + """InvalidVisibilityError + + Parameters + ---------- + message : str, optional + A human-readable string describing the error. + """ + + pass + + +class InvalidSortbyError(ValidationError): + """InvalidSortbyError + + Parameters + ---------- + message : str, optional + A human-readable string describing the error. + """ + + pass + + +class InvalidLimitError(ValidationError): + """InvalidLimitError + + Parameters + ---------- + message : str, optional + A human-readable string describing the error. + """ class InvalidFeatureFormatError(ValidationError): pass From 1d4a36419509af5c4a5b604ce46479592ee59b9d Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Tue, 10 Jul 2018 19:20:33 -0400 Subject: [PATCH 14/16] Update tilesets.py --- mapbox/services/tilesets.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mapbox/services/tilesets.py b/mapbox/services/tilesets.py index c40ef04..7be75e6 100644 --- a/mapbox/services/tilesets.py +++ b/mapbox/services/tilesets.py @@ -1,3 +1,5 @@ +"""The Tilesets class provides access to Mapbox's Tilesets API.""" + from mapbox.errors import ( InvalidTilesetTypeError, InvalidVisibilityError, From a58a11ad1a2927bd7f5e7a2531011080497b54b9 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Tue, 10 Jul 2018 19:22:34 -0400 Subject: [PATCH 15/16] Update test_tilesets.py --- tests/test_tilesets.py | 207 ++++++++++++++++++++++------------------- 1 file changed, 109 insertions(+), 98 deletions(-) diff --git a/tests/test_tilesets.py b/tests/test_tilesets.py index 08ba5ee..3ae6037 100644 --- a/tests/test_tilesets.py +++ b/tests/test_tilesets.py @@ -9,7 +9,10 @@ from base64 import b64encode -from pytest import raises +from pytest import ( + mark, + raises +) from responses import ( activate, @@ -17,9 +20,11 @@ GET ) + USERNAME = b64encode(b"{\"u\":\"user\"}").decode("utf-8") ACCESS_TOKEN = "pk.{}.test".format(USERNAME) + def test_object_attributes(): tilesets = Tilesets() @@ -30,92 +35,75 @@ def test_object_attributes(): assert tilesets.valid_sortbys assert tilesets.base_uri -def test_validate_tileset_type(): - tilesets = Tilesets() - # invalid value +def test_validate_tileset_type_invalid(): + tilesets = Tilesets() with raises(InvalidTilesetTypeError) as exception: tileset_type = "invalid" result = tilesets._validate_tileset_type(tileset_type) - # valid values - tileset_types = [ - "raster", - "vector" - ] +@mark.parametrize("tileset_type", ["raster", "vector"]) +def test_validate_tileset_type_valid(tileset_type): + tilesets = Tilesets() + result = tilesets._validate_tileset_type(tileset_type) + assert result == tileset_type - for tileset_type in tileset_types: - result = tilesets._validate_tileset_type(tileset_type) - assert result == tileset_type -def test_validate_visibility(): +def test_validate_visibility_invalid(): tilesets = Tilesets() - # invalid value - with raises(InvalidVisibilityError) as exception: visibility = "invalid" result = tilesets._validate_visibility(visibility) - # valid values - visibilities = [ - "private", - "public" - ] +@mark.parametrize("visibility", ["private", "public"]) +def test_validate_visibility_valid(visibility): + tilesets = Tilesets() + result = tilesets._validate_visibility(visibility) + assert result == visibility - for visibility in visibilities: - result = tilesets._validate_visibility(visibility) - assert result == visibility -def test_validate_sortby(): +def test_validate_sortby_invalid(): tilesets = Tilesets() - # invalid value - with raises(InvalidSortbyError) as exception: sortby = "invalid" result = tilesets._validate_sortby(sortby) - # valid values - - sortbys = [ - "created", - "modified" - ] - - for sortby in sortbys: - result = tilesets._validate_sortby(sortby) - assert result == sortby -def test_validate_limit(): +@mark.parametrize("sortby", ["created", "modified"]) +def test_validate_sortby_valid(sortby): tilesets = Tilesets() + result = tilesets._validate_sortby(sortby) + assert result == sortby - # invalid value - with raises(InvalidLimitError) as exception: - limit = 0 - result = tilesets._validate_limit(limit) - - # invalid value +@mark.parametrize("limit", [0, 501]) +def test_validate_limit_invalid(limit): + tilesets = Tilesets() with raises(InvalidLimitError) as exception: - limit = 501 result = tilesets._validate_limit(limit) - # valid value - limit = 100 +@mark.parametrize("limit", [1, 250, 500]) +def test_validate_limit_valid(limit): + tilesets = Tilesets() result = tilesets._validate_limit(limit) assert result == limit + @activate def test_tilesets(): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN), match_querystring=True, body="{\"key\": \"value\"}", status=200 @@ -125,103 +113,97 @@ def test_tilesets(): response = tilesets.tilesets() assert response.status_code == 200 + @activate -def test_tilesets_with_tileset_type(): +@mark.parametrize("tileset_type", ["raster", "vector"]) +def test_tilesets_with_tileset_type(tileset_type): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=raster".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type={}".format(tileset_type), match_querystring=True, body="{\"key\": \"value\"}", status=200 ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(tileset_type="raster") + response = tilesets.tilesets(tileset_type=tileset_type) assert response.status_code == 200 - add( - method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector".format(ACCESS_TOKEN), - match_querystring=True, - body="{\"key\": \"value\"}", - status=200 - ) - - tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(tileset_type="vector") - assert response.status_code == 200 @activate -def test_tilesets_with_visibility(): +@mark.parametrize("visibility", ["private", "public"]) +def test_tilesets_with_visibility(visibility): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=private".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&visibility={}".format(visibility), match_querystring=True, body="{\"key\": \"value\"}", status=200 ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(visibility="private") + response = tilesets.tilesets(visibility=visibility) assert response.status_code == 200 - add( - method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=public".format(ACCESS_TOKEN), - match_querystring=True, - body="{\"key\": \"value\"}", - status=200 - ) - - tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(visibility="public") - assert response.status_code == 200 @activate -def test_tilesets_with_sortby(): +@mark.parametrize("sortby", ["created", "modified"]) +def test_tilesets_with_sortby(sortby): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&sortby=created".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&sortby={}".format(sortby), match_querystring=True, body="{\"key\": \"value\"}", status=200 ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(sortby="created") + response = tilesets.tilesets(sortby=sortby) assert response.status_code == 200 - add( - method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&sortby=modified".format(ACCESS_TOKEN), - match_querystring=True, - body="{\"key\": \"value\"}", - status=200 - ) - - tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(sortby="modified") - assert response.status_code == 200 @activate -def test_tilesets_with_limit(): +@mark.parametrize("limit", [1, 250, 500]) +def test_tilesets_with_limit(limit): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&limit=500".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&limit={}".format(limit), match_querystring=True, body="{\"key\": \"value\"}", status=200 ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(limit=500) + response = tilesets.tilesets(limit=limit) assert response.status_code == 200 + @activate def test_tilesets_with_tileset_type_and_visibility(): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector&visibility=public".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=vector" + + "&visibility=public", match_querystring=True, body="{\"key\": \"value\"}", status=200 @@ -231,11 +213,17 @@ def test_tilesets_with_tileset_type_and_visibility(): response = tilesets.tilesets(tileset_type="vector", visibility="public") assert response.status_code == 200 + @activate def test_tilesets_with_tileset_type_and_sortby(): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector&sortby=created".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=vector" + + "&sortby=created", match_querystring=True, body="{\"key\": \"value\"}", status=200 @@ -245,11 +233,17 @@ def test_tilesets_with_tileset_type_and_sortby(): response = tilesets.tilesets(tileset_type="vector", sortby="created") assert response.status_code == 200 + @activate def test_tilesets_with_tileset_type_and_limit(): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&type=vector&limit=500".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=vector" + + "&limit=500", match_querystring=True, body="{\"key\": \"value\"}", status=200 @@ -259,11 +253,17 @@ def test_tilesets_with_tileset_type_and_limit(): response = tilesets.tilesets(tileset_type="vector", limit=500) assert response.status_code == 200 + @activate def test_tilesets_with_visibility_and_sortby(): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=public&sortby=created".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&visibility=public" + + "&sortby=created", match_querystring=True, body="{\"key\": \"value\"}", status=200 @@ -273,11 +273,17 @@ def test_tilesets_with_visibility_and_sortby(): response = tilesets.tilesets(visibility="public", sortby="created") assert response.status_code == 200 + @activate def test_tilesets_with_visibility_and_limit(): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&visibility=public&limit=500".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&visibility=public" + + "&limit=500", match_querystring=True, body="{\"key\": \"value\"}", status=200 @@ -287,11 +293,16 @@ def test_tilesets_with_visibility_and_limit(): response = tilesets.tilesets(visibility="public", limit=500) assert response.status_code == 200 + @activate def test_tilesets_with_sortby_and_limit(): add( method=GET, - url="https://api.mapbox.com/tilesets/v1/user?access_token={}&sortby=created&limit=500".format(ACCESS_TOKEN), + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user?access_token={}".format(ACCESS_TOKEN) + + "&sortby=created" + + "&limit=500", match_querystring=True, body="{\"key\": \"value\"}", status=200 From f789ec278b9cf27b8b21770a243b0a71779200e3 Mon Sep 17 00:00:00 2001 From: Igor Serebryany Date: Tue, 30 Jul 2019 12:43:26 -0700 Subject: [PATCH 16/16] rename `.tilesets` to `.list` for consistency other services in this SDK use `.list()`, in particular `Datasets` and `Uploads` --- docs/tilesets.md | 4 ++-- mapbox/services/tilesets.py | 4 ++-- tests/test_tilesets.py | 46 ++++++++++++++++++------------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/docs/tilesets.md b/docs/tilesets.md index a275a2c..eec0a60 100644 --- a/docs/tilesets.md +++ b/docs/tilesets.md @@ -30,10 +30,10 @@ Instantiate `Tilesets`. >>> tilesets = Tilesets() ``` -Call the `list_tilesets` method, passing in values for optional arguments as necessary - `tileset_type`, `visibility`, `sortby`, and `limit`. +Call the `list` method, passing in values for optional arguments as necessary - `tileset_type`, `visibility`, `sortby`, and `limit`. ```python ->>> response = tilesets.list_tilesets() +>>> response = tilesets.list() ``` Evaluate whether the request succeeded, and retrieve the tileset object from the response object. diff --git a/mapbox/services/tilesets.py b/mapbox/services/tilesets.py index 7be75e6..cefb2c1 100644 --- a/mapbox/services/tilesets.py +++ b/mapbox/services/tilesets.py @@ -62,7 +62,7 @@ def base_uri(self): self.host, self.api_name, self.api_version - ) + ) def _validate_tileset_type(self, tileset_type): """Validates tileset type, raising error if invalid.""" @@ -104,7 +104,7 @@ def _validate_limit(self, limit): return limit - def tilesets(self, tileset_type=None, visibility=None, + def list(self, tileset_type=None, visibility=None, sortby=None, limit=None): """Lists all tilesets for an account. diff --git a/tests/test_tilesets.py b/tests/test_tilesets.py index 3ae6037..634f7d7 100644 --- a/tests/test_tilesets.py +++ b/tests/test_tilesets.py @@ -10,7 +10,7 @@ from base64 import b64encode from pytest import ( - mark, + mark, raises ) @@ -97,7 +97,7 @@ def test_validate_limit_valid(limit): @activate -def test_tilesets(): +def test_list(): add( method=GET, url="https://api.mapbox.com" + @@ -110,13 +110,13 @@ def test_tilesets(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets() + response = tilesets.list() assert response.status_code == 200 @activate @mark.parametrize("tileset_type", ["raster", "vector"]) -def test_tilesets_with_tileset_type(tileset_type): +def test_list_with_tileset_type(tileset_type): add( method=GET, url="https://api.mapbox.com" + @@ -130,13 +130,13 @@ def test_tilesets_with_tileset_type(tileset_type): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(tileset_type=tileset_type) + response = tilesets.list(tileset_type=tileset_type) assert response.status_code == 200 @activate @mark.parametrize("visibility", ["private", "public"]) -def test_tilesets_with_visibility(visibility): +def test_list_with_visibility(visibility): add( method=GET, url="https://api.mapbox.com" + @@ -150,13 +150,13 @@ def test_tilesets_with_visibility(visibility): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(visibility=visibility) + response = tilesets.list(visibility=visibility) assert response.status_code == 200 @activate @mark.parametrize("sortby", ["created", "modified"]) -def test_tilesets_with_sortby(sortby): +def test_list_with_sortby(sortby): add( method=GET, url="https://api.mapbox.com" + @@ -170,13 +170,13 @@ def test_tilesets_with_sortby(sortby): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(sortby=sortby) + response = tilesets.list(sortby=sortby) assert response.status_code == 200 @activate @mark.parametrize("limit", [1, 250, 500]) -def test_tilesets_with_limit(limit): +def test_list_with_limit(limit): add( method=GET, url="https://api.mapbox.com" + @@ -190,12 +190,12 @@ def test_tilesets_with_limit(limit): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(limit=limit) + response = tilesets.list(limit=limit) assert response.status_code == 200 @activate -def test_tilesets_with_tileset_type_and_visibility(): +def test_list_with_tileset_type_and_visibility(): add( method=GET, url="https://api.mapbox.com" + @@ -210,12 +210,12 @@ def test_tilesets_with_tileset_type_and_visibility(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(tileset_type="vector", visibility="public") + response = tilesets.list(tileset_type="vector", visibility="public") assert response.status_code == 200 @activate -def test_tilesets_with_tileset_type_and_sortby(): +def test_list_with_tileset_type_and_sortby(): add( method=GET, url="https://api.mapbox.com" + @@ -230,12 +230,12 @@ def test_tilesets_with_tileset_type_and_sortby(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(tileset_type="vector", sortby="created") + response = tilesets.list(tileset_type="vector", sortby="created") assert response.status_code == 200 @activate -def test_tilesets_with_tileset_type_and_limit(): +def test_list_with_tileset_type_and_limit(): add( method=GET, url="https://api.mapbox.com" + @@ -250,12 +250,12 @@ def test_tilesets_with_tileset_type_and_limit(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(tileset_type="vector", limit=500) + response = tilesets.list(tileset_type="vector", limit=500) assert response.status_code == 200 @activate -def test_tilesets_with_visibility_and_sortby(): +def test_list_with_visibility_and_sortby(): add( method=GET, url="https://api.mapbox.com" + @@ -270,12 +270,12 @@ def test_tilesets_with_visibility_and_sortby(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(visibility="public", sortby="created") + response = tilesets.list(visibility="public", sortby="created") assert response.status_code == 200 @activate -def test_tilesets_with_visibility_and_limit(): +def test_list_with_visibility_and_limit(): add( method=GET, url="https://api.mapbox.com" + @@ -290,12 +290,12 @@ def test_tilesets_with_visibility_and_limit(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(visibility="public", limit=500) + response = tilesets.list(visibility="public", limit=500) assert response.status_code == 200 @activate -def test_tilesets_with_sortby_and_limit(): +def test_list_with_sortby_and_limit(): add( method=GET, url="https://api.mapbox.com" + @@ -309,5 +309,5 @@ def test_tilesets_with_sortby_and_limit(): ) tilesets = Tilesets(access_token=ACCESS_TOKEN) - response = tilesets.tilesets(sortby="created", limit=500) + response = tilesets.list(sortby="created", limit=500) assert response.status_code == 200