Skip to content

Commit

Permalink
Merge fbb353b into 9e6da71
Browse files Browse the repository at this point in the history
  • Loading branch information
willwhite committed Jul 30, 2015
2 parents 9e6da71 + fbb353b commit ba51bfe
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var xtend = require('xtend/mutable');
var MapboxGeocoder = require('./services/geocoder');
var MapboxSurface = require('./services/surface');
var MapboxDirections = require('./services/directions');
var MapboxUploads = require('./services/uploads');
var MapboxMatching = require('./services/matching');
var MapboxDatasets = require('./services/datasets');

Expand All @@ -29,6 +30,7 @@ xtend(MapboxClient.prototype,
MapboxSurface.prototype,
MapboxDirections.prototype,
MapboxMatching.prototype,
MapboxDatasets.prototype);
MapboxDatasets.prototype,
MapboxUploads.prototype);

module.exports = MapboxClient;
3 changes: 3 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports.API_GEOCODER_FORWARD = compile('${endpoint}/v4/geocode/${dataset}
module.exports.API_GEOCODER_REVERSE = compile('${endpoint}/v4/geocode/${dataset}/${location.longitude},${location.latitude}.json?${query}');
module.exports.API_DIRECTIONS = compile('${endpoint}/v4/directions/${profile}/${encodedWaypoints}.json?${query}');
module.exports.API_SURFACE = compile('${endpoint}/v4/surface/${mapid}.json?${query}');
module.exports.API_UPLOADS = compile('${endpoint}/uploads/v1/${owner}?${query}');
module.exports.API_UPLOAD = compile('${endpoint}/uploads/v1/${owner}/${upload}?${query}');
module.exports.API_UPLOAD_CREDENTIALS = compile('${endpoint}/uploads/v1/${owner}/credentials?${query}');
module.exports.API_MATCHING = compile('${endpoint}/matching/v4/${profile}.json?${query}');
module.exports.API_DATASET_DATASETS = compile('${endpoint}/datasets/v1/${owner}?${query}');
module.exports.API_DATASET_DATASET = compile('${endpoint}/datasets/v1/${owner}/${dataset}?${query}');
Expand Down
142 changes: 142 additions & 0 deletions lib/services/uploads.js
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);
});
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
},
"homepage": "https://github.com/mapbox/mapbox-sdk-js",
"devDependencies": {
"aws-sdk": "^2.1.41",
"browserify": "^11.0.0",
"documentation": "^2.1.0-alpha2",
"eslint": "^0.24.1",
"geojson-random": "^0.2.2",
"geojsonhint": "^1.1.0",
"hat": "0.0.3",
"polyline": "^0.1.0",
"tap": "^1.3.1",
"uglifyjs": "^2.4.10"
Expand Down
Binary file added test/fixtures/valid-onlytiles.mbtiles
Binary file not shown.
174 changes: 174 additions & 0 deletions test/uploads.js
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();
});

0 comments on commit ba51bfe

Please sign in to comment.