Skip to content

Commit 59118e3

Browse files
authored
Merge pull request #118 from swlodarski-sumoheavy/6.2.x
SP-1030 Add X-BitPay-Platform-Info header
2 parents 4b197fc + 61ed7a7 commit 59118e3

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

src/Client.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class Client {
5353
tokenContainer: TokenContainer | null,
5454
posToken: PosToken | null,
5555
environment?: Environment,
56+
platformInfo?: string,
5657
bitPayClient?: BitPayClient, // using for tests
5758
guidGenerator?: GuidGenerator // using for tests
5859
) {
@@ -82,13 +83,18 @@ export class Client {
8283
const ecKey = this.getEcKeyByPrivateKey(privateKey);
8384
this.guidGenerator = new GuidGenerator();
8485
this.tokenContainer = tokenContainer;
85-
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), ecKey, this.getIdentity(ecKey));
86+
this.bitPayClient = new BitPayClient(
87+
Client.getBaseUrl(environment),
88+
ecKey,
89+
this.getIdentity(ecKey),
90+
platformInfo
91+
);
8692
return;
8793
}
8894

8995
this.tokenContainer = tokenContainer;
9096
this.guidGenerator = new GuidGenerator();
91-
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), null, null);
97+
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), null, null, platformInfo);
9298

9399
if (posToken !== null) {
94100
this.tokenContainer.addPos(posToken.getValue());
@@ -101,17 +107,17 @@ export class Client {
101107
* @param posToken
102108
* @param environment
103109
*/
104-
public static createPosClient(posToken: string, environment?: Environment): Client {
105-
return new Client(null, null, null, new PosToken(posToken), environment);
110+
public static createPosClient(posToken: string, environment?: Environment, platformInfo?: string): Client {
111+
return new Client(null, null, null, new PosToken(posToken), environment, platformInfo);
106112
}
107113

108114
/**
109115
* Client factory based on config file
110116
*
111117
* @param configFilePath
112118
*/
113-
public static createClientByConfig(configFilePath: string): Client {
114-
return new Client(configFilePath, null, null, null);
119+
public static createClientByConfig(configFilePath: string, platformInfo?: string): Client {
120+
return new Client(configFilePath, null, null, null, undefined, platformInfo);
115121
}
116122

117123
/**
@@ -123,9 +129,10 @@ export class Client {
123129
public static createClientByPrivateKey(
124130
privateKey: string,
125131
tokenContainer: TokenContainer,
126-
environment?: Environment
132+
environment?: Environment,
133+
platformInfo?: string
127134
) {
128-
return new Client(null, new PrivateKey(privateKey), tokenContainer, null, environment);
135+
return new Client(null, new PrivateKey(privateKey), tokenContainer, null, environment, platformInfo);
129136
}
130137

131138
public getToken(facade: Facade) {
@@ -811,7 +818,7 @@ export class Client {
811818
return date.toISOString().split('T')[0];
812819
}
813820

814-
private initByConfigFilePath(configFilePath: string): void {
821+
private initByConfigFilePath(configFilePath: string, platformInfo?: string): void {
815822
if (!fs.existsSync(configFilePath)) {
816823
BitPayExceptionProvider.throwGenericExceptionWithMessage('Configuration file not found');
817824
}
@@ -823,7 +830,12 @@ export class Client {
823830
const tokens = envConfig['ApiTokens'];
824831
this.tokenContainer = new TokenContainer(tokens);
825832
const ecKey = this.getEcKeyByConfig(envConfig);
826-
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), ecKey, this.getIdentity(ecKey));
833+
this.bitPayClient = new BitPayClient(
834+
Client.getBaseUrl(environment),
835+
ecKey,
836+
this.getIdentity(ecKey),
837+
platformInfo
838+
);
827839
this.guidGenerator = new GuidGenerator();
828840
} catch (e: any) {
829841
BitPayExceptionProvider.throwGenericExceptionWithMessage('Error when reading configuration file');

src/Client/BitPayClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class BitPayClient {
1616
private readonly keyUtils: KeyUtils;
1717
private readonly responseParser: BitPayResponseParser;
1818

19-
public constructor(baseUrl: string, ecKey: KeyPair | null, identity: string | null) {
19+
public constructor(baseUrl: string, ecKey: KeyPair | null, identity: string | null, platformInfo?: string) {
2020
this.ecKey = ecKey;
2121
this.baseUrl = baseUrl;
2222
this.identity = identity;
@@ -27,6 +27,9 @@ export class BitPayClient {
2727
'x-bitpay-api-frame-version': Env.BitpayApiFrameVersion,
2828
'Content-Type': 'application/json'
2929
};
30+
if (platformInfo) {
31+
this.defaultHeaders['x-bitpay-platform-info'] = platformInfo;
32+
}
3033
this.keyUtils = new KeyUtils();
3134
this.responseParser = new BitPayResponseParser();
3235
}

test/clientUnit.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('BitPaySDK.Client', () => {
128128
return exampleUuid;
129129
});
130130

131-
client = new Client(null, null, tokenContainer, null, undefined, bpc, guidGenerator);
131+
client = new Client(null, null, tokenContainer, null, undefined, undefined, bpc, guidGenerator);
132132
oneMonthAgo = new Date();
133133
oneMonthAgo.setDate(oneMonthAgo.getDate() - 30);
134134

@@ -184,6 +184,13 @@ describe('BitPaySDK.Client', () => {
184184
expect(client.getToken(Facade.Pos)).toBe(posTokenValue);
185185
});
186186

187+
it('should create POS client with x-bitpay-platform-info header', async () => {
188+
const posTokenValue = 'posToken';
189+
const client = Client.createPosClient(posTokenValue, undefined, 'MyPlatform_v1.0.0');
190+
191+
expect(client.getToken(Facade.Pos)).toBe(posTokenValue);
192+
});
193+
187194
it('should create client by private key', async () => {
188195
const tokenContainer = new TokenContainer();
189196
const token = 'anotherMerchantToken';
@@ -197,10 +204,30 @@ describe('BitPaySDK.Client', () => {
197204
expect(client.getToken(Facade.Merchant)).toBe(token);
198205
});
199206

207+
it('should create client by private key with x-bitpay-platform-info header', async () => {
208+
const tokenContainer = new TokenContainer();
209+
const token = 'anotherMerchantToken';
210+
tokenContainer.addMerchant(token);
211+
212+
const client = Client.createClientByPrivateKey(
213+
'9ee267c293e74c12bf4035746834ad4f5690d546d7d10e15c92fc83043552186',
214+
tokenContainer,
215+
undefined,
216+
'MyPlatform_v1.0.0'
217+
);
218+
219+
expect(client.getToken(Facade.Merchant)).toBe(token);
220+
});
221+
200222
it('should create client by config file', async () => {
201223
const client = Client.createClientByConfig(__dirname + '/BitPayUnit.config.json');
202224
expect(client.getToken(Facade.Pos)).toBe('somePosToken');
203225
});
226+
227+
it('should create client by config file with x-bitpay-platform-info header', async () => {
228+
const client = Client.createClientByConfig(__dirname + '/BitPayUnit.config.json', 'MyPlatform_v1.0.0');
229+
expect(client.getToken(Facade.Pos)).toBe('somePosToken');
230+
});
204231
});
205232

206233
describe('Exceptions', () => {
@@ -216,6 +243,7 @@ describe('BitPaySDK.Client', () => {
216243
tokenContainer,
217244
null,
218245
undefined,
246+
undefined,
219247
new BitPayClient(host + '/', ecKey, 'someIdentity')
220248
);
221249

0 commit comments

Comments
 (0)