Skip to content

Commit

Permalink
Merge pull request #118 from swlodarski-sumoheavy/6.2.x
Browse files Browse the repository at this point in the history
SP-1030 Add X-BitPay-Platform-Info header
  • Loading branch information
bobbrodie authored Sep 3, 2024
2 parents 4b197fc + 61ed7a7 commit 59118e3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
32 changes: 22 additions & 10 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class Client {
tokenContainer: TokenContainer | null,
posToken: PosToken | null,
environment?: Environment,
platformInfo?: string,
bitPayClient?: BitPayClient, // using for tests
guidGenerator?: GuidGenerator // using for tests
) {
Expand Down Expand Up @@ -82,13 +83,18 @@ export class Client {
const ecKey = this.getEcKeyByPrivateKey(privateKey);
this.guidGenerator = new GuidGenerator();
this.tokenContainer = tokenContainer;
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), ecKey, this.getIdentity(ecKey));
this.bitPayClient = new BitPayClient(
Client.getBaseUrl(environment),
ecKey,
this.getIdentity(ecKey),
platformInfo
);
return;
}

this.tokenContainer = tokenContainer;
this.guidGenerator = new GuidGenerator();
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), null, null);
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), null, null, platformInfo);

if (posToken !== null) {
this.tokenContainer.addPos(posToken.getValue());
Expand All @@ -101,17 +107,17 @@ export class Client {
* @param posToken
* @param environment
*/
public static createPosClient(posToken: string, environment?: Environment): Client {
return new Client(null, null, null, new PosToken(posToken), environment);
public static createPosClient(posToken: string, environment?: Environment, platformInfo?: string): Client {
return new Client(null, null, null, new PosToken(posToken), environment, platformInfo);
}

/**
* Client factory based on config file
*
* @param configFilePath
*/
public static createClientByConfig(configFilePath: string): Client {
return new Client(configFilePath, null, null, null);
public static createClientByConfig(configFilePath: string, platformInfo?: string): Client {
return new Client(configFilePath, null, null, null, undefined, platformInfo);
}

/**
Expand All @@ -123,9 +129,10 @@ export class Client {
public static createClientByPrivateKey(
privateKey: string,
tokenContainer: TokenContainer,
environment?: Environment
environment?: Environment,
platformInfo?: string
) {
return new Client(null, new PrivateKey(privateKey), tokenContainer, null, environment);
return new Client(null, new PrivateKey(privateKey), tokenContainer, null, environment, platformInfo);
}

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

private initByConfigFilePath(configFilePath: string): void {
private initByConfigFilePath(configFilePath: string, platformInfo?: string): void {
if (!fs.existsSync(configFilePath)) {
BitPayExceptionProvider.throwGenericExceptionWithMessage('Configuration file not found');
}
Expand All @@ -823,7 +830,12 @@ export class Client {
const tokens = envConfig['ApiTokens'];
this.tokenContainer = new TokenContainer(tokens);
const ecKey = this.getEcKeyByConfig(envConfig);
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), ecKey, this.getIdentity(ecKey));
this.bitPayClient = new BitPayClient(
Client.getBaseUrl(environment),
ecKey,
this.getIdentity(ecKey),
platformInfo
);
this.guidGenerator = new GuidGenerator();
} catch (e: any) {
BitPayExceptionProvider.throwGenericExceptionWithMessage('Error when reading configuration file');
Expand Down
5 changes: 4 additions & 1 deletion src/Client/BitPayClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class BitPayClient {
private readonly keyUtils: KeyUtils;
private readonly responseParser: BitPayResponseParser;

public constructor(baseUrl: string, ecKey: KeyPair | null, identity: string | null) {
public constructor(baseUrl: string, ecKey: KeyPair | null, identity: string | null, platformInfo?: string) {
this.ecKey = ecKey;
this.baseUrl = baseUrl;
this.identity = identity;
Expand All @@ -27,6 +27,9 @@ export class BitPayClient {
'x-bitpay-api-frame-version': Env.BitpayApiFrameVersion,
'Content-Type': 'application/json'
};
if (platformInfo) {
this.defaultHeaders['x-bitpay-platform-info'] = platformInfo;
}
this.keyUtils = new KeyUtils();
this.responseParser = new BitPayResponseParser();
}
Expand Down
30 changes: 29 additions & 1 deletion test/clientUnit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('BitPaySDK.Client', () => {
return exampleUuid;
});

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

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

it('should create POS client with x-bitpay-platform-info header', async () => {
const posTokenValue = 'posToken';
const client = Client.createPosClient(posTokenValue, undefined, 'MyPlatform_v1.0.0');

expect(client.getToken(Facade.Pos)).toBe(posTokenValue);
});

it('should create client by private key', async () => {
const tokenContainer = new TokenContainer();
const token = 'anotherMerchantToken';
Expand All @@ -197,10 +204,30 @@ describe('BitPaySDK.Client', () => {
expect(client.getToken(Facade.Merchant)).toBe(token);
});

it('should create client by private key with x-bitpay-platform-info header', async () => {
const tokenContainer = new TokenContainer();
const token = 'anotherMerchantToken';
tokenContainer.addMerchant(token);

const client = Client.createClientByPrivateKey(
'9ee267c293e74c12bf4035746834ad4f5690d546d7d10e15c92fc83043552186',
tokenContainer,
undefined,
'MyPlatform_v1.0.0'
);

expect(client.getToken(Facade.Merchant)).toBe(token);
});

it('should create client by config file', async () => {
const client = Client.createClientByConfig(__dirname + '/BitPayUnit.config.json');
expect(client.getToken(Facade.Pos)).toBe('somePosToken');
});

it('should create client by config file with x-bitpay-platform-info header', async () => {
const client = Client.createClientByConfig(__dirname + '/BitPayUnit.config.json', 'MyPlatform_v1.0.0');
expect(client.getToken(Facade.Pos)).toBe('somePosToken');
});
});

describe('Exceptions', () => {
Expand All @@ -216,6 +243,7 @@ describe('BitPaySDK.Client', () => {
tokenContainer,
null,
undefined,
undefined,
new BitPayClient(host + '/', ecKey, 'someIdentity')
);

Expand Down

0 comments on commit 59118e3

Please sign in to comment.