Skip to content

Commit 381703f

Browse files
bobbrodiemwarzybok-sumoheavy
authored andcommitted
SP-737 Type Review: Java
2 parents 296d86c + eba15f1 commit 381703f

File tree

68 files changed

+2113
-696
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2113
-696
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@
239239
<artifactId>jackson-databind</artifactId>
240240
<version>2.14.2</version>
241241
</dependency>
242+
<dependency>
243+
<groupId>com.fasterxml.jackson.datatype</groupId>
244+
<artifactId>jackson-datatype-jsr310</artifactId>
245+
<version>2.16.0</version>
246+
</dependency>
242247
<dependency>
243248
<groupId>org.slf4j</groupId>
244249
<artifactId>slf4j-api</artifactId>

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public void authorizeClient(String pairingCode) throws BitPayApiException, BitPa
7373
"Failed to serialize Token object : " + e.getMessage());
7474
}
7575

76-
String jsonResponse = this.bitPayClient.post("tokens", json);
76+
HttpResponse response = this.bitPayClient.post("tokens", json);
77+
78+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
7779

7880
List<Token> tokens = null;
7981

@@ -117,12 +119,14 @@ public String authorizeClient(Facade facade) throws BitPayApiException, BitPayGe
117119
BitPayExceptionProvider.throwSerializeResourceException("Token", e.getMessage());
118120
}
119121

120-
String response = this.bitPayClient.post("tokens", json);
122+
HttpResponse response = this.bitPayClient.post("tokens", json);
123+
124+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
121125

122126
List<Token> tokens = null;
123127

124128
try {
125-
tokens = Arrays.asList(mapper.readValue(response, Token[].class));
129+
tokens = Arrays.asList(mapper.readValue(jsonResponse, Token[].class));
126130

127131
// Expecting a single token resource.
128132
if (tokens.size() != 1) {

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public Bill create(
9595
BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage());
9696
}
9797

98-
String jsonResponse = this.bitPayClient.post("bills", json, signRequest);
98+
HttpResponse response = this.bitPayClient.post("bills", json, signRequest);
99+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
99100

100101
try {
101102
bill = mapper.readerForUpdating(bill).readValue(jsonResponse);
@@ -130,7 +131,9 @@ public Bill get(
130131
ParameterAdder.execute(params, "token", token);
131132

132133
Bill bill = null;
133-
String jsonResponse = this.bitPayClient.get("bills/" + billId, params, signRequest);
134+
135+
HttpResponse response = this.bitPayClient.get("bills/" + billId, params, signRequest);
136+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
134137

135138
try {
136139
bill = JsonMapperFactory.create().readValue(jsonResponse, Bill.class);
@@ -157,7 +160,9 @@ public List<Bill> getBills(String status) throws BitPayGenericException, BitPayA
157160
List<Bill> bills = null;
158161

159162
try {
160-
String jsonResponse = this.bitPayClient.get("bills", params);
163+
HttpResponse response = this.bitPayClient.get("bills", params);
164+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
165+
161166
bills = Arrays.asList(
162167
JsonMapperFactory.create().readValue(jsonResponse, Bill[].class));
163168
} catch (JsonProcessingException e) {
@@ -179,7 +184,8 @@ public List<Bill> getBills() throws BitPayGenericException, BitPayApiException {
179184
ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT));
180185

181186
List<Bill> bills = null;
182-
String jsonResponse = this.bitPayClient.get("bills", params);
187+
HttpResponse response = this.bitPayClient.get("bills", params);
188+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
183189

184190
try {
185191
bills = Arrays.asList(
@@ -218,7 +224,8 @@ public Bill update(
218224
BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage());
219225
}
220226

221-
String jsonResponse = this.bitPayClient.update("bills/" + billId, json);
227+
HttpResponse response = this.bitPayClient.update("bills/" + billId, json);
228+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
222229

223230
try {
224231
bill = mapper.readerForUpdating(bill).readValue(jsonResponse);
@@ -260,7 +267,9 @@ public String deliver(
260267
BitPayExceptionProvider.throwEncodeException(e.getMessage());
261268
}
262269

263-
String response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest);
264-
return response.replace("\"", "");
270+
HttpResponse response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest);
271+
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());
272+
273+
return jsonResponse.replace("\"", "");
265274
}
266275
}

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

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@
1616
import java.net.URISyntaxException;
1717
import java.nio.charset.StandardCharsets;
1818
import java.util.List;
19-
import org.apache.http.HttpEntity;
20-
import org.apache.http.HttpResponse;
2119
import org.apache.http.client.HttpClient;
2220
import org.apache.http.client.methods.HttpDelete;
2321
import org.apache.http.client.methods.HttpGet;
2422
import org.apache.http.client.methods.HttpPost;
2523
import org.apache.http.client.methods.HttpPut;
2624
import org.apache.http.client.utils.URLEncodedUtils;
2725
import org.apache.http.entity.ByteArrayEntity;
26+
import org.apache.http.message.AbstractHttpMessage;
2827
import org.apache.http.message.BasicNameValuePair;
29-
import org.apache.http.util.EntityUtils;
3028
import org.bitcoinj.core.ECKey;
3129

3230
/**
@@ -68,7 +66,7 @@ public BitPayClient(
6866
* @throws BitPayGenericException BitPayGenericException class
6967
* @throws BitPayApiException BitPayApiException class
7068
*/
71-
public String get(
69+
public HttpResponse get(
7270
final String uri,
7371
final List<BasicNameValuePair> parameters
7472
) throws BitPayApiException, BitPayGenericException {
@@ -85,13 +83,11 @@ public String get(
8583
* @throws BitPayGenericException BitPayGenericException
8684
* @throws BitPayApiException BitPayApiException
8785
*/
88-
public String get(
86+
public HttpResponse get(
8987
final String uri,
9088
final List<BasicNameValuePair> parameters,
9189
final boolean signatureRequired
9290
) throws BitPayApiException, BitPayGenericException {
93-
String jsonResponse = null;
94-
9591
try {
9692
String fullUrl = this.baseUrl + uri;
9793
final HttpGet httpGet = this.httpRequestFactory.createHttpGet(fullUrl);
@@ -101,32 +97,22 @@ public String get(
10197
httpGet.setURI(new URI(fullUrl));
10298
}
10399

100+
this.addDefaultHeaders(httpGet);
104101
if (signatureRequired) {
105-
httpGet.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl));
106-
httpGet.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
102+
this.addSignatureRequiredHeaders(httpGet, fullUrl);
107103
}
108104

109-
httpGet.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
110-
httpGet.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
111-
httpGet.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
112-
httpGet.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
113-
114105
LoggerProvider.getLogger().logRequest(HttpGet.METHOD_NAME, fullUrl, null);
115106

116-
HttpResponse response = this.httpClient.execute(httpGet);
107+
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpGet));
117108

118-
final HttpEntity entity = response.getEntity();
119-
String jsonString = EntityUtils.toString(entity, "UTF-8");
120-
121-
LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, fullUrl, jsonString);
122-
123-
jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
109+
LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, fullUrl, response.getBody());
124110

111+
return response;
125112
} catch (IOException | URISyntaxException e) {
126113
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
114+
throw new BitPayApiException(e.getMessage(), null);
127115
}
128-
129-
return jsonResponse;
130116
}
131117

132118
/**
@@ -137,7 +123,7 @@ public String get(
137123
* @throws BitPayApiException BitPayApiException
138124
* @throws BitPayGenericException BitPayGenericException
139125
*/
140-
public String get(final String uri) throws BitPayApiException, BitPayGenericException {
126+
public HttpResponse get(final String uri) throws BitPayApiException, BitPayGenericException {
141127
return this.get(uri, null, false);
142128
}
143129

@@ -150,12 +136,10 @@ public String get(final String uri) throws BitPayApiException, BitPayGenericExce
150136
* @throws BitPayApiException BitPayApiException
151137
* @throws BitPayGenericException BitPayGenericException
152138
*/
153-
public String delete(
139+
public HttpResponse delete(
154140
final String uri,
155141
final List<BasicNameValuePair> parameters
156142
) throws BitPayApiException, BitPayGenericException {
157-
String jsonResponse = null;
158-
159143
try {
160144
String fullUrl = this.baseUrl + uri;
161145
final HttpDelete httpDelete = this.httpRequestFactory.createHttpDelete(fullUrl);
@@ -165,28 +149,20 @@ public String delete(
165149
httpDelete.setURI(new URI(fullUrl));
166150
}
167151

168-
httpDelete.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
169-
httpDelete.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
170-
httpDelete.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
171-
httpDelete.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
172-
httpDelete.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl));
173-
httpDelete.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
152+
this.addDefaultHeaders(httpDelete);
153+
this.addSignatureRequiredHeaders(httpDelete, fullUrl);
174154

175155
LoggerProvider.getLogger().logRequest(HttpDelete.METHOD_NAME, fullUrl, null);
176156

177-
HttpResponse response = this.httpClient.execute(httpDelete);
178-
179-
final HttpEntity entity = response.getEntity();
180-
String jsonString = EntityUtils.toString(entity, "UTF-8");
157+
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpDelete));
181158

182-
LoggerProvider.getLogger().logResponse(HttpDelete.METHOD_NAME, fullUrl, jsonString);
159+
LoggerProvider.getLogger().logResponse(HttpDelete.METHOD_NAME, fullUrl, response.getBody());
183160

184-
jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
161+
return response;
185162
} catch (IOException | URISyntaxException e) {
186163
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
164+
throw new BitPayApiException(e.getMessage(), null);
187165
}
188-
189-
return jsonResponse;
190166
}
191167

192168
/**
@@ -198,7 +174,7 @@ public String delete(
198174
* @throws BitPayApiException BitPayApiException
199175
* @throws BitPayGenericException BitPayGenericException
200176
*/
201-
public String post(
177+
public HttpResponse post(
202178
final String uri,
203179
final String json
204180
) throws BitPayApiException, BitPayGenericException {
@@ -215,45 +191,33 @@ public String post(
215191
* @throws BitPayApiException BitPayApiException
216192
* @throws BitPayGenericException BitPayGenericException
217193
*/
218-
public String post(
194+
public HttpResponse post(
219195
final String uri,
220196
final String json,
221197
final boolean signatureRequired
222198
) throws BitPayApiException, BitPayGenericException {
223-
String jsonResponse = null;
224-
225199
try {
226200
final String endpoint = this.baseUrl + uri;
227201
final HttpPost httpPost = this.httpRequestFactory.createHttpPost(endpoint);
228-
229202
httpPost.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8)));
230203

204+
this.addDefaultHeaders(httpPost);
205+
httpPost.addHeader("Content-Type", "application/json");
231206
if (signatureRequired) {
232-
httpPost.addHeader("x-signature", KeyUtils.sign(this.ecKey, this.baseUrl + uri + json));
233-
httpPost.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
207+
this.addSignatureRequiredHeaders(httpPost, endpoint + json);
234208
}
235209

236-
httpPost.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
237-
httpPost.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
238-
httpPost.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
239-
httpPost.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
240-
httpPost.addHeader("Content-Type", "application/json");
241-
242210
LoggerProvider.getLogger().logRequest(HttpPost.METHOD_NAME, endpoint, httpPost.toString());
243211

244-
HttpResponse response = this.httpClient.execute(httpPost);
245-
246-
final HttpEntity entity = response.getEntity();
247-
String jsonString = EntityUtils.toString(entity, "UTF-8");
212+
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpPost));
248213

249-
LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, endpoint, jsonString);
214+
LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, endpoint, response.getBody());
250215

251-
jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
216+
return response;
252217
} catch (IOException e) {
253218
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
219+
throw new BitPayApiException(e.getMessage(), null);
254220
}
255-
256-
return jsonResponse;
257221
}
258222

259223
/**
@@ -265,7 +229,7 @@ public String post(
265229
* @throws BitPayApiException BitPayApiException
266230
* @throws BitPayGenericException BitPayGenericException
267231
*/
268-
public String update(
232+
public HttpResponse update(
269233
final String uri,
270234
final String json
271235
) throws BitPayApiException, BitPayGenericException {
@@ -277,8 +241,7 @@ public String update(
277241

278242
httpPut.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8)));
279243

280-
httpPut.addHeader("x-signature", KeyUtils.sign(this.ecKey, this.baseUrl + uri + json));
281-
httpPut.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
244+
this.addSignatureRequiredHeaders(httpPut, endpoint + json);
282245
httpPut.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
283246
httpPut.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
284247
httpPut.addHeader("Content-Type", "application/json");
@@ -287,18 +250,27 @@ public String update(
287250

288251
LoggerProvider.getLogger().logRequest(HttpPut.METHOD_NAME, endpoint, httpPut.toString());
289252

290-
HttpResponse response = this.httpClient.execute(httpPut);
291-
292-
final HttpEntity entity = response.getEntity();
293-
String jsonString = EntityUtils.toString(entity, "UTF-8");
253+
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpPut));
294254

295-
LoggerProvider.getLogger().logResponse(HttpPut.METHOD_NAME, endpoint, jsonString);
255+
LoggerProvider.getLogger().logResponse(HttpPut.METHOD_NAME, endpoint, response.getBody());
296256

297-
jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
257+
return response;
298258
} catch (IOException e) {
299259
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
260+
throw new BitPayApiException(e.getMessage(), null);
300261
}
262+
}
263+
264+
private void addDefaultHeaders(AbstractHttpMessage httpMessage) {
265+
httpMessage.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
266+
httpMessage.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
267+
httpMessage.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
268+
httpMessage.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
269+
}
301270

302-
return jsonResponse;
271+
private void addSignatureRequiredHeaders(AbstractHttpMessage httpMessage, String uri)
272+
throws BitPayGenericException {
273+
httpMessage.addHeader("x-signature", KeyUtils.sign(this.ecKey, uri));
274+
httpMessage.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
303275
}
304276
}

0 commit comments

Comments
 (0)