def add_source(ctx, username, id, features, no_validation, 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) with BytesIO() as io: for feature in features: url = f"{mapbox_api}/tilesets/v1/sources/{username}/{id}?access_token={mapbox_token}" 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 r.status_code == 200: click.echo(json.dumps(r.json(), indent=indent)) else: raise errors.TilesetsError(r.text)
def view_source(username, id, token=None, indent=None): """View a Tileset Source's information tilesets view-source <username> <id> """ mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/sources/{1}/{2}?access_token={3}".format( mapbox_api, username, id, mapbox_token) r = requests.get(url) if r.status_code == 200: click.echo(json.dumps(r.json(), indent=indent)) else: raise errors.TilesetsError(r.text)
def view_recipe(tileset, token=None, indent=None): """View a tileset's recipe JSON tilesets view-recipe <tileset_id> """ mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/{1}/recipe?access_token={2}".format( mapbox_api, tileset, mapbox_token) r = requests.get(url) if r.status_code == 200: click.echo(json.dumps(r.json(), indent=indent)) else: raise errors.TilesetsError(r.text)
def delete_source(username, id, token=None): """Delete a Tileset Source + all of its files. tilesets delete-source <username> <id> """ mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/sources/{1}/{2}?access_token={3}".format( mapbox_api, username, id, mapbox_token) r = requests.delete(url) if r.status_code == 201: click.echo("Source deleted.") else: raise errors.TilesetsError(r.text)
def list_sources(username, token=None): """List all Tileset Sources for an account. Response is an un-ordered array of sources. tilesets list-sources <username> """ mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/sources/{1}?access_token={2}".format( mapbox_api, username, mapbox_token) r = requests.get(url) if r.status_code == 200: for source in r.json(): click.echo(source["id"]) else: raise errors.TilesetsError(r.text)
def delete_source(username, id, force, token=None): """Delete a Tileset Source + all of its files. tilesets delete-source <username> <id> """ if not force: click.confirm("Are you sure you want to delete {0} {1}?".format( username, id), abort=True) mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/sources/{1}/{2}?access_token={3}".format( mapbox_api, username, id, mapbox_token) r = requests.delete(url) if r.status_code == 201: click.echo("Source deleted.") else: raise errors.TilesetsError(r.text)
def update_recipe(tileset, recipe, token=None, indent=None): """Update a Recipe JSON document for a particular tileset tilesets update-recipe <tileset_id> <path_to_recipe> """ mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/{1}/recipe?access_token={2}".format( mapbox_api, tileset, mapbox_token) with open(recipe) as json_recipe: recipe_json = json.load(json_recipe) r = requests.patch(url, json=recipe_json) if r.status_code == 201: click.echo("Updated recipe.", err=True) click.echo(r.text) else: raise errors.TilesetsError(r.text)
def publish(tileset, token=None, indent=None): """Publish your tileset. tilesets publish <tileset_id> """ mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/{1}/publish?access_token={2}".format( mapbox_api, tileset, mapbox_token) r = requests.post(url) if r.status_code == 200: click.echo(json.dumps(r.json(), indent=indent)) click.echo( f"You can view the status of your tileset with the `tilesets status {tileset}` command.", err=True, ) else: raise errors.TilesetsError(f"{r.text}")
def list(username, verbose, token=None, indent=None): """List all tilesets for an account. By default the response is a simple list of tileset IDs. If you would like an array of all tileset's information, use the --versbose flag. tilests list <username> """ mapbox_api = _get_api() mapbox_token = _get_token(token) url = "{0}/tilesets/v1/{1}?access_token={2}".format( mapbox_api, username, mapbox_token) r = requests.get(url) if r.status_code == 200: if verbose: for tileset in r.json(): click.echo(json.dumps(tileset, indent=indent)) else: for tileset in r.json(): click.echo(tileset["id"]) else: raise errors.TilesetsError(r.text)
def add_source(ctx, username, id, features, no_validation, 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 tempfile.TemporaryFile() as file: for feature in features: if not no_validation: utils.validate_geojson(feature) file.write((json.dumps(feature) + "\n").encode("utf-8")) file.seek(0) m = MultipartEncoder(fields={"file": ("file", file)}) resp = requests.post( url, data=m, headers={ "Content-Disposition": "multipart/form-data", "Content-type": m.content_type, }, ) if resp.status_code == 200: click.echo(json.dumps(resp.json(), indent=indent)) else: raise errors.TilesetsError(resp.text)