From 625b52ef930feeff64a261bf62ce60ec3c34f9ce Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:15:35 -0400 Subject: [PATCH 1/8] Update README.md --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 0d8d8f7..1821168 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ $ mapbox ... * [surface](#surface) * [upload](#upload) * [datasets](#datasets) +* [tilesets](#tilesets) For any command that takes waypoints or features as an input you can either specify: @@ -462,6 +463,28 @@ Options: --help Show this message and exit. ``` +### tilesets +``` +Usage: mapbox tilesets [OPTIONS] + + The Mapbox Tilesets API supports reading metadata for raster and vector + tilesets. + + mapbox tilesets + + An access token is required. See "mapbox --help". + +Options: + -t, --tileset-type [raster|vector] + Filter results by type + -v, --visibility [private|public] + Filter results by visibility + -s, --sortby [created|modified] + Sort results by timestamp + -l, --limit TEXT Limit number of results (pagination) + --help Show this message and exit. + ``` + ## Alternative command syntax When saving a fraction of a second matters you can call the mapboxcli module From d20b5556b723b319bd8cb026981f69b128f197a5 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:16:31 -0400 Subject: [PATCH 2/8] Create tilesets.md --- examples/tilesets.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/tilesets.md diff --git a/examples/tilesets.md b/examples/tilesets.md new file mode 100644 index 0000000..73e3ca5 --- /dev/null +++ b/examples/tilesets.md @@ -0,0 +1,21 @@ +# Tilesets + +Access the Mapbox Tilesets API from the command line. + +# Listing Tilesets + +To list all tilesets for an account, run `mapbox tilesets`. + +``` +mapbox tilesets +``` + +Use the `--type`, `--visibility`, `--sortby`, and `--limit` options for additional functionality. + +# Viewing Help + +For help, use the `--help` option. + +``` +mapbox tilesets --help +``` From 53fe793228135b9d55ca3ab10d78c328774a679a Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:17:02 -0400 Subject: [PATCH 3/8] Update README.md --- examples/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/README.md b/examples/README.md index b39294a..c7572f5 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,3 +4,4 @@ * [Staticmaps](static_maps.md) * [Surface](surface.md) * [Geocoding](geocoding.md) +* [Tilesets](tilesets.md) From 84200add43e19063d82ab6bdb280fc593d128040 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:19:32 -0400 Subject: [PATCH 4/8] Create test_tilesets.py --- tests/test_tilesets.py | 455 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 455 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..04e3c9f --- /dev/null +++ b/tests/test_tilesets.py @@ -0,0 +1,455 @@ +import base64 + +from mapboxcli.scripts.cli import main_group + +from click.testing import CliRunner + +import responses + + +USERNAME = base64.b64encode(b"{\"u\":\"user\"}").decode("utf-8") +ACCESS_TOKEN = "pk.{}.test".format(USERNAME) + + +def test_cli_tilesets_validation_error(): + # invalid --limit + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--limit", 1000 + ] + ) + + assert result.exit_code != 0 + assert "Error" in result.output + + +@responses.activate +def test_cli_tilesets_server_error(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=500 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets" + ] + ) + + assert result.exit_code != 0 + assert "Error" in result.output + + +@responses.activate +def test_cli_tilesets(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN), + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_tileset_type(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=raster", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--tileset-type", "raster" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_visibility(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&visibility=private", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--visibility", "private" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_sortby(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&sortby=created", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--sortby", "created" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_limit(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&limit=500", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--limit", "500" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_tileset_type_and_visibility(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=raster" + + "&visibility=private", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--tileset-type", "raster", + "--visibility", "private" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_tileset_type_and_sortby(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=raster" + + "&sortby=created", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--tileset-type", "raster", + "--sortby", "created" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_tileset_type_and_limit(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=raster" + + "&limit=500", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--tileset-type", "raster", + "--limit", "500" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_visibility_and_sortby(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&visibility=private" + + "&sortby=created", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--visibility", "private", + "--sortby", "created" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_visibility_and_limit(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&visibility=private" + + "&limit=500", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--visibility", "private", + "--limit", "500" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_sortby_and_limit(): + responses.add( + method=responses.GET, + 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 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--sortby", "created", + "--limit", "500" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_tileset_type_visibility_sortby_and_limit(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=raster" + + "&visibility=private" + + "&sortby=created" + + "&limit=500", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "--tileset-type", "raster", + "--visibility", "private", + "--sortby", "created", + "--limit", "500" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" + + +@responses.activate +def test_cli_tilesets_with_short_options(): + responses.add( + method=responses.GET, + url="https://api.mapbox.com" + + "/tilesets/v1" + + "/user" + + "?access_token={}".format(ACCESS_TOKEN) + + "&type=raster" + + "&visibility=private" + + "&sortby=created" + + "&limit=500", + match_querystring=True, + body="{\"key\": \"value\"}", + status=200 + ) + + runner = CliRunner() + + result = runner.invoke( + main_group, + [ + "--access-token", "{}".format(ACCESS_TOKEN), + "tilesets", + "-t", "raster", + "-v", "private", + "-s", "created", + "-l", "500" + ] + ) + + assert result.exit_code == 0 + assert result.output == "{\"key\": \"value\"}" + "\n" From ba267d7ff14a36984420bc9922b2770b443a1935 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:22:19 -0400 Subject: [PATCH 5/8] Update cli.py --- mapboxcli/scripts/cli.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mapboxcli/scripts/cli.py b/mapboxcli/scripts/cli.py index 29661e2..245de8f 100644 --- a/mapboxcli/scripts/cli.py +++ b/mapboxcli/scripts/cli.py @@ -12,8 +12,17 @@ import mapboxcli from mapboxcli.compat import configparser from mapboxcli.scripts import ( - config, geocoding, directions, distance, mapmatching, uploads, static, - surface, datasets) + config, + geocoding, + directions, + distance, + mapmatching, + uploads, + static, + surface, + datasets, + tilesets +) def configure_logging(verbosity): @@ -105,3 +114,4 @@ def main_group(ctx, verbose, quiet, access_token, config): main_group.add_command(static.staticmap) main_group.add_command(surface.surface) main_group.add_command(datasets.datasets) +main_group.add_command(tilesets.tilesets) From 5d47ced5c353445a853fc2ff5c1fa1fa8d1d45b6 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:24:08 -0400 Subject: [PATCH 6/8] Create tilesets.py --- mapboxcli/scripts/tilesets.py | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 mapboxcli/scripts/tilesets.py diff --git a/mapboxcli/scripts/tilesets.py b/mapboxcli/scripts/tilesets.py new file mode 100644 index 0000000..afcccb6 --- /dev/null +++ b/mapboxcli/scripts/tilesets.py @@ -0,0 +1,67 @@ +import json + +import click +import cligj + +import mapbox +from mapboxcli.errors import MapboxCLIException + +@click.command( + short_help="List all tilesets for an account" +) + +@click.option( + "--tileset-type", + "-t", + type=click.Choice(mapbox.Tilesets.valid_tileset_types), + help="Filter results by type" +) + +@click.option( + "--visibility", + "-v", + type=click.Choice(mapbox.Tilesets.valid_visibilities), + help="Filter results by visibility" +) + +@click.option( + "--sortby", + "-s", + type=click.Choice(mapbox.Tilesets.valid_sortbys), + help="Sort results by timestamp" +) + +@click.option( + "--limit", + "-l", + help="Limit number of results (pagination)" +) + +@click.pass_context +def tilesets(ctx, tileset_type, visibility, sortby, limit): + """The Mapbox Tilesets API supports reading + metadata for raster and vector tilesets. + + mapbox tilesets + + An access token is required. See "mapbox --help". + """ + + access_token = (ctx.obj and ctx.obj.get("access_token")) or None + + service = mapbox.Tilesets(access_token=access_token) + + try: + res = service.tilesets( + tileset_type=tileset_type, + visibility=visibility, + sortby=sortby, + limit=int(limit) if limit is not None else None + ) + except mapbox.errors.ValidationError as exc: + raise click.BadParameter(str(exc)) + + if res.status_code == 200: + click.echo(json.dumps(res.json())) + else: + raise MapboxCLIException(res.text.strip()) From 94cc931cbdf56fb9fd41f2dbf75c7b4701c86d99 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:43:11 -0400 Subject: [PATCH 7/8] Update README.md --- examples/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index f799ca0..94ab18a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,6 +2,5 @@ * [Geocoding](geocoding.md) * [Mapmatching](mapmatching.md) * [Staticmaps](static_maps.md) -* [Surface](surface.md) * [Geocoding](geocoding.md) * [Tilesets](tilesets.md) From 0d38a24a507b84133e8c2acab769c04f7e6b4134 Mon Sep 17 00:00:00 2001 From: critical-path <37241479+critical-path@users.noreply.github.com> Date: Sat, 16 Jun 2018 11:44:46 -0400 Subject: [PATCH 8/8] Update cli.py --- mapboxcli/scripts/cli.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mapboxcli/scripts/cli.py b/mapboxcli/scripts/cli.py index 0d22d1d..2282a4f 100644 --- a/mapboxcli/scripts/cli.py +++ b/mapboxcli/scripts/cli.py @@ -15,11 +15,9 @@ config, geocoding, directions, - distance, mapmatching, uploads, static, - surface, datasets, tilesets )