bitballoon
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -47,3 +47,3 @@ var base64 = require('base64-js'), | ||
} | ||
this.request({ | ||
@@ -65,10 +65,10 @@ type: "post", | ||
}, | ||
authorizeFromCode: function(code, cb) { | ||
var client = this; | ||
if (!(this.client_id && this.client_secret && this.redirect_uri)) { | ||
return cb("Instantiate the client with a client_id, client_secret and redirect_uri to authorize from code"); | ||
} | ||
this.request({ | ||
@@ -94,3 +94,3 @@ type: "post", | ||
}, | ||
authorizeUrl: function(options) { | ||
@@ -110,4 +110,4 @@ if (!(this.client_id && this.redirect_uri)) { | ||
site: function(id, cb) { this.element({model: Client.models.Site, id: id}, cb); }, | ||
createSite: function(options, cb) { | ||
createSite: function(options, cb) { | ||
this.withAuthorization(cb, function() { | ||
@@ -118,2 +118,4 @@ if (options.dir) { | ||
Client.models.Site.createFromZip(this, options.zip, cb); | ||
} else { | ||
Client.models.Site.create(this, options, cb); | ||
} | ||
@@ -126,29 +128,29 @@ }); | ||
form: function(id, cb) { this.element({model: Client.models.Form, id: id}, cb); }, | ||
submissions: function(options, cb) { this.collection({model: Client.models.Submission}, options, cb); }, | ||
submission: function(id, cb) { this.element({model: Client.models.Submission, id: id}, cb); }, | ||
users: function(options, cb) { this.collection({model: Client.models.User}, options, cb); }, | ||
user: function(id, cb) { this.element({model: Client.models.User, id: id}, cb); }, | ||
createUser: function(attributes, cb) { | ||
this.create({model: Client.models.User, attributes: attributes}, cb); | ||
}, | ||
dnsZones: function(options, cb) { this.collection({model: Client.models.DnsZone}, options, cb); }, | ||
dnsZone: function(id, cb) { this.element({model: Client.models.DnsZone, id: id}, cb); }, | ||
createDnsZone: function(attributes, cb) { | ||
this.create({model: Client.models.DnsZone, attributes: attributes}, cb); | ||
}, | ||
accessToken: function(id, cb) { this.element({model: Client.models.AccessToken, id: id}, cb); }, | ||
createAccessToken: function(attributes, cb) { | ||
this.create({model: Client.models.AccessToken, attributes: attributes}, cb); | ||
}, | ||
collection: function(options, pagination, cb) { | ||
@@ -168,3 +170,3 @@ if (cb === undefined) { cb = pagination; pagination = {}} | ||
}, | ||
element: function(options, cb) { | ||
@@ -178,5 +180,5 @@ this.withAuthorization(cb, function() { | ||
}); | ||
}); | ||
}); | ||
}, | ||
create: function(options, cb) { | ||
@@ -192,3 +194,3 @@ this.withAuthorization(cb, function() { | ||
}); | ||
}); | ||
}); | ||
}, | ||
@@ -209,3 +211,3 @@ | ||
}, | ||
destroy: function(options, cb) { | ||
@@ -232,9 +234,9 @@ this.withAuthorization(cb, function() { | ||
headers['Content-Type'] = options.contentType || "application/json"; | ||
if (options.body) { | ||
body = prepareBody(options.body, headers); | ||
} | ||
headers['Content-Length'] = body ? body.length : 0; | ||
headers['Content-Length'] = body ? body.length : 0; | ||
if (this.access_token && !options.auth) { | ||
@@ -256,3 +258,3 @@ headers['Authorization'] = "Bearer " + this.access_token | ||
data = null; | ||
res.on("data", function(chunk) { | ||
@@ -267,6 +269,6 @@ body += chunk; | ||
cb(null, data, client); | ||
} else if (res.statusCode == 401) { | ||
} else if (res.statusCode == 401 || res.statusCode == 403) { | ||
cb("Authentication failed", null, client); | ||
} else { | ||
if ((requestOptions.method === "get" || | ||
if ((requestOptions.method === "get" || | ||
requestOptions.method === "put" || | ||
@@ -283,3 +285,3 @@ requestOptions.method === "delete") && | ||
}); | ||
request.on("error", function(err) { | ||
@@ -296,3 +298,3 @@ if ((requestOptions.method == "get" || | ||
}); | ||
if (body) { | ||
@@ -303,3 +305,3 @@ request.write(body); | ||
}, | ||
withAuthorization: function(cb, fn) { | ||
@@ -311,2 +313,2 @@ if (!this.isAuthorized()) return cb("Not authorized: Instantiate client with access_token"); | ||
exports.Client = Client; | ||
exports.Client = Client; |
var model = require("./model"); | ||
if (typeof(require) !== 'undefined') { | ||
var fs = require("fs"); | ||
} | ||
var Deploy = model.constructor(); | ||
@@ -7,2 +11,5 @@ Deploy.path = "/deploys"; | ||
Deploy.prototype = { | ||
isReady: function() { | ||
return this.state == "ready" || this.state == "current"; | ||
}, | ||
restore: function(cb) { | ||
@@ -18,5 +25,63 @@ var self = this; | ||
}); | ||
}, | ||
publish: function(cb) { | ||
this.restore(cb); | ||
}, | ||
refresh: function(cb) { | ||
var self = this; | ||
this.client.request({ | ||
url: this.apiPath | ||
}, function(err, data, client) { | ||
if (err) return cb(err); | ||
Deploy.call(self, client, data); | ||
cb(null, self); | ||
}); | ||
}, | ||
waitForReady: function(cb) { | ||
var self = this; | ||
if (this.isReady()) { | ||
process.nextTick(function() { cb(null, self); }); | ||
} else { | ||
setTimeout(function() { | ||
self.refresh(function(err) { | ||
if (err) return cb(err); | ||
self.waitForReady(cb); | ||
}); | ||
}, 1000); | ||
} | ||
}, | ||
uploadFiles: function(files, cb) { | ||
if (this.state !== "uploading") return cb(null, this); | ||
if (files.length == 0) { return this.refresh(cb); } | ||
var self = this, | ||
cbCalled = false, | ||
uploaded = []; | ||
files.forEach(function(file) { | ||
fs.readFile(file.abs, function(err, data) { | ||
if (cbCalled) return null; | ||
if (err) { cbCalled = true; return cb(err); } | ||
self.client.request({ | ||
url: "/deploys/" + self.id + "/files/" + file.rel, | ||
type: "put", | ||
body: data, | ||
contentType: "application/octet-stream", | ||
ignoreResponse: true | ||
}, function(err) { | ||
if (cbCalled) return null; | ||
if (err) { cbCalled = true; return cb(err); } | ||
uploaded.push(file); | ||
if (uploaded.length == files.length) { | ||
self.refresh(cb); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}; | ||
exports.Deploy = Deploy; | ||
exports.Deploy = Deploy; |
@@ -11,2 +11,2 @@ exports.constructor = function() { | ||
return obj; | ||
} | ||
} |
@@ -71,3 +71,3 @@ var path = require("path"), | ||
var createFromDir = function(client, dir, siteId, cb) { | ||
var deployFromDir = function(site, dir, cb) { | ||
var fullDir = path.resolve(dir); | ||
@@ -80,5 +80,5 @@ | ||
calculateShas(files, function(err, filesWithShas) { | ||
client.request({ | ||
url: "/sites" + (siteId ? "/" + siteId : ""), | ||
type: siteId ? "put" : "post", | ||
site.client.request({ | ||
url: site.apiPath + "/deploys", | ||
type: "post", | ||
body: JSON.stringify({ | ||
@@ -89,8 +89,8 @@ files: filesWithShas | ||
if (err) return cb(err); | ||
var site = new Site(client, data); | ||
var deploy = new Deploy(site.client, data); | ||
var shas = {}; | ||
data.required.forEach(function(sha) { shas[sha] = true; }); | ||
var filtered = files.filter(function(file) { return shas[filesWithShas[file.rel]]; }); | ||
site.uploadFiles(filtered, function(err, site) { | ||
cb(err, site); | ||
deploy.uploadFiles(filtered, function(err, deploy) { | ||
cb(err, deploy); | ||
}); | ||
@@ -103,3 +103,3 @@ }); | ||
var createFromZip = function(client, zip, siteId, cb) { | ||
var deployFromZip = function(site, zip, cb) { | ||
var fullPath = zip.match(/^\//) ? zip : process.cwd() + "/" + zip; | ||
@@ -110,5 +110,5 @@ | ||
client.request({ | ||
url: "/sites" + (siteId ? "/" + siteId : ""), | ||
type: siteId ? "put" : "post", | ||
site.client.request({ | ||
url: site.apiPath + "/deploys", | ||
type: "post", | ||
body: zipData, | ||
@@ -119,3 +119,3 @@ contentType: "application/zip" | ||
return cb(null, new Site(client, data)); | ||
return cb(null, new Deploy(site.client, data)); | ||
}); | ||
@@ -142,9 +142,23 @@ }); | ||
Site.createFromDir = function(client, dir, cb) { | ||
createFromDir(client, dir, null, cb); | ||
Site.create(client, {}, function(err, site) { | ||
site.createDeploy({dir: dir}, function(err, deploy) { | ||
site.deploy_id = deploy.id; | ||
cb(null, site); | ||
}) | ||
}); | ||
}; | ||
Site.createFromZip = function(client, zip, cb) { | ||
createFromZip(client, zip, null, cb); | ||
Site.create(client, {}, function(err, site) { | ||
site.createDeploy({zip: zip}, function(err, deploy) { | ||
site.deploy_id = deploy.id; | ||
cb(null, site); | ||
}) | ||
}); | ||
}; | ||
Site.create = function(client, attributes, cb) { | ||
client.create({model: Site, attributes: attributesForUpdate(attributes)}, cb); | ||
}; | ||
Site.prototype = { | ||
@@ -183,2 +197,17 @@ isReady: function() { | ||
createDeploy: function(attributes, cb) { | ||
if (attributes.dir) { | ||
deployFromDir(this, attributes.dir, cb); | ||
} else if (attributes.zip) { | ||
deployFromZip(this, attributes.zip, cb); | ||
} else { | ||
cb("You must specify a 'dir' or a 'zip' to deploy"); | ||
} | ||
}, | ||
createDraftDeploy: function(attributes, cb) { | ||
attributes.draft = true; | ||
this.createDeploy(attributes, cb); | ||
}, | ||
waitForReady: function(cb) { | ||
@@ -233,34 +262,2 @@ var self = this; | ||
this.client.element({prefix: this.apiPath, model: Deploy, id: id}, cb); | ||
}, | ||
uploadFiles: function(files, cb) { | ||
if (this.state !== "uploading") return cb(null, this); | ||
if (files.length == 0) { return this.refresh(cb); } | ||
var self = this, | ||
cbCalled = false, | ||
uploaded = []; | ||
files.forEach(function(file) { | ||
fs.readFile(file.abs, function(err, data) { | ||
if (cbCalled) return null; | ||
if (err) { cbCalled = true; return cb(err); } | ||
self.client.request({ | ||
url: "/sites/" + self.id + "/files/" + file.rel, | ||
type: "put", | ||
body: data, | ||
contentType: "application/octet-stream", | ||
ignoreResponse: true | ||
}, function(err) { | ||
if (cbCalled) return null; | ||
if (err) { cbCalled = true; return cb(err); } | ||
uploaded.push(file); | ||
if (uploaded.length == files.length) { | ||
self.refresh(cb); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
@@ -267,0 +264,0 @@ }; |
@@ -5,3 +5,3 @@ { | ||
"description": "BitBalloon API client", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "url": "" |
@@ -43,3 +43,3 @@ BitBalloon Node Client | ||
client.authorizeFromCredentials(function(err, access_token) { | ||
if (err) return console.log(err); | ||
if (err) return console.log(err); | ||
// Client is now ready to do requests | ||
@@ -90,41 +90,17 @@ // You can store the access_token to avoid authorizing in the future | ||
Creating a site from a directory (requires node): | ||
Creating a new empty site: | ||
```js | ||
client.createSite({dir: "/tmp/my-site"}, function(err, site) { | ||
// do work | ||
}); | ||
``` | ||
client.createSite({name: "my-unique-site-name", domain: "example.com", password: "secret"}, function(err, site) { | ||
console.log(site); | ||
}) | ||
Creating a site from a zip file (requires node): | ||
To deploy a site from a dir and wait for the processing of the site to finish: | ||
```js | ||
client.createSite({zip: "/tmp/my-site.zip"}, function(err, site) { | ||
// do work | ||
}); | ||
``` | ||
Both methods will create the site and upload the files. The site will then be processing. | ||
```js | ||
client.createSite({zip: "/tmp-my-site.zip"}, function(err, site) { | ||
site.state == "processing" | ||
}); | ||
``` | ||
Refresh a site to update the state: | ||
```js | ||
site.refresh(function(err, site) { | ||
console.log(site.state); | ||
}); | ||
``` | ||
Use `waitForReady` to wait until a site has finished processing. | ||
```js | ||
client.createSite({zip: "/tmp-my-site.zip"}, function(err, site) { | ||
site.waitForReady(function(err, site) { | ||
if (err) return console.log("Error deploying site %o", err); | ||
console.log("Site deployed"); | ||
client.createSite({}, function(err, site) { | ||
site.createDeploy({dir: "/tmp/my-site"}, function(err, deploy) { | ||
deploy.waitForReady(function(deploy) { | ||
console.log("Deploy is done: ", deploy); | ||
}); | ||
}); | ||
@@ -134,25 +110,10 @@ }); | ||
Redeploy a site from a dir: | ||
Creating a new deploy for a site from a zip file: | ||
```js | ||
client.site(id, function(err, site) { | ||
if (err) return console.log("Error finding site %o", err); | ||
site.update({dir: "/tmp/my-site"}, function(err, site) { | ||
if (err) return console.log("Error updating site %o", err); | ||
site.waitForReady(function(err, site) { | ||
if (err) return console.log("Error updating site %o", err); | ||
console.log("Site redeployed"); | ||
}); | ||
}); | ||
}) | ||
``` | ||
Redeploy a site from a zip file: | ||
```ruby | ||
client.site(id, function(err, site) { | ||
if (err) return console.log("Error finding site %o", err); | ||
site.update({zip: "/tmp/my-site.zip"}, function(err, site) { | ||
site.createDeploy({zip: "/tmp/my-site.zip"}, function(err, deploy) { | ||
if (err) return console.log("Error updating site %o", err); | ||
site.waitForReady(function(err, site) { | ||
deploy.waitForReady(function(err, deploy) { | ||
if (err) return console.log("Error updating site %o", err); | ||
@@ -278,3 +239,3 @@ console.log("Site redeployed"); | ||
if (err) return console.log("Error getting file %o", err); | ||
file.readFile(function(err, data) { | ||
@@ -284,3 +245,3 @@ if (err) return console.log("Error reading file %o", err); | ||
}); | ||
file.writeFile("Hello, World!", function(err, file) { | ||
@@ -313,8 +274,24 @@ if (err) return console.log("Error writing to file %o", err); | ||
Restore a deploy (makes it the current live version of the site) | ||
Create a new deploy: | ||
```js | ||
site.createDeploy({dir: "/path/to/folder"}, function(err, deploy) { | ||
console.log(deploy) | ||
}) | ||
``` | ||
Create a draft deploy (wont get published after processing): | ||
```js | ||
site.createDeploy({dir: "/path/to/folder", draft: true}, function(err, deploy) { | ||
console.log(deploy); | ||
}) | ||
``` | ||
Publish a deploy (makes it the current live version of the site) | ||
```js | ||
site.deploy(id, function(err, deploy) { | ||
if (err) return console.log(err); | ||
deploy.restore(function(err, deploy) { | ||
deploy.publish(function(err, deploy) { | ||
// restored | ||
@@ -325,2 +302,3 @@ }); | ||
Snippets | ||
@@ -327,0 +305,0 @@ ======== |
Sorry, the diff of this file is too big to display
339523
29
9466
534
10