Skip to content

Commit

Permalink
Merge pull request #1 from blocktrail/webhooks-api
Browse files Browse the repository at this point in the history
Webhooks api
  • Loading branch information
rubensayshi committed Jan 5, 2015
2 parents 8da88c5 + 527163e commit d38136c
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 44 deletions.
166 changes: 155 additions & 11 deletions lib/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var APIClient = function (options) {
* get a single address
*
* @param address string address hash
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.address = function(address, cb) {
Expand All @@ -51,7 +51,7 @@ APIClient.prototype.address = function(address, cb) {
*
* @param address string address hash
* @param params array pagination: {page: 1, limit: 20, sort_dir: 'asc'}
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.addressTransactions = function(address, params, cb) {
Expand All @@ -65,7 +65,7 @@ APIClient.prototype.addressTransactions = function(address, params, cb) {
*
* @param address string address hash
* @param params array pagination: {page: 1, limit: 20, sort_dir: 'asc'}
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.addressUnconfirmedTransactions = function(address, params, cb) {
Expand All @@ -79,7 +79,7 @@ APIClient.prototype.addressUnconfirmedTransactions = function(address, params, c
*
* @param address string address hash
* @param params array pagination: {page: 1, limit: 20, sort_dir: 'asc'}
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.addressUnspentOutputs = function(address, params, cb) {
Expand All @@ -93,7 +93,7 @@ APIClient.prototype.addressUnspentOutputs = function(address, params, cb) {
*
* @param address string address hash
* @param signature string a signed message (the address hash) using the private key of the address
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.verifyAddress = function(address, signature, cb) {
Expand All @@ -106,7 +106,7 @@ APIClient.prototype.verifyAddress = function(address, signature, cb) {
* get all blocks (paginated)
*
* @param params array pagination: {page: 1, limit: 20, sort_dir: 'asc'}
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.allBlocks = function(params, cb) {
Expand All @@ -119,7 +119,7 @@ APIClient.prototype.allBlocks = function(params, cb) {
* get a block
*
* @param block string|int a block hash or a block height
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.block = function(block, cb) {
Expand All @@ -131,7 +131,7 @@ APIClient.prototype.block = function(block, cb) {
/**
* get the latest block
*
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.blockLatest = function(cb) {
Expand All @@ -144,8 +144,8 @@ APIClient.prototype.blockLatest = function(cb) {
* get all transactions for a block (paginated)
*
* @param block string|int a block hash or a block height
* @param params array pagination: {page: 1, limit: 20, sort_dir: 'asc'}
* @param cb
* @param params object pagination: {page: 1, limit: 20, sort_dir: 'asc'}
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.blockTransactions = function(block, params, cb) {
Expand All @@ -158,7 +158,7 @@ APIClient.prototype.blockTransactions = function(block, params, cb) {
* get a single transaction
*
* @param tx string transaction hash
* @param cb
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.transaction = function(tx, cb) {
Expand All @@ -167,6 +167,150 @@ APIClient.prototype.transaction = function(tx, cb) {
return self.client.get("/transaction/" + tx, null, cb);
};

/**
* get a paginated list of all webhooks associated with the api user
* @param params object pagination: {page: 1, limit: 20}
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.allWebhooks = function(params, cb) {
var self = this;

return self.client.get("/webhooks", params, cb);
};

/**
* create a new webhook
* @param url string the url to receive the webhook events
* @param identifier string a unique identifier associated with the webhook
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.setupWebhook = function(url, identifier, cb) {
var self = this;
if(typeof identifier == "function" && typeof cb == "undefined") {
//mimic function overloading
cb = identifier;
identifier = null;
}

return self.client.post("/webhook", null, {url: url, identifier: identifier}, cb);
};

/**
* get an existing webhook by it's identifier
* @param identifier string the unique identifier of the webhook to get
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.getWebhook = function(identifier, cb) {
var self = this;

return self.client.get("/webhook/" + identifier, null, cb);
};

/**
* update an existing webhook
* @param identifier string the unique identifier of the webhook
* @param webhookData object the data to update: {identifier: newIdentifier, url:newUrl}
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.updateWebhook = function(identifier, webhookData, cb) {
var self = this;

return self.client.put("/webhook/" + identifier, null, webhookData, cb);
};

/**
* deletes an existing webhook and any event subscriptions associated with it
* @param identifier string the unique identifier of the webhook
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.deleteWebhook = function(identifier, cb) {
var self = this;

return self.client.delete("/webhook/" + identifier, null, cb);
};

/**
* get a paginated list of all the events a webhook is subscribed to
* @param identifier string the unique identifier of the webhook
* @param params object pagination: {page: 1, limit: 20}
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.getWebhookEvents = function(identifier, params, cb) {
var self = this;
if(typeof params == "function" && typeof cb == "undefined") {
//mimic function overloading
cb = params;
params = null;
}

return self.client.get("/webhook/" + identifier + "/events", params, cb);
};

/**
* subscribes a webhook to transaction events on a particular address
* @param identifier string the unique identifier of the webhook
* @param address string the address hash
* @param confirmations integer the amount of confirmations to send
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.subscribeAddressTransactions = function(identifier, address, confirmations, cb) {
var self = this;
var postData = {
'event_type': 'address-transactions',
'address': address,
'confirmations': confirmations
};

return self.client.post("/webhook/" + identifier + "/events", null, postData, cb);
};

/**
* subscribes a webhook to a new block event
* @param identifier string the unique identifier of the webhook
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.subscribeNewBlocks = function(identifier, cb) {
var self = this;
var postData = {
'event_type': 'block'
};

return self.client.post("/webhook/" + identifier + "/events", null, postData, cb);
};

/**
* removes an address transaction event subscription from a webhook
* @param identifier string the unique identifier of the webhook
* @param address string the address hash
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.unsubscribeAddressTransactions = function(identifier, address, cb) {
var self = this;

return self.client.delete("/webhook/" + identifier + "/address-transactions/" + address, null, cb);
};

/**
* removes a block event subscription from a webhook
* @param identifier string the unique identifier of the webhook
* @param cb function callback function to call when request is complete
* @return q.Promise
*/
APIClient.prototype.unsubscribeNewBlocks = function(identifier, cb) {
var self = this;

return self.client.delete("/webhook/" + identifier + "/block/", null, cb);
};

module.exports = function(options) {
return new APIClient(options);
};
6 changes: 5 additions & 1 deletion lib/rest_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ RestClient.prototype.post = function (path, params, data, fn) {
return this.create_request({auth: 'http-signature'}).request('POST', path, params, data, fn);
};

RestClient.prototype.put = function (path, params, data, fn) {
return this.create_request({auth: 'http-signature'}).request('PUT', path, params, data, fn);
};

RestClient.prototype.get = function (path, params, fn) {
return this.create_request().request('GET', path, params, null, fn);
};

RestClient.prototype.delete = function (path, params, fn) {
return this.create_request().request('DELETE', path, params, null, fn);
return this.create_request({auth: 'http-signature'}).request('DELETE', path, params, null, fn);
};

function extend(target) {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"contributors": [
{
"name": "Ruben de Vries"
},
{
"name": "Oisin Akiboye Conolly",
"email": "[email protected]"
}
],
"devDependencies": {
Expand Down
Loading

0 comments on commit d38136c

Please sign in to comment.