From c0748ce56bbd4f3d48aef555fe3d250304cb6db2 Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Wed, 9 Dec 2015 02:24:42 +0200 Subject: [PATCH 1/9] Correct require argument format --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93c1f35..33d20f8 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Example Usage: `npm install localbitcoins-node` ```javascript -var LBCClient = require(localbitcoins-node); +var LBCClient = require('localbitcoins-node'); var lbc = new LBCClient(api_key, api_secret); // Display user's info From 5ed66390ae46b51382ca60302ca2038f0d7b11fb Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Wed, 9 Dec 2015 02:47:52 +0200 Subject: [PATCH 2/9] Add gitignore file --- .gitignore | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..123ae94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules From b88a0fbb8e4e64ccddcf62c9513fe8d5c6c9371a Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Wed, 9 Dec 2015 02:51:44 +0200 Subject: [PATCH 3/9] add ad_id paramater --- README.md | 8 ++++++-- index.js | 31 ++++++++++++++++++++++--------- package.json | 4 ++-- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 33d20f8..829beda 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,12 @@ Example Usage: var LBCClient = require('localbitcoins-node'); var lbc = new LBCClient(api_key, api_secret); +var ad_id; //set to value when applicable +var params = {}; + + // Display user's info -lbc.api('myself', null, function(error, data) { +lbc.api('myself', ad_id, params, function(error, data) { if(error) { console.log(error); } @@ -27,7 +31,7 @@ lbc.api('myself', null, function(error, data) { ``` -To-Do: +To-Do: - Get different methods working with querystring parameters added to message Credit: diff --git a/index.js b/index.js index ee5a713..c409cff 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ var nonce = (new Date).getTime(); function LBCClient(key, secret, otp) { var self = this; - + var config = { url: 'https://localbitcoins.com/api', key: key, @@ -22,22 +22,22 @@ function LBCClient(key, secret, otp) { * @param {Function} callback A callback function to be executed when the request is complete * @return {Object} The request object */ - function api(method, params, callback) { + function api(method, ad_id, params, callback) { var methods = { onlineAds: ['buy-bitcoins-online'], public: ['countrycodes'], private: ['ad-get', 'ad-get/ad_id', 'myself', 'ads', - 'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed', + 'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed', 'dashboard/released/buyer', 'dashboard/canceled/buyer', 'dashboard/closed/buyer', 'dashboard/released/seller', 'dashboard/canceled/seller', 'dashboard/closed/seller', 'wallet-send' ] }; if(methods.public.indexOf(method) !== -1) { - return publicMethod(method, params, callback); + return publicMethod(method, params, ad_id, callback); } else if(methods.private.indexOf(method) !== -1) { - return privateMethod(method, params, callback); + return privateMethod(method, params, ad_id, callback); } else { throw new Error(method + ' is not a valid API method.'); @@ -51,10 +51,16 @@ function LBCClient(key, secret, otp) { * @param {Function} callback A callback function to be executed when the request is complete * @return {Object} The request object */ - function publicMethod(method, params, callback) { + function publicMethod(method, params, ad_id, callback) { params = params || {}; - var path = '/' + method; + var path; + if (ad_id) { + path = '/' + method + '/' + ad_id; + } else { + path = '/' + method; + } + var url = config.url + path; return rawRequest(url, {}, params, callback); @@ -67,10 +73,17 @@ function LBCClient(key, secret, otp) { * @param {Function} callback A callback function to be executed when the request is complete * @return {Object} The request object */ - function privateMethod(method, params, callback) { + function privateMethod(method, params, ad_id, callback) { params = params || {}; - var path = '/' + method; + var path; + + if (ad_id) { + path = '/' + method + '/' + ad_id; + } else { + path = '/' + method; + } + var url = config.url + path; var signature = getMessageSignature(path, params, nonce); diff --git a/package.json b/package.json index de03188..d69bcd1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "localbitcoins-node", - "version": "0.0.3", + "version": "0.0.4", "description": "LocalBitcoins API wrapper for NodeJS", "keywords": [ "localbitcoins", @@ -9,7 +9,7 @@ "bitcoin" ], "author": "Anthony Mayfield (https://github.com/mrmayfield)", - "contributors": [], + "contributors": ["Vlad Nistor (https://github.com/vnistor)"], "license": "MIT", "dependencies": { "querystring": ">=0.2.0", From c56f7e80319db1cec5d588bd5b9d4a4f1d6cda46 Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Sun, 13 Dec 2015 01:00:08 +0200 Subject: [PATCH 4/9] add GET HTTP method capabilities --- index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c409cff..5ac5882 100644 --- a/index.js +++ b/index.js @@ -123,13 +123,57 @@ function LBCClient(key, secret, otp) { */ function rawRequest(url, headers, params, callback) { + var gets = ['ad-get', 'dashboard', 'dashboard/released', 'dashboard/canceled', + 'dashboard/closed', 'dashboard/released/buyer', 'dashboard/canceled/buyer', + 'dashboard/closed/buyer', 'dashboard/released/seller', 'dashboard/canceled/seller', + 'dashboard/closed/seller']; + var posts = [ 'ad-get/ad_id', 'myself', 'ads', + 'wallet-send', 'wallet-balance', 'wallet-addr']; + + if (posts.indexOf(posts) !== -1) { + + var options = { + url: url + '/', + headers: headers, + form: params, + }; + + var req = request.post(options, function(error, response, body) { + if(typeof callback === 'function') { + var data; + + if(error) { + callback.call(self, new Error('Error in server response: ' + JSON.stringify(error)), null); + return; + } + + try { + data = JSON.parse(body); + } + catch(e) { + callback.call(self, new Error('Could not understand response from server: ' + body), null); + return; + } + + if(data.error && data.error.length) { + callback.call(self, data.error, null); + } + else { + callback.call(self, null, data); + } + } + }); + + return req; + + } else { + var options = { url: url + '/', headers: headers, - form: params, }; - var req = request.post(options, function(error, response, body) { + var req = request.get(options, function(error, response, body) { if(typeof callback === 'function') { var data; @@ -157,6 +201,7 @@ function LBCClient(key, secret, otp) { return req; } + } self.api = api; self.publicMethod = publicMethod; From 8f4e8c45f83aaf00cee447016b03fc60e2c43925 Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Sun, 13 Dec 2015 01:02:25 +0200 Subject: [PATCH 5/9] Added GET HTTP method and id capabilities This version breaks things through the introduction of id capabilities, bumping version to 0.1. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d69bcd1..c27b49c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "localbitcoins-node", - "version": "0.0.4", + "version": "0.1", "description": "LocalBitcoins API wrapper for NodeJS", "keywords": [ "localbitcoins", From 2cebd19ff3a27f9a35bbdda8d839853d970c209f Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Sun, 13 Dec 2015 01:03:17 +0200 Subject: [PATCH 6/9] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c27b49c..3343b4f 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "type": "git", "url": "https://github.com/mrmayfield/localbitcoins-node" }, - "main": "localbitcoins.js", + "main": "index.js", "engines": { "node": ">=0.10" }, From f00e265d09e202e5bfcf4bb1fcac6b51e8a6b088 Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Sun, 13 Dec 2015 02:59:31 +0200 Subject: [PATCH 7/9] don not bypas POST HTTP methods --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5ac5882..8f751af 100644 --- a/index.js +++ b/index.js @@ -95,7 +95,7 @@ function LBCClient(key, secret, otp) { 'Apiauth-Signature': signature }; - return rawRequest(url, headers, params, callback); + return rawRequest(url, headers, params, method, callback); } /** @@ -121,7 +121,7 @@ function LBCClient(key, secret, otp) { * @param {Function} callback A callback function to call when the request is complete * @return {Object} The request object */ - function rawRequest(url, headers, params, callback) { + function rawRequest(url, headers, params, method, callback) { var gets = ['ad-get', 'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed', 'dashboard/released/buyer', 'dashboard/canceled/buyer', @@ -130,7 +130,7 @@ function LBCClient(key, secret, otp) { var posts = [ 'ad-get/ad_id', 'myself', 'ads', 'wallet-send', 'wallet-balance', 'wallet-addr']; - if (posts.indexOf(posts) !== -1) { + if (posts.indexOf(method) !== -1) { var options = { url: url + '/', From 62fd6d98dec0546d037c44db52b3847004c750cd Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Sun, 13 Dec 2015 03:00:10 +0200 Subject: [PATCH 8/9] don not bypas POST HTTP methods --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3343b4f..24cd0bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "localbitcoins-node", - "version": "0.1", + "version": "0.1.1", "description": "LocalBitcoins API wrapper for NodeJS", "keywords": [ "localbitcoins", From 2b44c8837fbc8b2ac4054f63e2ea1e5ba522cdfd Mon Sep 17 00:00:00 2001 From: Vlad Nistor Date: Sun, 13 Dec 2015 11:10:06 +0200 Subject: [PATCH 9/9] add /wallet route --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8f751af..aaa202d 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ function LBCClient(key, secret, otp) { 'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed', 'dashboard/released/buyer', 'dashboard/canceled/buyer', 'dashboard/closed/buyer', 'dashboard/released/seller', 'dashboard/canceled/seller', 'dashboard/closed/seller', - 'wallet-send' + 'wallet-send', 'wallet' ] }; if(methods.public.indexOf(method) !== -1) { @@ -126,7 +126,7 @@ function LBCClient(key, secret, otp) { var gets = ['ad-get', 'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed', 'dashboard/released/buyer', 'dashboard/canceled/buyer', 'dashboard/closed/buyer', 'dashboard/released/seller', 'dashboard/canceled/seller', - 'dashboard/closed/seller']; + 'dashboard/closed/seller', 'wallet']; var posts = [ 'ad-get/ad_id', 'myself', 'ads', 'wallet-send', 'wallet-balance', 'wallet-addr'];