Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
add patch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Nov 21, 2016
1 parent 7aadbb0 commit d52e4f5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
11 changes: 6 additions & 5 deletions mapbox/services/uploads.py
@@ -1,6 +1,4 @@
# mapbox
import os.path
import uuid

from boto3.session import Session as boto3_session
from uritemplate import URITemplate
Expand Down Expand Up @@ -74,7 +72,7 @@ def stage(self, fileobj, creds=None):

return creds['url']

def create(self, stage_url, tileset, name=None):
def create(self, stage_url, tileset, name=None, patch=False):
"""Initiates the creation process from the
staging S3 bucket into the user's tileset.
Expand All @@ -96,6 +94,9 @@ def create(self, stage_url, tileset, name=None):
msg = {'tileset': tileset,
'url': stage_url}

if patch:
msg['patch'] = patch

if name is not None:
msg['name'] = name

Expand Down Expand Up @@ -149,10 +150,10 @@ def status(self, upload):
self.handle_http_error(resp)
return resp

def upload(self, fileobj, tileset, name=None):
def upload(self, fileobj, tileset, name=None, patch=False):
"""High level function to upload a file object to mapbox tileset
Effectively replicates the upload functionality using the HTML form
Returns a response object where the json() is a dict with upload metadata
"""
url = self.stage(fileobj)
return self.create(url, tileset, name=name)
return self.create(url, tileset, name=name, patch=patch)
44 changes: 44 additions & 0 deletions tests/test_upload.py
Expand Up @@ -322,3 +322,47 @@ def test_invalid_fileobj():
with pytest.raises(mapbox.errors.InvalidFileError):
mapbox.Uploader(access_token=access_token).upload(
'tests/moors.json', 'test1')


@responses.activate
def test_upload_patch(monkeypatch):
"""Upload a file and create a tileset in patch mode"""

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

def ensure_patch(request):
payload = json.loads(request.body.decode())
assert payload['patch']
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.GET,
'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_patch,
match_querystring=True,
content_type='application/json')

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

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

0 comments on commit d52e4f5

Please sign in to comment.