From c3c598ec0505a046fb229d55f692da8d0b6be529 Mon Sep 17 00:00:00 2001 From: Quentin Dreyer Date: Wed, 18 Dec 2013 17:39:54 +0100 Subject: [PATCH 1/4] Added PUT/DELETE methods --- bitbucket/index.js | 14 +++++++++++++- bitbucket/request.js | 25 +++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/bitbucket/index.js b/bitbucket/index.js index ca5c696..5d21233 100644 --- a/bitbucket/index.js +++ b/bitbucket/index.js @@ -141,7 +141,7 @@ var BitBucket = exports.BitBucket = function(debug, proxy, http) { * @param {Object} requestOptions reconfigure the request */ this["delete"] = function(route, parameters, requestOptions, callback) { - return this.getRequest().send(route, parameters, 'DELETE', requestOptions, callback); + return this.getRequest().delete(route, parameters || {}, requestOptions, callback); }; /** @@ -156,6 +156,18 @@ var BitBucket = exports.BitBucket = function(debug, proxy, http) { return this.getRequest().post(route, parameters || {}, requestOptions, callback); }; + /** + * Call any route, POST method + * Ex: api.post('repos/show/my-username', {'email': 'my-new-email@provider.org'}) + * + * @param {String} route the GitHub route + * @param {Object} parameters POST parameters + * @param {Object} requestOptions reconfigure the request + */ + this.put = function(route, parameters, requestOptions, callback) { + return this.getRequest().put(route, parameters || {}, requestOptions, callback); + }; + /** * Get the request * diff --git a/bitbucket/request.js b/bitbucket/request.js index c7617de..3118a58 100644 --- a/bitbucket/request.js +++ b/bitbucket/request.js @@ -93,6 +93,22 @@ var Request = exports.Request = function(options) { return this.send(apiPath, parameters, 'POST', options, callback); }; + /** + * Send a PUT request + * @see send + */ + this.put = function(apiPath, parameters, options, callback) { + return this.send(apiPath, parameters, 'PUT', options, callback); + }; + + /** + * Send a DELETE request + * @see send + */ + this.delete = function(apiPath, parameters, options, callback) { + return this.send(apiPath, parameters, 'DELETE', options, callback); + }; + /** * Send a request to the server, receive a response, * decode the response and returns an associative array @@ -146,8 +162,8 @@ var Request = exports.Request = function(options) { "Content-Length": "0", "Content-Type": "application/x-www-form-urlencoded" }; - var getParams = httpMethod != "POST" ? parameters : {}; - var postParams = httpMethod == "POST" ? parameters : {}; + var getParams = httpMethod == "GET" ? parameters : {}; + var postParams = httpMethod != "GET" ? parameters : {}; var getQuery = querystring.stringify(getParams); @@ -193,7 +209,7 @@ var Request = exports.Request = function(options) { var getOptions = { host: host, - post: port, + port: port, path: path, method: httpMethod, headers: headers @@ -201,6 +217,7 @@ var Request = exports.Request = function(options) { this.$debug('send ' + httpMethod + ' request: ' + path); var request = require(this.$options.protocol).request(getOptions, function(response) { + console.log("response", response); response.setEncoding('utf8'); var body = []; @@ -227,7 +244,7 @@ var Request = exports.Request = function(options) { }); }); - if (httpMethod == "POST") + if (httpMethod != "GET") request.write(postQuery); request.end(); From e4d6977d6849bf530e660ab4d3df24538c5a381d Mon Sep 17 00:00:00 2001 From: Quentin Dreyer Date: Wed, 18 Dec 2013 17:42:06 +0100 Subject: [PATCH 2/4] Clean up --- bitbucket/index.js | 6 +++--- bitbucket/request.js | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/bitbucket/index.js b/bitbucket/index.js index 5d21233..adcfd08 100644 --- a/bitbucket/index.js +++ b/bitbucket/index.js @@ -134,7 +134,7 @@ var BitBucket = exports.BitBucket = function(debug, proxy, http) { /** * Call any route, DELETE method - * Ex: api.delete('repos/show/my-username/my-repo') + * Ex: api.delete('repos/show/my-username/my-repo/ressoure-id') * * @param {String} route the GitHub route * @param {Object} parameters GET parameters @@ -157,8 +157,8 @@ var BitBucket = exports.BitBucket = function(debug, proxy, http) { }; /** - * Call any route, POST method - * Ex: api.post('repos/show/my-username', {'email': 'my-new-email@provider.org'}) + * Call any route, PUT method + * Ex: api.put('repos/show/my-username/ressoure-id', {'email': 'my-new-email@provider.org'}) * * @param {String} route the GitHub route * @param {Object} parameters POST parameters diff --git a/bitbucket/request.js b/bitbucket/request.js index 3118a58..caee835 100644 --- a/bitbucket/request.js +++ b/bitbucket/request.js @@ -217,7 +217,6 @@ var Request = exports.Request = function(options) { this.$debug('send ' + httpMethod + ' request: ' + path); var request = require(this.$options.protocol).request(getOptions, function(response) { - console.log("response", response); response.setEncoding('utf8'); var body = []; From abd9b9abb143ad11b652780052715e1b8d1d92b4 Mon Sep 17 00:00:00 2001 From: Quentin Dreyer Date: Thu, 19 Dec 2013 12:28:39 +0100 Subject: [PATCH 3/4] Changed configure so you can partially update options --- bitbucket/request.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bitbucket/request.js b/bitbucket/request.js index caee835..409270a 100644 --- a/bitbucket/request.js +++ b/bitbucket/request.js @@ -42,9 +42,10 @@ var Request = exports.Request = function(options) { this.configure = function(options) { options = options || {}; - this.$options = {}; + this.$options = this.$options || {}; for (var key in this.$defaults) { - this.$options[key] = options[key] !== undefined ? options[key] : this.$defaults[key]; + if (options[key] !== undefined) this.$options[key] = options[key] + else if (!this.$options[key]) this.$options[key] = this.$defaults[key] } return this; From 136ae939958ae1be71d6d28f7ed6a849448be6c0 Mon Sep 17 00:00:00 2001 From: Quentin Dreyer Date: Thu, 19 Dec 2013 12:35:17 +0100 Subject: [PATCH 4/4] Update package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7653a6e..d593210 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name" : "bitbucket", - "version" : "0.0.1", + "name" : "bitbucket2", + "version" : "0.0.2", "description" : "Wrapper for the BitBucket API", "author": "Fabian Jakobs ", "homepage": "http://github.com/ajaxorg/node-bitbucket.git",