Skip to content

Commit 3d28dde

Browse files
committed
Allow outgoing request with no Authorization header
`predicsisAPI.setOauthToken(false)`
1 parent 4789693 commit 3d28dde

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ angular.module('YourAngularApplication', ['predicsis.jsSDK'])
4747
});
4848
```
4949

50+
> If you want to send API requests with no Authorization header, you must explicitly use `predicsisAPI.setOauthToken(false);`
51+
5052
Default values:
5153
- api endpoint: `https://api.predicsis.com`
5254
- error handler: `throw Error(response);`

app/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ angular.module('demo-jsSDK', ['predicsis.jsSDK'])
44
})
55

66
.run(function(predicsisAPI) {
7-
predicsisAPI.setOauthToken('d75d2750e04ab0c3c6f44a20271496098600d22e602a6e002deacfa5b07be6c5');
7+
predicsisAPI.setOauthToken(false);
88
predicsisAPI.setErrorHandler(console.log.bind(console));
99
})
1010

dist/predicsis-jsSDK.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,36 @@ angular
55
.module('predicsis.jsSDK', ['predicsis.jsSDK.models', 'predicsis.jsSDK.helpers', 'restangular'])
66
.provider('predicsisAPI', function () {
77
'use strict';
8+
89
var errorHandler = function(response) { throw Error(response); };
910
var baseURL = 'https://api.predicsis.com';
1011
var oauthToken = 'no-token-defined';
12+
var requestHeaders = {
13+
Accept: 'application/json'
14+
};
15+
16+
this.setErrorHandler = function(handler) { errorHandler = handler; };
1117

1218
this.setBaseUrl = function(url) { baseURL = url; };
1319
this.getBaseUrl = function() { return baseURL; };
1420

15-
this.setOauthToken = function(token) { oauthToken = token; };
1621
this.getOauthToken = function() { return oauthToken; };
1722
this.hasOauthToken = function() { return Boolean(oauthToken === undefined); };
18-
19-
this.setErrorHandler = function(handler) { errorHandler = handler; };
23+
this.setOauthToken = function(token) {
24+
if (token !== false) {
25+
console.log(token);
26+
requestHeaders.Authorization = 'Bearer ' + token;
27+
oauthToken = token;
28+
}
29+
};
2030

2131
this.$get = function(Restangular, uploadHelper,
2232
Datafiles, Datasets, Dictionaries, Jobs, Modalities, Models, OauthTokens, OauthApplications,
2333
PreparationRules, Projects, Reports, UserSettings, Sources, Users, Variables) {
2434
var self = this;
2535

2636
Restangular.setBaseUrl(this.getBaseUrl());
27-
Restangular.setDefaultHeaders({ accept: 'application/json', Authorization: 'Bearer ' + this.getOauthToken() });
37+
Restangular.setDefaultHeaders(requestHeaders);
2838
Restangular.setErrorInterceptor(function(response) { errorHandler(response); });
2939
Jobs.setErrorHandler(function(err) {
3040
err = {
@@ -73,7 +83,7 @@ angular
7383
_restangular: Restangular,
7484
setOauthToken: function(token) {
7585
self.setOauthToken(token);
76-
Restangular.setDefaultHeaders({ accept: 'application/json', Authorization: token });
86+
Restangular.setDefaultHeaders(requestHeaders);
7787
},
7888
setErrorHandler: function(handler) {
7989
self.setErrorHandler(handler);
@@ -3297,8 +3307,7 @@ angular
32973307
.service('uploadHelper', function($rootScope, $injector) {
32983308
'use strict';
32993309

3300-
const http = { CREATED: 201 };
3301-
3310+
var HTTP = { CREATED: 201 };
33023311
var concurrentUploads = {};
33033312
var Sources = $injector.get('Sources');
33043313

@@ -3311,7 +3320,7 @@ angular
33113320
key: getKey(credential, file.name),
33123321
AWSAccessKeyId: credential.aws_access_key_id,
33133322
'Content-Type': 'multipart/form-data',
3314-
success_action_status: http.CREATED,
3323+
success_action_status: HTTP.CREATED,
33153324
acl: 'private',
33163325
policy: credential.policy,
33173326
signature: credential.signature
@@ -3332,7 +3341,7 @@ angular
33323341
delete concurrentUploads[uploadObject.id];
33333342
uploadObject.isUploading = false;
33343343

3335-
if(xhr2.status === http.CREATED) {
3344+
if(xhr2.status === HTTP.CREATED) {
33363345
$rootScope.$broadcast('jsSDK.upload.uploaded', uploadObject);
33373346
} else {
33383347
$rootScope.$broadcast('jsSDK.upload.error', { upload: uploadObject, request: xhr2 });

dist/predicsis-jsSDK.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/predicsis-jsSDK.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/predicsis-jsSDK.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,32 @@ angular
99
var errorHandler = function(response) { throw Error(response); };
1010
var baseURL = 'https://api.predicsis.com';
1111
var oauthToken = 'no-token-defined';
12+
var requestHeaders = {
13+
Accept: 'application/json'
14+
};
15+
16+
this.setErrorHandler = function(handler) { errorHandler = handler; };
1217

1318
this.setBaseUrl = function(url) { baseURL = url; };
1419
this.getBaseUrl = function() { return baseURL; };
1520

16-
this.setOauthToken = function(token) { oauthToken = token; };
1721
this.getOauthToken = function() { return oauthToken; };
1822
this.hasOauthToken = function() { return Boolean(oauthToken === undefined); };
19-
20-
this.setErrorHandler = function(handler) { errorHandler = handler; };
23+
this.setOauthToken = function(token) {
24+
if (token !== false) {
25+
console.log(token);
26+
requestHeaders.Authorization = 'Bearer ' + token;
27+
oauthToken = token;
28+
}
29+
};
2130

2231
this.$get = function(Restangular, uploadHelper,
2332
Datafiles, Datasets, Dictionaries, Jobs, Modalities, Models, OauthTokens, OauthApplications,
2433
PreparationRules, Projects, Reports, UserSettings, Sources, Users, Variables) {
2534
var self = this;
2635

2736
Restangular.setBaseUrl(this.getBaseUrl());
28-
Restangular.setDefaultHeaders({ accept: 'application/json', Authorization: 'Bearer ' + this.getOauthToken() });
37+
Restangular.setDefaultHeaders(requestHeaders);
2938
Restangular.setErrorInterceptor(function(response) { errorHandler(response); });
3039
Jobs.setErrorHandler(function(err) {
3140
err = {
@@ -74,7 +83,7 @@ angular
7483
_restangular: Restangular,
7584
setOauthToken: function(token) {
7685
self.setOauthToken(token);
77-
Restangular.setDefaultHeaders({ accept: 'application/json', Authorization: token });
86+
Restangular.setDefaultHeaders(requestHeaders);
7887
},
7988
setErrorHandler: function(handler) {
8089
self.setErrorHandler(handler);

0 commit comments

Comments
 (0)