diff --git a/example.js b/example.js index 59e97e8..4266cc2 100644 --- a/example.js +++ b/example.js @@ -25,7 +25,7 @@ app.get("YOUR_CALLBACK_URL", function (req, res) { var accessToken = results[0], accessTokenSecret = results[1], userId = results[2].encoded_user_id; - return client.requestResource("/profile.json", "GET", accessToken, accessTokenSecret).then(function (results) { + return client.get("/profile.json", accessToken, accessTokenSecret).then(function (results) { var response = results[0]; res.send(response); }); @@ -34,4 +34,4 @@ app.get("YOUR_CALLBACK_URL", function (req, res) { }); }); -app.listen(1024); +app.listen(3000); \ No newline at end of file diff --git a/fitbit-api-client.js b/fitbit-api-client.js index f35c3ca..2dd753b 100644 --- a/fitbit-api-client.js +++ b/fitbit-api-client.js @@ -1,5 +1,5 @@ var OAuth = require("oauth").OAuth, - Q = require("q"); + Promise = require("bluebird"); function FitbitApiClient(consumerKey, consumerSecret) { this.oauth = new OAuth( @@ -15,23 +15,38 @@ function FitbitApiClient(consumerKey, consumerSecret) { FitbitApiClient.prototype = { getRequestToken: function () { - var deferred = Q.defer(); - this.oauth.getOAuthRequestToken(deferred.makeNodeResolver()); - return deferred.promise; + var getRequestToken = Promise.promisify(this.oauth.getOAuthRequestToken, this.oauth); + return getRequestToken(); }, getAccessToken: function (requestToken, requestTokenSecret, verifier) { - var deferred = Q.defer(); - this.oauth.getOAuthAccessToken(requestToken, requestTokenSecret, verifier, deferred.makeNodeResolver()); - return deferred.promise; + var getAccessToken = Promise.promisify(this.oauth.getOAuthAccessToken, this.oauth); + return getAccessToken(requestToken, requestTokenSecret, verifier); + }, + + get: function (path, accessToken, accessTokenSecret, userId) { + var getResource = Promise.promisify(this.oauth.get, this.oauth); + return getResource(getUrl(path, userId), accessToken, accessTokenSecret); + }, + + post: function (path, accessToken, accessTokenSecret, data, userId) { + var postResource = Promise.promisify(this.oauth.post, this.oauth); + return postResource(getUrl(path, userId), accessToken, accessTokenSecret, data); }, - requestResource: function (path, method, accessToken, accessTokenSecret, userId) { - var url = "https://api.fitbit.com/1/user/" + (userId || "-") + path; - var deferred = Q.defer(); - this.oauth.getProtectedResource(url, method, accessToken, accessTokenSecret, deferred.makeNodeResolver()); - return deferred.promise; + put: function (path, accessToken, accessTokenSecret, data, userId) { + var putResource = Promise.promisify(this.oauth.put, this.oauth); + return putResource(getUrl(path, userId), accessToken, accessTokenSecret, data); + }, + + delete: function (path, accessToken, accessTokenSecret, userId) { + var deleteResource = Promise.promisify(this.oauth.delete, this.oauth); + return deleteResource(getUrl(path, userId), accessToken, accessTokenSecret); } }; +function getUrl(path, userId) { + return url = "https://api.fitbit.com/1/user/" + (userId || "-") + path; +} + module.exports = FitbitApiClient; \ No newline at end of file diff --git a/package.json b/package.json index b3f0fcb..a74b01d 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,29 @@ { - "name": "fitbit-node", - "version": "0.1.0", - "description": "A Fitbit API client library written in Node.js.", - "main": "fitbit-api-client.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "https://github.com/lukasolson/fitbit-node" - }, - "keywords": [ - "Fitbit", - "Node" - ], - "author": "Lukas Olson ", - "license": "BSD", - "bugs": { - "url": "https://github.com/lukasolson/fitbit-node/issues" - }, - "dependencies": { - "oauth": "~0.9.11", - "q": "~1.0.0" - }, - "devDependencies": { - "express": "~3.4.8" - } + "name": "fitbit-node", + "version": "1.0.0", + "description": "A Fitbit API client library written in Node.js.", + "main": "fitbit-api-client.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/lukasolson/fitbit-node" + }, + "keywords": [ + "Fitbit", + "Node" + ], + "author": "Lukas Olson ", + "license": "BSD", + "bugs": { + "url": "https://github.com/lukasolson/fitbit-node/issues" + }, + "dependencies": { + "bluebird": "^2.9.34", + "oauth": "^0.9.13" + }, + "devDependencies": { + "express": "^4.13.1" + } } diff --git a/readme.md b/readme.md index 0e9d932..d228e4e 100644 --- a/readme.md +++ b/readme.md @@ -12,10 +12,19 @@ An API client library written for Fitbit in NodeJS. Constructor. Use the `consumerKey` and `consumerSecret` provided to you when you registered your app on [dev.fitbit.com](http://dev.fitbit.com/). #### `getRequestToken()` -Get a request token. This is the first step of the OAuth flow. Returns a Q promise. When this promise is resolved with a request token, forward the user to the Fitbit site (e.g., http://www.fitbit.com/oauth/authorize?oauth_token=) for authentication. (You can substitute "authenticate" instead of "authorize" in the URL if you do not wish to forward to the Fitbit site for authentication next time you request an access token.) +Get a request token. This is the first step of the OAuth flow. Returns a promise. When this promise is resolved with a request token, forward the user to the Fitbit site (e.g., http://www.fitbit.com/oauth/authorize?oauth_token=) for authentication. (You can substitute "authenticate" instead of "authorize" in the URL if you do not wish to forward to the Fitbit site for authentication next time you request an access token.) #### `getAccessToken(requestToken, requestTokenSecret, verifier)` -After the user authorizes with Fitbit, he/she will be forwarded to the URL you specify in your Fitbit API application settings, and the `requestToken` and `verifier` will be in the URL. Use these, along with the `requestTokenSecret` you received above to request an access token in order to make API calls. Returns a Q promise. +After the user authorizes with Fitbit, he/she will be forwarded to the URL you specify in your Fitbit API application settings, and the `requestToken` and `verifier` will be in the URL. Use these, along with the `requestTokenSecret` you received above to request an access token in order to make API calls. Returns a promise. -#### `requestResource(url, httpMethod, accessToken, accessTokenSecret, [userId])` -Make an API call to the Fitbit servers. (See [example.js](https://github.com/lukasolson/fitbit-node/blob/master/example.js) for an example.) Returns a Q promise. \ No newline at end of file +#### `get(url, accessToken, accessTokenSecret, [userId])` +Make a GET API call to the Fitbit servers. (See [example.js](https://github.com/lukasolson/fitbit-node/blob/master/example.js) for an example.) Returns a promise. + +#### `post(url, accessToken, accessTokenSecret, data, [userId])` +Make a POST API call to the Fitbit servers. Returns a promise. + +#### `put(url, accessToken, accessTokenSecret, data, [userId])` +Make a PUT API call to the Fitbit servers. Returns a promise. + +#### `delete(url, accessToken, accessTokenSecret, [userId])` +Make a DELETE API call to the Fitbit servers. Returns a promise. \ No newline at end of file