-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
324 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
'use strict'; | ||
|
||
var invariant = require('invariant'), | ||
request = require('superagent'), | ||
makeService = require('../make_service'), | ||
constants = require('../constants'), | ||
makeURL = require('../make_url'); | ||
|
||
var Uploads = module.exports = makeService('MapboxUploads'); | ||
|
||
/** | ||
* To retrieve a listing of uploads for a particular account. | ||
* This request requires an access token with the uploads:list scope. | ||
* | ||
* @param {Function} callback called with (err, uploads) | ||
* @returns {undefined} nothing, calls callback | ||
* @example | ||
* var mapboxClient = new MapboxClient('ACCESSTOKEN'); | ||
* mapboxClient.listUploads(function(err, uploads) { | ||
* console.log(uploads); | ||
* // [ | ||
* // { | ||
* // "complete": true, | ||
* // "tileset": "example.mbtiles", | ||
* // "error": null, | ||
* // "id": "abc123", | ||
* // "modified": "2014-11-21T19:41:10.000Z", | ||
* // "created": "2014-11-21T19:41:10.000Z", | ||
* // "owner": "example", | ||
* // "progress": 1 | ||
* // }, | ||
* // { | ||
* // "complete": false, | ||
* // "tileset": "example.foo", | ||
* // "error": null, | ||
* // "id": "xyz789", | ||
* // "modified": "2014-11-21T19:41:10.000Z", | ||
* // "created": "2014-11-21T19:41:10.000Z", | ||
* // "owner": "example", | ||
* // "progress": 0 | ||
* // } | ||
* // ] | ||
* }); | ||
*/ | ||
Uploads.prototype.listUploads = function(owner, callback) { | ||
// defaults to the owner of the provided token if omitted | ||
if (callback === undefined && typeof owner === 'function') { | ||
callback = owner; | ||
owner = this.user; | ||
} | ||
|
||
invariant(typeof callback === 'function', 'callback must be a function'); | ||
|
||
var url = makeURL(this, constants.API_UPLOADS, { owner: owner }); | ||
|
||
request(url, function(err, res) { | ||
callback(err, res.body); | ||
}); | ||
}; | ||
|
||
Uploads.prototype.createUploadCredentials = function(owner, callback) { | ||
// defaults to the owner of the provided token if omitted | ||
if (callback === undefined && typeof owner === 'function') { | ||
callback = owner; | ||
owner = this.user; | ||
} | ||
|
||
invariant(typeof callback === 'function', 'callback must be a function'); | ||
invariant(typeof owner === 'string', 'owner must be a string'); | ||
|
||
var url = makeURL(this, constants.API_UPLOAD_CREDENTIALS, { owner: owner }); | ||
|
||
request | ||
.get(url) | ||
.end(function(err, res) { | ||
callback(err, res.body); | ||
}); | ||
}; | ||
|
||
Uploads.prototype.createUpload = function(options, owner, callback) { | ||
// defaults to the owner of the provided token if omitted | ||
if (callback === undefined && typeof owner === 'function') { | ||
callback = owner; | ||
owner = this.user; | ||
} | ||
|
||
invariant(typeof options === 'object', 'options must be an object'); | ||
invariant(typeof owner === 'string', 'owner must be a string'); | ||
invariant(typeof callback === 'function', 'callback must be a function'); | ||
|
||
var url = makeURL(this, constants.API_UPLOADS, { owner: owner }); | ||
|
||
request | ||
.post(url) | ||
.send(options) | ||
.end(function(err, res) { | ||
callback(err, res.body); | ||
}); | ||
}; | ||
|
||
Uploads.prototype.readUpload = function(upload, owner, callback) { | ||
// defaults to the owner of the provided token if omitted | ||
if (callback === undefined && typeof owner === 'function') { | ||
callback = owner; | ||
owner = this.user; | ||
} | ||
|
||
invariant(typeof upload === 'string', 'upload must be a string'); | ||
invariant(typeof callback === 'function', 'callback must be a function'); | ||
|
||
var url = makeURL(this, constants.API_UPLOAD, { | ||
owner: owner, | ||
upload: upload | ||
}); | ||
|
||
request(url, function(err, res) { | ||
callback(err, res.body); | ||
}); | ||
}; | ||
|
||
Uploads.prototype.deleteUpload = function(upload, owner, callback) { | ||
// defaults to the owner of the provided token if omitted | ||
if (callback === undefined && typeof owner === 'function') { | ||
callback = owner; | ||
owner = this.user; | ||
} | ||
|
||
invariant(typeof upload === 'string', 'upload must be a string'); | ||
invariant(typeof owner === 'string', 'owner must be a string'); | ||
invariant(typeof callback === 'function', 'callback must be a function'); | ||
|
||
var url = makeURL(this, constants.API_UPLOAD, { | ||
owner: owner, | ||
upload: upload | ||
}); | ||
|
||
request | ||
.del(url) | ||
.end(function(err) { | ||
callback(err); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* eslint no-shadow: 0 */ | ||
'use strict'; | ||
|
||
var test = require('tap').test; | ||
var MapboxClient = require('../lib/services/uploads'); | ||
var AWS = require('aws-sdk'); | ||
var hat = require('hat'); | ||
var fs = require('fs'); | ||
|
||
test('UploadClient', function(uploadClient) { | ||
var testStagedFiles = []; | ||
var testUploads = []; | ||
|
||
uploadClient.test('#createUploadCredentials', function(createUploadCredentials) { | ||
createUploadCredentials.test('typecheck', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
assert.throws(function() { | ||
client.createUploadCredentials(100, function() {}); | ||
}, 'throws owner must be a string error'); | ||
assert.throws(function() { | ||
client.createUploadCredentials(); | ||
}, 'throw no callback function error'); | ||
assert.end(); | ||
}); | ||
|
||
createUploadCredentials.test('with owner', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
|
||
client.createUploadCredentials(client.user, function(err, credentials) { | ||
assert.ifError(err, 'success'); | ||
var s3 = new AWS.S3({ | ||
accessKeyId: credentials.accessKeyId, | ||
secretAccessKey: credentials.secretAccessKey, | ||
sessionToken: credentials.sessionToken, | ||
region: 'us-east-1' | ||
}); | ||
s3.putObject({ | ||
Bucket: credentials.bucket, | ||
Key: credentials.key, | ||
Body: fs.createReadStream(__dirname + '/fixtures/valid-onlytiles.mbtiles') | ||
}, function(err, resp) { | ||
assert.ifError(err, 'success'); | ||
testStagedFiles.push({ | ||
bucket: credentials.bucket, | ||
key: credentials.key | ||
}); | ||
assert.end(); | ||
}); | ||
}); | ||
}); | ||
|
||
createUploadCredentials.test('without owner', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
|
||
client.createUploadCredentials(function(err, credentials) { | ||
assert.ifError(err, 'success'); | ||
var s3 = new AWS.S3({ | ||
accessKeyId: credentials.accessKeyId, | ||
secretAccessKey: credentials.secretAccessKey, | ||
sessionToken: credentials.sessionToken, | ||
region: 'us-east-1' | ||
}); | ||
s3.putObject({ | ||
Bucket: credentials.bucket, | ||
Key: credentials.key, | ||
Body: fs.createReadStream(__dirname + '/fixtures/valid-onlytiles.mbtiles') | ||
}, function(err, resp) { | ||
assert.ifError(err, 'success'); | ||
testStagedFiles.push({ | ||
bucket: credentials.bucket, | ||
key: credentials.key | ||
}); | ||
assert.end(); | ||
}); | ||
}); | ||
}); | ||
|
||
createUploadCredentials.end(); | ||
}); | ||
|
||
uploadClient.test('#createUpload', function(createUpload) { | ||
createUpload.test('typecheck', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
assert.throws(function() { | ||
client.createUpload('ham', function() {}); | ||
}, 'throws option not an object error'); | ||
assert.throws(function() { | ||
client.createUpload(); | ||
}, 'throws no callback function error'); | ||
assert.end(); | ||
}); | ||
|
||
createUpload.test('without owner', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
var staged = testStagedFiles.shift(); | ||
var url = 'http://' + staged.bucket + '.s3.amazonaws.com/' + staged.key; | ||
client.createUpload({ | ||
tileset: [client.user, hat()].join('.'), | ||
url: url | ||
}, function(err, upload) { | ||
assert.ifError(err, 'success'); | ||
testUploads.push(upload); | ||
assert.end(); | ||
}); | ||
}); | ||
|
||
createUpload.test('with owner', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
var staged = testStagedFiles.shift(); | ||
var url = 'http://' + staged.bucket + '.s3.amazonaws.com/' + staged.key; | ||
client.createUpload({ | ||
tileset: [client.user, hat()].join('.'), | ||
url: url | ||
}, client.user, function(err, upload) { | ||
assert.ifError(err, 'success'); | ||
testUploads.push(upload); | ||
assert.end(); | ||
}); | ||
}); | ||
|
||
createUpload.end(); | ||
}); | ||
|
||
uploadClient.test('#readUpload', function(readUpload) { | ||
readUpload.test('typecheck', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
assert.throws(function() { | ||
client.readUpload(100, function() {}); | ||
}, 'throws owner must be a string error'); | ||
assert.throws(function() { | ||
client.readUpload(); | ||
}, 'throws no callback function error'); | ||
assert.end(); | ||
}); | ||
|
||
readUpload.test('without owner', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
var upload = testUploads.shift(); | ||
var attempts = 0; | ||
function poll() { | ||
client.readUpload(upload.id, function(err, upload) { | ||
assert.ifError(err, 'success'); | ||
if (attempts > 10) throw new Error('Upload did not complete in time'); | ||
// we are waiting for mapbox to process the upload | ||
if (!upload.complete) return setTimeout(poll, Math.pow(2, attempts++) * 1000); | ||
assert.end(); | ||
}); | ||
} | ||
poll(); | ||
}); | ||
|
||
readUpload.test('with owner', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created upload client'); | ||
var upload = testUploads.shift(); | ||
client.readUpload(upload.id, upload.owner, function(err, upload) { | ||
assert.ifError(err, 'success'); | ||
assert.end(); | ||
}); | ||
}); | ||
|
||
readUpload.end(); | ||
}); | ||
|
||
uploadClient.end(); | ||
}); |