Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding better url construction and dryrun mode #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions tests/test_cli_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ def test_cli_add_source(mock_request_post, MockResponse):
)


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.post")
def test_cli_add_source_dryrun(mock_request_post, MockResponse):
message = {"id": "mapbox://tileset-source/test-user/hello-world"}
mock_request_post.return_value = MockResponse(message, status_code=200)
runner = CliRunner()
result = runner.invoke(
add_source,
["--dryrun", "test-user", "hello-world", "tests/fixtures/valid.ldgeojson"],
)
assert not mock_request_post.called

assert result.exit_code == 0

assert (
result.output
== """[DRYRUN] Adding 1 features to https://api.mapbox.com/tilesets/v1/sources/test-user/hello-world\n"""
)


@pytest.mark.usefixtures("token_environ")
@mock.patch("requests.get")
def test_cli_view_source(mock_request_get, MockResponse):
Expand Down
28 changes: 19 additions & 9 deletions tilesets/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,33 +305,43 @@ def validate_source(features):
@click.argument("id", required=True, type=str)
@cligj.features_in_arg
@click.option("--no-validation", is_flag=True, help="Bypass source file validation")
@click.option("--dryrun", is_flag=True, default=False, help="Don't upload")
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
@click.pass_context
def add_source(ctx, username, id, features, no_validation, token=None, indent=None):
def add_source(
ctx, username, id, features, no_validation, dryrun, token=None, indent=None
):
"""Create/add a tileset source

tilesets add-source <username> <id> <path/to/source/data>
"""
mapbox_api = _get_api()
mapbox_token = _get_token(token)

url = (
f"{mapbox_api}/tilesets/v1/sources/{username}/{id}?access_token={mapbox_token}"
)
with BytesIO() as io:
for feature in features:
url = f"{mapbox_api}/tilesets/v1/sources/{username}/{id}?access_token={mapbox_token}"
for count, feature in enumerate(features, 1):
if not no_validation:
utils.validate_geojson(feature)

io.write((json.dumps(feature) + "\n").encode("utf-8"))

io.seek(0)

r = requests.post(url, files={"file": ("tileset-source", io)})
if not dryrun:
r = requests.post(url, files={"file": ("tileset-source", io)})

if r.status_code == 200:
click.echo(json.dumps(r.json(), indent=indent))
else:
raise errors.TilesetsError(r.text)
if r.status_code == 200:
click.echo(json.dumps(r.json(), indent=indent))
else:
raise errors.TilesetsError(r.text)
else:
click.echo(
f"[DRYRUN] Adding {count} features to {mapbox_api}/tilesets/v1/sources/{username}/{id}",
err=True,
)


@cli.command("view-source")
Expand Down