Skip to content

Commit

Permalink
add support for passing in extra authorization into the headers (clie…
Browse files Browse the repository at this point in the history
…nt secret/url)
  • Loading branch information
Evelyn Chan committed Feb 12, 2015
1 parent c40bcd3 commit f8da450
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 33 deletions.
2 changes: 1 addition & 1 deletion api-angular-min.js

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

30 changes: 24 additions & 6 deletions api-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
body: body,
url: this._endpoint + "?" + queryString,
cookies: this._options.cookies,
clientId: this._options.clientId
clientId: this._options.clientId,
secret: this._options.secret
})
.then(parseResponseBody)
.then(resolveQueue.bind(this, this._queue))
Expand Down Expand Up @@ -319,6 +320,11 @@
return obj1;
}

// Adds other configuration after the api has already been initialized
TaggedApi.prototype.addOptions = function(options) {
this._options = mergeRecursive(options || {}, this._options);
}

if (typeof exports !== 'undefined') {
// We're in a nodejs environment, export this module
module.exports = TaggedApi;
Expand All @@ -336,16 +342,22 @@
var Promise = context.Promise || require('bluebird');
var SESSION_COOKIE_NAME_REGEX = /(?:^| )S=/;

AngularAdapter.$inject = ['$http', '$document'];
function AngularAdapter($http, $document) {
AngularAdapter.$inject = ['$http', '$document', '$window'];
function AngularAdapter($http, $document, $window) {
this._$http = $http;
this._$document = $document;
this._$window = $window;
}

AngularAdapter.prototype.post = function(req) {
var headers = {
'x-tagged-client-id': req.clientId,
'x-tagged-client-url': this._$window.location
};
return this._$http.post(req.url, req.body, {
timeout: 10000,
transformResponse: transformResponse
transformResponse: transformResponse,
headers: headers
}).then(formatResponse);
};

Expand Down Expand Up @@ -437,8 +449,14 @@
}, angularAdapter);

// Wrap `execute()` in an Angular promise
api.execute = function(method, params) {
return $q.when(TaggedApi.prototype.execute.call(this, method, params));
api.execute = function(method, options) {
if (!options.clientId || typeof options.clientId === 'undefined') {
return $q.reject('Client ID is required to make API calls');
}

// append the client ID from the params sent from client
TaggedApi.prototype.execute.call(this, options.clientId);
return $q.when(TaggedApi.prototype.execute.call(this, method, options.params || {}));
};

return api;
Expand Down
12 changes: 9 additions & 3 deletions lib/http_adapter/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
var Promise = context.Promise || require('bluebird');
var SESSION_COOKIE_NAME_REGEX = /(?:^| )S=/;

AngularAdapter.$inject = ['$http', '$document'];
function AngularAdapter($http, $document) {
AngularAdapter.$inject = ['$http', '$document', '$window'];
function AngularAdapter($http, $document, $window) {
this._$http = $http;
this._$document = $document;
this._$window = $window;
}

AngularAdapter.prototype.post = function(req) {
var headers = {
'x-tagged-client-id': req.clientId,
'x-tagged-client-url': this._$window.location
};
return this._$http.post(req.url, req.body, {
timeout: 10000,
transformResponse: transformResponse
transformResponse: transformResponse,
headers: headers
}).then(formatResponse);
};

Expand Down
6 changes: 6 additions & 0 deletions lib/http_adapter/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Http.prototype.post = function(req) {
headers['x-tagged-client-id'] = req.clientId;
}

if (req.secret) {
headers['x-tagged-client-secret'] = req.secret;
}

console.info('post /api headers: ', headers);

return this._request.postAsync({
url: req.url,
body: req.body,
Expand Down
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
body: body,
url: this._endpoint + "?" + queryString,
cookies: this._options.cookies,
clientId: this._options.clientId
clientId: this._options.clientId,
secret: this._options.secret
})
.then(parseResponseBody)
.then(resolveQueue.bind(this, this._queue))
Expand Down Expand Up @@ -319,6 +320,11 @@
return obj1;
}

// Adds other configuration after the api has already been initialized
TaggedApi.prototype.addOptions = function(options) {
this._options = mergeRecursive(options || {}, this._options);
}

if (typeof exports !== 'undefined') {
// We're in a nodejs environment, export this module
module.exports = TaggedApi;
Expand Down
10 changes: 8 additions & 2 deletions lib/wrappers/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
}, angularAdapter);

// Wrap `execute()` in an Angular promise
api.execute = function(method, params) {
return $q.when(TaggedApi.prototype.execute.call(this, method, params));
api.execute = function(method, options) {
if (!options.clientId || typeof options.clientId === 'undefined') {
return $q.reject('Client ID is required to make API calls');
}

// append the client ID from the params sent from client
TaggedApi.prototype.execute.call(this, options.clientId);
return $q.when(TaggedApi.prototype.execute.call(this, method, options.params || {}));
};

return api;
Expand Down
21 changes: 20 additions & 1 deletion test/unit/lib/http_adapter/angular_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ describe('Angular Adapter', function() {
cookie: 'foo=bar; S=test_session_token'
}
];
this.adapter = new AngularAdapter(this.$http, this.$document);
this.$window = {
location: '/foo'
};
this.adapter = new AngularAdapter(this.$http, this.$document, this.$window);
});

it('is a constructor', function() {
Expand Down Expand Up @@ -62,6 +65,22 @@ describe('Angular Adapter', function() {
});
result.should.become({ body: 'some data'});
});

it('sets client ID and url in headers', function() {
var url = 'http://example.com/foo';
var body = 'post body';
var clientId = 'testclientid';
var location = '/foo';
this.adapter.post({
url: url,
body: body,
clientId: clientId
});
this.$http.post.calledOnce.should.be.true;
this.$http.post.lastCall.args[2].should.have.property('headers');
this.$http.post.lastCall.args[2].headers['x-tagged-client-id'].should.equal(clientId);
this.$http.post.lastCall.args[2].headers['x-tagged-client-url'].should.equal(location);
});
});

describe('getSessionToken', function() {
Expand Down
36 changes: 17 additions & 19 deletions test/unit/lib/http_adapter/node_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ describe('Node HTTP Adapter', function() {
describe('post()', function() {
beforeEach(function() {
this.http = new Http(this.request);
this.url = 'http://example.com/foo';
this.body = 'post body';
});

it('posts body to specified url', function() {
var url = 'http://example.com/foo';
var body = 'post body';
this.http.post({
url: url,
body: body
url: this.url,
body: this.body
});
this.request.post.calledOnce.should.be.true;
this.request.post.lastCall.args[0].should.have.property('url', url);
this.request.post.lastCall.args[0].should.have.property('url', this.url);
});

it('posts cookies to specified url', function() {
Expand All @@ -53,29 +53,28 @@ describe('Node HTTP Adapter', function() {
this.request.post.lastCall.args[0].headers.should.have.property('Cookie', cookies);
});

it('posts client id header if provided', function() {
var url = 'http://example.com/foo';
var body = 'post body';
it('posts client id and client secret to header if provided', function() {
var cookies = 'testcookies=1';
var clientId = 'testClientId';
var clientSecret = 'testSecret';
this.http.post({
url: url,
body: body,
url: this.url,
body: this.body,
cookies: cookies,
clientId: clientId
clientId: clientId,
secret: clientSecret
});
this.request.post.calledOnce.should.be.true;
this.request.post.lastCall.args[0].should.have.property('headers');
this.request.post.lastCall.args[0].headers.should.have.property('x-tagged-client-id', clientId);
this.request.post.lastCall.args[0].headers.should.have.property('x-tagged-client-secret', clientSecret);
});

it('resolves with response', function() {
var url = 'http://example.com/foo';
var body = 'post body';
var expectedBody = 'expected body';
var result = this.http.post({
url: url,
body: body
url: this.url,
body: this.body
});

this.request.respondWith(null, {
Expand All @@ -88,17 +87,16 @@ describe('Node HTTP Adapter', function() {
});

it('rejects with error', function() {
var url = 'http://example.com/foo';
var body = 'post body';
var error = 'test error';
var result = this.http.post({
url: url,
body: body
url: this.url,
body: this.body
});

this.request.respondWith(error);

return result.should.be.rejectedWith(error);
});

});
});
1 change: 1 addition & 0 deletions test/unit/lib/wrappers/angular_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ describe('Angular Wrapper', function() {
var api = this.module.factory.lastCall.args[1]($http, $q);
this.TaggedApi.lastCall.args[2].should.be.instanceOf(this.TaggedApi.AngularAdapter);
});

});

0 comments on commit f8da450

Please sign in to comment.