Repository: mapbox/mapbox-sdk-py
Pull request #142 (2016-11-21 16:22:52)

add patch mode
cc @sgillies @rclark 


Patch:
diff --git a/mapbox/services/uploads.py b/mapbox/services/uploads.py
index 333930c..b4844e4 100644
--- a/mapbox/services/uploads.py
+++ b/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
@@ -74,7 +72,7 @@ class Uploader(Service):
 
         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.
 
@@ -96,6 +94,9 @@ class Uploader(Service):
         msg = {'tileset': tileset,
                'url': stage_url}
 
+        if patch:
+            msg['patch'] = patch
+
         if name is not None:
             msg['name'] = name
 
@@ -149,10 +150,10 @@ class Uploader(Service):
         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)


Test patch:
diff --git a/tests/test_upload.py b/tests/test_upload.py
index a2991a0..a643a64 100644
--- a/tests/test_upload.py
+++ b/tests/test_upload.py
@@ -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)