Skip to content

Commit

Permalink
Merge pull request mapbox#271 from mapbox/uploads-flags
Browse files Browse the repository at this point in the history
add bypass flag for uploads service
  • Loading branch information
mapsam authored Mar 6, 2019
2 parents fa4a857 + 4cac56a commit 72d19db
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
8 changes: 6 additions & 2 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Changes
=======

0.17.2 (2019-11-30)
0.18.0 (2019-03-05)
-------------------
- Added support for bypass_mbtiles_validation flag in the Uploads API (#271)

0.17.2 (2018-11-30)
-------------------

Bug fixes:
Expand Down Expand Up @@ -41,7 +45,7 @@ New features:
- Added support for Directions v5 (#203).
- The Mapbox Distance API is no more and the distance module has been deleted
(#232). Users must use the DirectionsMatrix introduced in 0.15.
- Project documentation is now on Read the Docs:
- Project documentation is now on Read the Docs:
https://mapbox-mapbox.readthedocs-hosted.com/en/latest/.

0.15.1 (2018-01-16)
Expand Down
2 changes: 1 addition & 1 deletion mapbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# mapbox
__version__ = "0.17.2"
__version__ = "0.18.0"

from .services.datasets import Datasets
from .services.directions import Directions
Expand Down
15 changes: 12 additions & 3 deletions mapbox/services/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def stage(self, fileobj, creds=None, callback=None):

return creds['url']

def create(self, stage_url, tileset, name=None, patch=False):
def create(self, stage_url, tileset, name=None, patch=False, bypass=False):
"""Create a tileset
Note: this step is refered to as "upload" in the API docs;
Expand Down Expand Up @@ -144,6 +144,9 @@ def create(self, stage_url, tileset, name=None, patch=False):
patch: bool
Optional patch mode which requires a flag on the owner's
account.
bypass: bool
Optional bypass validation mode for MBTiles which requires
a flag on the owner's account.
Returns
-------
Expand All @@ -158,6 +161,9 @@ def create(self, stage_url, tileset, name=None, patch=False):
if patch:
msg['patch'] = patch

if bypass:
msg['bypass_mbtiles_validation'] = bypass

msg['name'] = name if name else _name

uri = URITemplate(self.baseuri + '/{username}').expand(
Expand Down Expand Up @@ -246,7 +252,7 @@ def status(self, upload, account=None, username=None):
self.handle_http_error(resp)
return resp

def upload(self, fileobj, tileset, name=None, patch=False, callback=None):
def upload(self, fileobj, tileset, name=None, patch=False, callback=None, bypass=False):
"""Upload data and create a Mapbox tileset
Effectively replicates the Studio upload feature. Returns a
Expand All @@ -265,6 +271,9 @@ def upload(self, fileobj, tileset, name=None, patch=False, callback=None):
patch: bool
Optional patch mode which requires a flag on the owner's
account.
bypass: bool
Optional bypass validation mode for MBTiles which requires
a flag on the owner's account.
callback: func
A function that takes a number of bytes processed as its
sole argument. May be used with a progress bar.
Expand All @@ -275,4 +284,4 @@ def upload(self, fileobj, tileset, name=None, patch=False, callback=None):
"""
tileset = self._validate_tileset(tileset)
url = self.stage(fileobj, callback=callback)
return self.create(url, tileset, name=name, patch=patch)
return self.create(url, tileset, name=name, patch=patch, bypass=bypass)
45 changes: 45 additions & 0 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,51 @@ def ensure_patch(request):
assert job['tileset'] == "{0}.test1".format(username)


@responses.activate
def test_upload_bypass(monkeypatch):
"""Upload a file and create a tileset bypassing mbtiles validation"""

monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

def ensure_bypass(request):
payload = json.loads(request.body.decode())
assert payload['bypass_mbtiles_validation']
headers = {}
return (201, headers, upload_response_body)

# Credentials
query_body = """
{{"key": "_pending/{username}/key.test",
"accessKeyId": "ak.test",
"bucket": "tilestream-tilesets-production",
"url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
"secretAccessKey": "sak.test",
"sessionToken": "st.test"}}""".format(username=username)

responses.add(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
username, access_token),
match_querystring=True,
body=query_body, status=200,
content_type='application/json')

responses.add_callback(
responses.POST,
'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
callback=ensure_bypass,
match_querystring=True,
content_type='application/json')

with open('tests/moors.json', 'rb') as src:
res = mapbox.Uploader(access_token=access_token).upload(
src, 'testuser.Test1', name='test1', bypass=True)

assert res.status_code == 201
job = res.json()
assert job['tileset'] == "{0}.test1".format(username)


def test_upload_tileset_validation():
with pytest.raises(mapbox.errors.ValidationError):
with open('tests/moors.json', 'rb') as src:
Expand Down

0 comments on commit 72d19db

Please sign in to comment.