Skip to content

Commit 8c17abb

Browse files
Pull request #6: Add tracking to requests
Merge in SDK/node_telesign from feature/EOA2028 to developer * commit '8b66cb15d7a49cb5227b29ed823db1ddc3de582e': Fix unit test Adjust tests Added tracking to requests
2 parents 1e472d3 + 8b66cb1 commit 8c17abb

File tree

5 files changed

+82
-19
lines changed

5 files changed

+82
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "telesignsdk",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"description": "Official TeleSign SDK for Rest APIs including Messaging (SMS), Score, PhoneID, Voice, and AppVerify",
55
"repository": {
66
"type": "git",

src/RestClient.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const crypto = require("crypto");
2-
const fs = require('fs');
3-
const path = require('path');
42
const Constants = require('./Constants.js');
3+
const { getInstalledVersion } = require('./Util.js');
54

65
/***
76
* The TeleSign RestClient is a generic HTTP REST client that can be extended to make
@@ -17,25 +16,28 @@ class RestClient {
1716
restEndpoint = "https://rest-api.telesign.com",
1817
timeout = 15000,
1918
userAgent = null,
20-
contentType = "application/x-www-form-urlencoded") {
19+
source = "node_telesign",
20+
sdkVersionOrigin = null,
21+
sdkVersionDependency = null,
22+
contentType = "application/x-www-form-urlencoded" ) {
2123
this.requestWrapper = requestWrapper
2224
this.customerId = customerId;
2325
this.apiKey = apiKey;
2426
this.restEndpoint = (restEndpoint === null ? "https://rest-api.telesign.com" : restEndpoint);
2527
this.timeout = timeout;
2628
this.contentType = contentType ;
29+
const currentVersionSdk = sdkVersionOrigin || getInstalledVersion()
2730

2831
try {
2932
if (userAgent === null) {
30-
const packageJsonPath = path.join(__dirname, '..', 'package.json')
31-
const packageJson = fs.readFileSync(packageJsonPath, 'utf8');
32-
const packageData = JSON.parse(packageJson);
33-
const version = packageData.version;
34-
this.userAgent = `TeleSignSDK/ECMAScript-Node v ${version}`
33+
this.userAgent = `TeleSignSDK/ECMAScript-Node`
3534
+ ` ${process.arch}`
3635
+ `/${process.platform}`
3736
+ ` ${process.release.name}`
38-
+ `/${process.version}`; // Generates a Node useragent - helpful in diagnosing errors
37+
+ `/${process.version}` // Generates a Node useragent - helpful in diagnosing errors
38+
+ ` OriginatingSDK/${source}`
39+
+ ` SDKVersion/${currentVersionSdk}`
40+
+ (source !== "node_telesign" ? ` DependencySDKVersion/${sdkVersionDependency}` : ``);
3941
}
4042
}
4143
catch (err) {
@@ -173,7 +175,7 @@ class RestClient {
173175
nonce,
174176
this.userAgent,
175177
authMethod);
176-
178+
177179
var requestParams = {
178180
headers: headers,
179181
url: telesignURL,

src/TeleSign.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ module.exports = class TeleSign {
1212
apiKey,
1313
restEndpoint = "https://rest-api.telesign.com",
1414
timeout = 15000,
15-
useragent = null) {
15+
useragent = null,
16+
source = "node_telesign",
17+
sdkVersionOrigin = null,
18+
sdkVersionDependency = null) {
1619

1720
const requestWrapper = new FetchRequestWrapper();
18-
this.rest = new RestClient(requestWrapper, customerId, apiKey, restEndpoint, timeout, useragent);
21+
this.rest = new RestClient(requestWrapper, customerId, apiKey, restEndpoint, timeout, useragent, source, sdkVersionOrigin, sdkVersionDependency);
1922
this.sms = new MessagingClient(requestWrapper, customerId, apiKey, restEndpoint, timeout, useragent);
2023
this.voice = new VoiceClient(requestWrapper, customerId, apiKey, restEndpoint, timeout, useragent);
2124
this.score = new ScoreClient(requestWrapper, customerId, apiKey, restEndpoint, timeout, useragent);

src/Util.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
function getInstalledVersion() {
5+
try {
6+
const packageJsonPath = path.join(__dirname, '..', 'package.json')
7+
const packageJson = fs.readFileSync(packageJsonPath, 'utf8');
8+
const packageData = JSON.parse(packageJson);
9+
const version = packageData.version;
10+
return version;
11+
} catch (err) {
12+
return null;
13+
}
14+
}
15+
16+
module.exports = {
17+
getInstalledVersion,
18+
}

test/RestClient.test.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function restClient() {
5454
it('should set user agent based on OS information', () => {
5555
const telesign = new RestClient(requestWrapper, customerId, apiKey);
5656

57-
expect(telesign.userAgent).toContain('TeleSignSDK/ECMAScript-Node v');
57+
expect(telesign.userAgent).toContain('TeleSignSDK/ECMAScript-Node');
5858
});
5959

6060
it('should change the attribute restEndpoint', () => {
@@ -89,7 +89,8 @@ async function restClient() {
8989
it('should set userAgent to the expected value', () => {
9090
const rc = new RestClient(requestWrapper, customerId, apiKey);
9191

92-
expect(rc.userAgent).toContain('TeleSignSDK/ECMAScript-Node v ');
92+
expect(rc.userAgent).toContain('TeleSignSDK/ECMAScript-Node');
93+
expect(rc.userAgent).toContain('OriginatingSDK/node_telesign');
9394
});
9495

9596
it('should return default userAgent on error', () => {
@@ -375,7 +376,9 @@ async function restClient() {
375376
const expectedResponse = { message: 'Successful response' }
376377
var optionsSent = null
377378
const requestWrapper = new FetchRequestWrapperMock(response, null, expectedResponse, (options) => optionsSent = options);
379+
378380
const telesign = new RestClient(requestWrapper, 'customerId', 'apiKey');
381+
379382
const resource = '/test';
380383
const params = { key1: 'value1', key2: 'value2' }
381384
const nonce = 'A1592C6F-E384-4CDB-BC42-C3AB970369E9';
@@ -388,7 +391,10 @@ async function restClient() {
388391
expect(optionsSent.headers).toHaveProperty('Authorization', 'TSA customerId:y7PFf4BjQViy9TfeUTQutsQzKm/6T7NrklwOfaOTRKc=');
389392
expect(optionsSent.headers).toHaveProperty('Content-Type', '');
390393
expect(optionsSent.headers).toHaveProperty('Date', 'Wed, 14 Dec 2016 18:20:12 GMT');
391-
expect(optionsSent.headers).toHaveProperty('User-Agent', function(value) {expect(value).toContain('TeleSignSDK/ECMAScript-Node v 3.0.0')});
394+
expect(optionsSent.headers).toHaveProperty('User-Agent', function(value) {
395+
expect(value).toContain('TeleSignSDK/ECMAScript-Node')
396+
expect(value).toContain('OriginatingSDK/node_telesign')
397+
});
392398
expect(optionsSent.headers).toHaveProperty('x-ts-auth-method', 'HMAC-SHA256');
393399
expect(optionsSent.headers).toHaveProperty('x-ts-auth-method', 'HMAC-SHA256')
394400
expect(optionsSent.headers).toHaveProperty('x-ts-nonce', 'A1592C6F-E384-4CDB-BC42-C3AB970369E9')
@@ -483,7 +489,10 @@ async function restClient() {
483489
expect(optionsSent.headers).toHaveProperty('Authorization', 'TSA customerId:aQk5d8nanixOKIzrQfzIWjEqvVDxEuMOCoSoiH7Cnsc=');
484490
expect(optionsSent.headers).toHaveProperty('Content-Type', 'application/x-www-form-urlencoded');
485491
expect(optionsSent.headers).toHaveProperty('Date', 'Wed, 15 Dec 2016 18:20:12 GMT');
486-
expect(optionsSent.headers).toHaveProperty('User-Agent', function(value) {expect(value).toContain('TeleSignSDK/ECMAScript-Node v 3.0.0')});
492+
expect(optionsSent.headers).toHaveProperty('User-Agent', function(value) {
493+
expect(value).toContain('TeleSignSDK/ECMAScript-Node')
494+
expect(value).toContain('OriginatingSDK/node_telesign')
495+
});
487496
expect(optionsSent.headers).toHaveProperty('x-ts-auth-method', 'HMAC-SHA256')
488497
expect(optionsSent.headers).toHaveProperty('x-ts-nonce', 'FD7E3E50-6F1A-4BAF-9A5C-2F11B9A5B654')
489498
expect(optionsSent).toHaveProperty('method', 'POST');
@@ -517,7 +526,10 @@ async function restClient() {
517526
expect(optionsSent.headers).toHaveProperty('Authorization', 'TSA customerId:5/gV/TLGSxrKPCUsuAwBpu5ZFm/xNAQpPuMe+Jvtt1k=');
518527
expect(optionsSent.headers).toHaveProperty('Content-Type', 'application/x-www-form-urlencoded');
519528
expect(optionsSent.headers).toHaveProperty('Date', 'Wed, 15 Dec 2016 18:20:12 GMT');
520-
expect(optionsSent.headers).toHaveProperty('User-Agent', function(value) {expect(value).toContain('TeleSignSDK/ECMAScript-Node v 3.0.0')});
529+
expect(optionsSent.headers).toHaveProperty('User-Agent', function(value) {
530+
expect(value).toContain('TeleSignSDK/ECMAScript-Node')
531+
expect(value).toContain('OriginatingSDK/node_telesign')
532+
});
521533
expect(optionsSent.headers).toHaveProperty('x-ts-auth-method', 'HMAC-SHA256')
522534
expect(optionsSent.headers).toHaveProperty('x-ts-nonce', 'FD7E3E50-6F1A-4BAF-9A5C-2F11B9A5B654')
523535
expect(optionsSent).toHaveProperty('method', 'PUT');
@@ -540,6 +552,9 @@ async function restClient() {
540552
'https://rest-api.telesign.com',
541553
15000,
542554
null,
555+
'node_telesign',
556+
null,
557+
null,
543558
contentType
544559
);
545560
const jsonParams = { key: 'value' };
@@ -570,6 +585,9 @@ async function restClient() {
570585
'https://rest-api.telesign.com',
571586
15000,
572587
null,
588+
'node_telesign',
589+
null,
590+
null,
573591
'application/json'
574592
);
575593
const params = null
@@ -607,7 +625,29 @@ async function restClient() {
607625

608626
expect(telesign.phoneid.restEndpoint).toBe("https://rest-api.telesign.com");
609627
expect(telesign.phoneid.timeout).toBe(15000);
610-
expect(telesign.phoneid.userAgent).toContain('TeleSignSDK/ECMAScript-Node v');
628+
expect(telesign.phoneid.userAgent).toContain('TeleSignSDK/ECMAScript-Node');
629+
expect(telesign.phoneid.userAgent).toContain('OriginatingSDK/node_telesign');
630+
});
631+
632+
test('Use Telesign from FS', async () => {
633+
const telesign = new TeleSignSDK(
634+
customerId,
635+
apiKey,
636+
restEndpoint,
637+
15000,
638+
null,
639+
'node_telesign_enterprise',
640+
'1.0.0',
641+
'2.0.0'
642+
);
643+
644+
expect(telesign.rest.restEndpoint).toBe("https://rest-api.telesign.com");
645+
expect(telesign.rest.contentType).toBe("application/x-www-form-urlencoded");
646+
expect(telesign.rest.timeout).toBe(15000);
647+
expect(telesign.rest.userAgent).toContain('TeleSignSDK/ECMAScript-Node');
648+
expect(telesign.rest.userAgent).toContain('OriginatingSDK/node_telesign_enterprise');
649+
expect(telesign.rest.userAgent).toContain('SDKVersion/1.0.0');
650+
expect(telesign.rest.userAgent).toContain('DependencySDKVersion/2.0.0');
611651
});
612652

613653
// AppVerify test ------------------------

0 commit comments

Comments
 (0)