Skip to content

Commit

Permalink
Merge pull request #109 from pipedrive/oauth
Browse files Browse the repository at this point in the history
OAuth2 access token support
  • Loading branch information
ziimk authored Jan 16, 2020
2 parents fb811bf + 19a5d7c commit 11e0a1c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 26 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ npm install pipedrive

## Roadmap & known issues
- [Missing async/await promise support](https://github.com/pipedrive/client-nodejs/issues/81)
- [Missing oauth 2.0 support](https://github.com/pipedrive/client-nodejs/issues/78)

## API Documentation
The Pipedrive REST API documentation can be found at https://developers.pipedrive.com/v1
Expand Down Expand Up @@ -38,6 +37,12 @@ This Pipedrive API client is distributed under the MIT licence.
- Run unit and integration tests
- Make PR

## Options
* `strictMode` - In strict mode `*_id` items in the responses are numeric IDs. Default is *false* in which case expanded
objects are returned. Strict mode is recommended and is likely to be the default in the future.
* `oauth` - whether the API token is to be used as OAuth bearer token instead of classic API key (default is *false*).
When setting `oauth` to true your application code must take care of fetching, storing and refershing the tokens.

# How to use
With a pre-set API token:
```js
Expand All @@ -64,13 +69,15 @@ pipedrive.Deals.getAll({}, function(err, deals) {
### Supported objects

* Activities
* ActivityFields
* ActivityTypes
* Authorizations
* Currencies
* CompanyFeatures,
* CompanySettings,
* Deals
* DealFields
* EmailThreads
* Files
* Filters
* Goals
Expand All @@ -87,6 +94,7 @@ pipedrive.Deals.getAll({}, function(err, deals) {
* SearchResults
* Stages
* Users
* userFields
* Webhooks

### Supported operations for object collections
Expand Down
3 changes: 1 addition & 2 deletions lib/Pipedrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

var defaults = {
strictMode: false,
apiHost: null,
apiVersion: null
oauth: false,
};

options = _.extend({}, defaults, options);
Expand Down
5 changes: 3 additions & 2 deletions lib/apiUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

module.exports = function apiUrl(path, options, tokenNeeded) {
var queryObj = {};
if (tokenNeeded) {

if (!options.oauth && tokenNeeded) {
queryObj.api_token = options.apiToken;
}
if (options.strictMode === true) {
Expand All @@ -19,4 +20,4 @@

return baseUri + '/' + path + (_.keys(queryObj).length > 0 ? '?' + qs.stringify(queryObj) : '');
};
})();
})();
2 changes: 2 additions & 0 deletions lib/blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Scoped in API under /v1/{objectName} standard namespaces.
exports.apiObjects = [
'activities',
'activityFields',
'activityTypes',
'authorizations',
'companyFeatures',
Expand All @@ -32,6 +33,7 @@ exports.apiObjects = [
'searchResults',
'stages',
'users',
'userFields',
'webhooks'
];

Expand Down
10 changes: 7 additions & 3 deletions lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
requestOptions;

this.options = options || {};
this.options.method = this.options.method || method || 'GET';
this.options.method = this.options.method || method || 'GET';

if(this.options.query) {
query = typeof this.options.query == 'string' ? this.options.query : qs.stringify(this.options.query);
query = typeof this.options.query == 'string' ? this.options.query : qs.stringify(this.options.query);
url += (url.match(/\?/) ? '&' : '?') + query;
}

Expand Down Expand Up @@ -76,6 +76,10 @@
headers['content-type'] = this.options.json ? 'application/json' : 'application/x-www-form-urlencoded';
}

if (options.accessToken) {
headers['Authorization'] = 'Bearer ' + options.accessToken;
}

requestOptions = {
method: method,
maxResponseLength: 10 * 1024 * 1024,
Expand Down Expand Up @@ -124,4 +128,4 @@

util.inherits(Request, EventEmitter);

})();
})();
47 changes: 31 additions & 16 deletions lib/restHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

this.options = _.extend({}, defaults, options || {});

this.resolveOptions = function (options) {
options = options || {};
if(this.options.oauth) {
options.accessToken = this.options.apiToken;
}
return options;
};

return this;
}

Expand Down Expand Up @@ -82,9 +90,9 @@
// GET /items
RestHandlers.prototype.listItems = function(object, params, callback) {
var self = this,
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
dataObject = object == 'authorizations' ? { multipart: false, data: paramsToSupply } : { query: qs.stringify(paramsToSupply) },
req = rest[object == 'authorizations' ? 'post' : 'get'](apiUrl(object, this.options, false), dataObject);
req = rest[object == 'authorizations' ? 'post' : 'get'](apiUrl(object, this.options, false), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('GET', object, data, callback, req, res);
Expand All @@ -96,9 +104,9 @@
// GET /items/find
RestHandlers.prototype.findItems = function(object, params, callback) {
var self = this,
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
dataObject = { query: qs.stringify(paramsToSupply) },
req = rest.get(apiUrl(object + '/find', this.options, false), dataObject);
req = rest.get(apiUrl(object + '/find', this.options, false), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('GET', object, data, callback, req, res);
Expand All @@ -110,9 +118,9 @@
// GET /items/timeline
RestHandlers.prototype.timelineItems = function(object, params, callback) {
var self = this,
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
dataObject = { query: qs.stringify(paramsToSupply) },
req = rest.get(apiUrl(object + '/timeline', this.options, false), dataObject);
req = rest.get(apiUrl(object + '/timeline', this.options, false), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('GET', object, data, callback, req, res);
Expand All @@ -124,9 +132,9 @@
// GET /searchResults/field
RestHandlers.prototype.searchFields = function(object, params, callback) {
var self = this,
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
dataObject = { query: qs.stringify(paramsToSupply) },
req = rest.get(apiUrl(object + '/field', this.options, false), dataObject);
req = rest.get(apiUrl(object + '/field', this.options, false), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('GET', object, data, callback, req, res);
Expand All @@ -138,8 +146,9 @@
// GET /items/5
RestHandlers.prototype.getItem = function(object, id, callback, params) {
var self = this,
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken ? { api_token: this.options.apiToken } : {}),
req = rest.get(apiUrl(object + '/' + id, this.options, false), { json: true, query: qs.stringify(paramsToSupply) });
paramsToSupply = _.extend({}, _.isObject(params) ? params : {}, this.options.apiToken && !this.options.oauth ? { api_token: this.options.apiToken } : {}),
dataObject = { json: true, query: qs.stringify(paramsToSupply) },
req = rest.get(apiUrl(object + '/' + id, this.options, false), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('GET', object, data, callback, req, res);
Expand All @@ -153,7 +162,8 @@
var self = this,
multipart_objects = ['files'],
multipart = (_.indexOf(multipart_objects, object) != -1),
req = rest.post(apiUrl(object, this.options, true), { json: true, multipart: multipart, data: params });
dataObject = { json: true, multipart: multipart, data: params },
req = rest.post(apiUrl(object, this.options, true), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('POST', object, data, callback, req, res);
Expand All @@ -165,7 +175,8 @@
// PUT /items/5
RestHandlers.prototype.editItem = function(itemId, object, params, callback) {
var self = this,
req = rest.put(apiUrl(object + '/' + itemId, this.options, true), { json: true, multipart: false, data: params });
dataObject = { json: true, multipart: false, data: params },
req = rest.put(apiUrl(object + '/' + itemId, this.options, true), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('PUT', object, data, callback, req, res);
Expand All @@ -177,7 +188,8 @@
// DELETE /items/5
RestHandlers.prototype.removeItem = function(itemId, object, params, callback) {
var self = this,
req = rest.del(apiUrl(itemId ? object + '/' + itemId : object, this.options, true), { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { id: itemId }) });
dataObject = { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { id: itemId }) },
req = rest.del(apiUrl(itemId ? object + '/' + itemId : object, this.options, true), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('DELETE', object, data, (_.isFunction(params) ? params : callback), req, res);
Expand All @@ -189,7 +201,8 @@
// DELETE /items
RestHandlers.prototype.removeManyItems = function(itemIds, object, params, callback) {
var self = this,
req = rest.del(apiUrl(object, this.options, true), { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { ids: itemIds }) });
dataObject = { json: true, multipart: false, data: (_.isObject(params) && !_.isFunction(params) ? params : { ids: itemIds }) },
req = rest.del(apiUrl(object, this.options, true), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('DELETE', object, data, (_.isFunction(params) ? params : callback), req, res);
Expand All @@ -205,7 +218,8 @@
return false;
}
var self = this,
req = rest.post(apiUrl(object + '/' + whichId + '/merge', this.options, true), { json: true, multipart: false, data: { merge_with_id: withId } });
dataObject = { json: true, multipart: false, data: { merge_with_id: withId } },
req = rest.post(apiUrl(object + '/' + whichId + '/merge', this.options, true), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('POST', object, data, callback, req, res);
Expand All @@ -220,7 +234,8 @@
}

var self = this,
req = rest.post(apiUrl(object + '/' + whichId + '/duplicate', this.options, true), { json: true, multipart: false, data: {} });
dataObject = { json: true, multipart: false, data: {} },
req = rest.post(apiUrl(object + '/' + whichId + '/duplicate', this.options, true), this.resolveOptions(dataObject));

req.on('complete', function(data, res) {
self.genericResponse('POST', object, data, callback, req, res);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pipedrive",
"version": "9.1.1",
"version": "9.2.0",
"description": "Pipedrive REST client for NodeJS",
"keywords": [
"pipedrive",
Expand Down

0 comments on commit 11e0a1c

Please sign in to comment.