Skip to content

Commit 96b0324

Browse files
Merge pull request #739 from watson-developer-cloud/apikey-username
perf: use token manager when user gives username as `apikey`
2 parents 038c955 + 6c9a989 commit 96b0324

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/base_service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function hasCredentials(obj: any): boolean {
7373
}
7474

7575
function hasBasicCredentials(obj: any): boolean {
76-
return obj && obj.username && obj.password;
76+
return obj && obj.username && obj.password && obj.username !== 'apikey';
7777
}
7878

7979
export class BaseService {
@@ -131,6 +131,11 @@ export class BaseService {
131131
iamAccessToken: _options.iam_access_token,
132132
iamUrl: _options.iam_url
133133
});
134+
} else if (_options.username === 'apikey') {
135+
this.tokenManager = new IamTokenManagerV1({
136+
iamApikey: _options.password,
137+
iamUrl: _options.iam_url
138+
});
134139
} else {
135140
this.tokenManager = null;
136141
}

test/unit/test.base_service.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,15 @@ describe('BaseService', function() {
231231
assert.deepEqual(actual, expected);
232232
assert.notEqual(instance.tokenManager, null);
233233
});
234+
235+
it('should create a token manager instance if username is `apikey` and use the password as the API key', function() {
236+
const apikey = 'abcd-1234';
237+
const instance = new TestService({
238+
username: 'apikey',
239+
password: apikey,
240+
});
241+
assert.notEqual(instance.tokenManager, null);
242+
assert.equal(instance.tokenManager.iamApikey, apikey);
243+
assert.equal(instance._options.headers, undefined);
244+
});
234245
});

0 commit comments

Comments
 (0)