Skip to content

Commit befd951

Browse files
committed
SP-1027 Add X-BitPay-Platform-Info header
1 parent 5997f2c commit befd951

File tree

4 files changed

+388
-5
lines changed

4 files changed

+388
-5
lines changed

src/main/java/com/bitpay/sdk/Client.java

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public Client(PosToken token) throws BitPayGenericException {
8383
this(token, Environment.PROD);
8484
}
8585

86+
/**
87+
* Constructor for POS facade.
88+
*
89+
* @param token POS token
90+
* @param platformInfo Platform info
91+
* @throws BitPayGenericException BitPayGenericException class
92+
*/
93+
public Client(PosToken token, String platformInfo) throws BitPayGenericException {
94+
this(token, Environment.PROD, platformInfo);
95+
}
96+
8697
/**
8798
* Constructor for POS facade.
8899
*
@@ -109,6 +120,35 @@ public Client(
109120
this.guidGenerator = new GuidGenerator();
110121
}
111122

123+
/**
124+
* Constructor for POS facade.
125+
*
126+
* @param token POS token
127+
* @param environment Environment
128+
* @param platformInfo Platform info
129+
* @throws BitPayGenericException BitPayGenericException class
130+
*/
131+
public Client(
132+
PosToken token,
133+
Environment environment,
134+
String platformInfo
135+
) throws BitPayGenericException {
136+
if (Objects.isNull(token) || Objects.isNull(environment)) {
137+
BitPayExceptionProvider.throwMissingParameterException();
138+
}
139+
140+
this.tokenContainer = new TokenContainer();
141+
this.tokenContainer.addPos(token.value());
142+
this.bitPayClient = new BitPayClient(
143+
getHttpClient(null, null),
144+
new HttpRequestFactory(),
145+
getBaseUrl(environment),
146+
null,
147+
platformInfo
148+
);
149+
this.guidGenerator = new GuidGenerator();
150+
}
151+
112152
/**
113153
* Constructor for use if the keys and SIN are managed by this library.
114154
*
@@ -138,6 +178,38 @@ public Client(
138178
this.guidGenerator = new GuidGenerator();
139179
}
140180

181+
/**
182+
* Constructor for use if the keys and SIN are managed by this library.
183+
*
184+
* @param environment Target environment. Options: Env.Test / Env.Prod
185+
* @param privateKey The full path to the securely located private key or the HEX key value.
186+
* @param tokenContainer Object containing the available tokens.
187+
* @param proxyDetails HttpHost Optional Proxy setting (set to NULL to ignore)
188+
* @param proxyCredentials CredentialsProvider Optional Proxy Basic Auth Credentials (set to NULL to ignore)
189+
* @param platformInfo Platform Info
190+
* @throws BitPayGenericException BitPayGenericException class
191+
*/
192+
public Client(
193+
Environment environment,
194+
PrivateKey privateKey,
195+
TokenContainer tokenContainer,
196+
HttpHost proxyDetails,
197+
CredentialsProvider proxyCredentials,
198+
String platformInfo
199+
) throws BitPayGenericException {
200+
ECKey ecKey = getEcKey(privateKey);
201+
this.tokenContainer = tokenContainer;
202+
this.deriveIdentity(ecKey);
203+
this.bitPayClient = new BitPayClient(
204+
getHttpClient(proxyDetails, proxyCredentials),
205+
new HttpRequestFactory(),
206+
getBaseUrl(environment),
207+
ecKey,
208+
platformInfo
209+
);
210+
this.guidGenerator = new GuidGenerator();
211+
}
212+
141213
/**
142214
* Constructor for use if the keys and SIN are managed by this library.
143215
*
@@ -168,6 +240,40 @@ public Client(
168240
this.guidGenerator = new GuidGenerator();
169241
}
170242

243+
244+
/**
245+
* Constructor for use if the keys and SIN are managed by this library.
246+
*
247+
* @param configFilePath The path to the configuration file.
248+
* @param proxy HttpHost Optional Proxy setting (set to NULL to ignore)
249+
* @param proxyCredentials CredentialsProvider Optional Proxy Basic Auth Credentials (set to NULL to ignore)
250+
* @param platformInfo Platform Info
251+
* @throws BitPayGenericException BitPayGenericException class
252+
*/
253+
public Client(
254+
ConfigFilePath configFilePath,
255+
HttpHost proxy,
256+
CredentialsProvider proxyCredentials,
257+
String platformInfo
258+
) throws BitPayGenericException {
259+
Config config = this.buildConfigFromFile(configFilePath);
260+
this.tokenContainer = new TokenContainer(config);
261+
ECKey ecKey = this.getEcKey(config);
262+
if (Objects.isNull(ecKey)) {
263+
BitPayExceptionProvider.throwValidationException("Missing ECKey");
264+
}
265+
266+
this.deriveIdentity(ecKey);
267+
this.bitPayClient = new BitPayClient(
268+
getHttpClient(proxy, proxyCredentials),
269+
new HttpRequestFactory(),
270+
getBaseUrl(config.getEnvironment()),
271+
ecKey,
272+
platformInfo
273+
);
274+
this.guidGenerator = new GuidGenerator();
275+
}
276+
171277
/**
172278
* Constructor for all injected classes.
173279
*
@@ -199,6 +305,18 @@ public static Client createPosClient(PosToken token) throws BitPayGenericExcepti
199305
return new Client(token);
200306
}
201307

308+
/**
309+
* Create pos (light) client.
310+
*
311+
* @param token the token
312+
* @param platformInfo the platform info
313+
* @return the client
314+
* @throws BitPayGenericException BitPayGenericException class
315+
*/
316+
public static Client createPosClient(PosToken token, String platformInfo) throws BitPayGenericException {
317+
return new Client(token, platformInfo);
318+
}
319+
202320
/**
203321
* Create pos (light) client.
204322
*
@@ -214,6 +332,23 @@ public static Client createPosClient(
214332
return new Client(token, environment);
215333
}
216334

335+
/**
336+
* Create pos (light) client.
337+
*
338+
* @param token the token
339+
* @param environment environment
340+
* @param platformInfo the platform info
341+
* @return the client
342+
* @throws BitPayGenericException BitPayGenericException class
343+
*/
344+
public static Client createPosClient(
345+
PosToken token,
346+
Environment environment,
347+
String platformInfo
348+
) throws BitPayGenericException {
349+
return new Client(token, environment, platformInfo);
350+
}
351+
217352
/**
218353
* Create standard client.
219354
*
@@ -233,6 +368,27 @@ public static Client createClientByPrivateKey(
233368
return new Client(env, privateKey, tokenContainer, null, null);
234369
}
235370

371+
/**
372+
* Create standard client.
373+
*
374+
* @param privateKey the private key
375+
* @param tokenContainer the token container
376+
* @param environment environment
377+
* @param platformInfo the platform info
378+
* @return Client Client
379+
* @throws BitPayGenericException BitPayGenericException class
380+
*/
381+
public static Client createClientByPrivateKey(
382+
PrivateKey privateKey,
383+
TokenContainer tokenContainer,
384+
Environment environment,
385+
String platformInfo
386+
) throws BitPayGenericException {
387+
Environment env = Objects.isNull(environment) ? Environment.PROD : environment;
388+
389+
return new Client(env, privateKey, tokenContainer, null, null, platformInfo);
390+
}
391+
236392
/**
237393
* Create standard client.
238394
*
@@ -244,6 +400,21 @@ public static Client createClientByConfigFilePath(ConfigFilePath configFilePath)
244400
return new Client(configFilePath, null, null);
245401
}
246402

403+
/**
404+
* Create standard client.
405+
*
406+
* @param configFilePath the config file path
407+
* @param platformInfo the platform info
408+
* @return the client
409+
* @throws BitPayGenericException BitPayGenericException class
410+
*/
411+
public static Client createClientByConfigFilePath(
412+
ConfigFilePath configFilePath,
413+
String platformInfo
414+
) throws BitPayGenericException {
415+
return new Client(configFilePath, null, null, platformInfo);
416+
}
417+
247418

248419
/**
249420
* Authorize this client with the server using the specified pairing code (Server Initiated Pairing).

src/main/java/com/bitpay/sdk/client/BitPayClient.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class BitPayClient {
3636
private final HttpRequestFactory httpRequestFactory;
3737
private final String baseUrl;
3838
private final ECKey ecKey;
39+
private String platformInfo = null;
3940

4041
/**
4142
* Instantiates a new Bit pay client.
@@ -57,6 +58,29 @@ public BitPayClient(
5758
this.ecKey = ecKey;
5859
}
5960

61+
/**
62+
* Instantiates a new Bit pay client.
63+
*
64+
* @param httpClient the http client
65+
* @param httpRequestFactory the http request factory
66+
* @param baseUrl the base url
67+
* @param ecKey the ECKey
68+
* @param platformInfo the Platform Info
69+
*/
70+
public BitPayClient(
71+
final HttpClient httpClient,
72+
final HttpRequestFactory httpRequestFactory,
73+
final String baseUrl,
74+
final ECKey ecKey,
75+
final String platformInfo
76+
) {
77+
this.httpClient = httpClient;
78+
this.baseUrl = baseUrl;
79+
this.httpRequestFactory = httpRequestFactory;
80+
this.ecKey = ecKey;
81+
this.platformInfo = platformInfo;
82+
}
83+
6084
/**
6185
* Send GET request.
6286
*
@@ -241,12 +265,9 @@ public HttpResponse update(
241265

242266
httpPut.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8)));
243267

244-
this.addSignatureRequiredHeaders(httpPut, endpoint + json);
245-
httpPut.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
246-
httpPut.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
268+
this.addDefaultHeaders(httpPut);
247269
httpPut.addHeader("Content-Type", "application/json");
248-
httpPut.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
249-
httpPut.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
270+
this.addSignatureRequiredHeaders(httpPut, endpoint + json);
250271

251272
LoggerProvider.getLogger().logRequest(HttpPut.METHOD_NAME, endpoint, httpPut.toString());
252273

@@ -266,6 +287,9 @@ private void addDefaultHeaders(AbstractHttpMessage httpMessage) {
266287
httpMessage.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
267288
httpMessage.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
268289
httpMessage.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
290+
if (this.platformInfo != null && !this.platformInfo.isEmpty()) {
291+
httpMessage.addHeader("x-bitPay-platform-info", this.platformInfo);
292+
}
269293
}
270294

271295
private void addSignatureRequiredHeaders(AbstractHttpMessage httpMessage, String uri)

src/test/java/com/bitpay/sdk/ClientTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ public void it_should_provide_pos_client() throws BitPayGenericException {
9393
Assertions.assertEquals(posToken, bitpay.getAccessToken(Facade.POS));
9494
}
9595

96+
@Test
97+
public void it_should_provide_pos_client_with_platform_info_header() throws BitPayGenericException {
98+
// given
99+
String posToken = "posToken";
100+
101+
// when
102+
Client bitpay = Client.createPosClient(new PosToken(posToken), "MyPlatform_v1.0.0");
103+
104+
// then
105+
Assertions.assertEquals(posToken, bitpay.getAccessToken(Facade.POS));
106+
}
107+
96108
@Test
97109
public void it_should_provide_client_by_key() throws BitPayGenericException {
98110
// given
@@ -109,6 +121,22 @@ public void it_should_provide_client_by_key() throws BitPayGenericException {
109121
Assertions.assertEquals(merchantToken, bitpay.getAccessToken(Facade.MERCHANT));
110122
}
111123

124+
@Test
125+
public void it_should_provide_client_by_key_with_platform_info_header() throws BitPayGenericException {
126+
// given
127+
String privateKey =
128+
"3082013102010104208ae30afbc7e93cb10cb983f70863e546b53f0b2c6158b1a71b576fd09790cff3a081e33081e0020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f3044042000000000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000000000000000000000000000000704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a124032200035d6a7e38d7c08b8a626e2390d0360a72a58bd1c5e1348e0eb810d4bbab3d3adf";
129+
String merchantToken = "merchantToken";
130+
TokenContainer tokens = new TokenContainer();
131+
tokens.addMerchant(merchantToken);
132+
133+
// when
134+
Client bitpay = Client.createClientByPrivateKey(new PrivateKey(privateKey), tokens, Environment.TEST, "MyPlatform_v1.0.0");
135+
136+
// then
137+
Assertions.assertEquals(merchantToken, bitpay.getAccessToken(Facade.MERCHANT));
138+
}
139+
112140
@Test
113141
public void it_should_provide_client_by_config() throws BitPayGenericException {
114142
// given
@@ -122,6 +150,19 @@ public void it_should_provide_client_by_config() throws BitPayGenericException {
122150
Assertions.assertEquals("payoutToken", bitpay.getAccessToken(Facade.PAYOUT));
123151
}
124152

153+
@Test
154+
public void it_should_provide_client_by_config_with_platform_info_header() throws BitPayGenericException {
155+
// given
156+
String path = System.getProperty("user.dir") + "/src/test/java/com/bitpay/sdk/BitPay.config.json";
157+
158+
// when
159+
Client bitpay = Client.createClientByConfigFilePath(new ConfigFilePath(path), "MyPlatform_v1.0.0");
160+
161+
// then
162+
Assertions.assertEquals("merchantToken", bitpay.getAccessToken(Facade.MERCHANT));
163+
Assertions.assertEquals("payoutToken", bitpay.getAccessToken(Facade.PAYOUT));
164+
}
165+
125166
@Test
126167
public void it_should_throws_BitPayApiException_for_invalid_privateKey() {
127168
BitPayGenericException exception = Assertions.assertThrows(BitPayGenericException.class, () -> {

0 commit comments

Comments
 (0)