Skip to content

Commit

Permalink
Replace Q with bluebird, allow post data to be sent with PUT/POST, up…
Browse files Browse the repository at this point in the history
…date API
  • Loading branch information
lukasolson committed Jul 27, 2015
1 parent 9c4fce2 commit 9470e68
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 45 deletions.
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -34,4 +34,4 @@ app.get("YOUR_CALLBACK_URL", function (req, res) {
});
});

app.listen(1024);
app.listen(3000);
39 changes: 27 additions & 12 deletions fitbit-api-client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var OAuth = require("oauth").OAuth,
Q = require("q");
Promise = require("bluebird");

function FitbitApiClient(consumerKey, consumerSecret) {
this.oauth = new OAuth(
Expand All @@ -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;
54 changes: 27 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <olson.lukas@gmail.com>",
"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 <olson.lukas@gmail.com>",
"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"
}
}
17 changes: 13 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<requestToken>) 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=<requestToken>) 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.
#### `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.

0 comments on commit 9470e68

Please sign in to comment.