forked from MadKudu/node-hubspot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e4a386e
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# EditorConfig is awesome: http://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# node-hubspot | ||
|
||
Node.js wrapper for the HubSpot API | ||
|
||
## install | ||
|
||
npm install node-hubspot | ||
|
||
## use | ||
|
||
var Client = require('hubspot'); | ||
|
||
var client = new Client(); | ||
|
||
client.useKey('API_KEY'); | ||
client.useToken('MY_ACCESS_TOKEN'); | ||
client.campaigns.get(function(err, res) { | ||
if (err) { throw err; } | ||
console.log(res); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/client'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
var request = require('request'); | ||
|
||
var baseUrl = 'http://api.hubapi.com/'; | ||
|
||
function Client() { | ||
var self = this; | ||
|
||
self.token; | ||
self.key; | ||
|
||
function useToken (token) { | ||
if (!token || typeof token !== 'string') { return cb(new Error("You must provide a token.")); } | ||
self.token = token; | ||
} | ||
|
||
function useKey (key) { | ||
if (!key || typeof key !== 'string') { return cb(new Error("You must provide a key.")); } | ||
self.key = key; | ||
} | ||
|
||
var contacts = { | ||
get: function(options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
var call = { | ||
method: 'GET', | ||
url: baseUrl + 'contacts/v1/lists/all/contacts/all', | ||
qs: options | ||
}; | ||
sendRequest(call, cb); | ||
} | ||
}; | ||
|
||
var lists = { | ||
get: function(options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
var call = { | ||
method: 'GET', | ||
url: baseUrl + 'contacts/v1/lists', | ||
qs: options | ||
}; | ||
sendRequest(call, cb); | ||
} | ||
}; | ||
|
||
function sendRequest (call, cb) { | ||
if (!self.key && !self.token) { return cb(new Error("You need to provide either a token or a key.")); } | ||
|
||
call.qs = call.qs || {}; | ||
if (self.key) { | ||
call.qs.hapikey = self.key; | ||
} else if (self.token) { | ||
call.qs.access_token = self.token; | ||
} | ||
|
||
console.log(call); | ||
request(call, handleResponse(cb)); | ||
} | ||
|
||
function handleResponse (cb) { | ||
return function (err, res, data) { | ||
if (err) { | ||
console.log('error'); | ||
console.log(err); | ||
return cb(err); | ||
} | ||
console.log('res'); | ||
console.log(require('util').inspect(res)); | ||
console.log('data'); | ||
console.log(require('util').inspect(data)); | ||
if (typeof data === 'string') { | ||
try { | ||
var parsed = JSON.parse(data); | ||
data = parsed; | ||
} catch (e) { | ||
console.log('error parsing response'); | ||
} | ||
} | ||
if (data.error) { return cb(new Error(data.error)); } | ||
if (data && data.length && data.length > 0 && data[0].error_message) { return cb(new Error(data[0].error_message)); } | ||
return cb(null, data); | ||
} | ||
} | ||
|
||
return { | ||
contacts: contacts, | ||
lists: lists, | ||
useToken: useToken, | ||
useKey: useKey | ||
} | ||
} | ||
|
||
module.exports = Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "hubspot", | ||
"version": "0.0.1", | ||
"description": "A node wrapper for the HubSpot API", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"hubspot" | ||
], | ||
"author": "Brian Falk", | ||
"license": "BSD" | ||
} |