Skip to content

Commit

Permalink
SP-737 Type Review: Java
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbrodie authored and mwarzybok-sumoheavy committed Dec 13, 2023
2 parents 296d86c + eba15f1 commit 381703f
Show file tree
Hide file tree
Showing 68 changed files with 2,113 additions and 696 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/bitpay/sdk/client/AuthorizationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public void authorizeClient(String pairingCode) throws BitPayApiException, BitPa
"Failed to serialize Token object : " + e.getMessage());
}

String jsonResponse = this.bitPayClient.post("tokens", json);
HttpResponse response = this.bitPayClient.post("tokens", json);

String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());

List<Token> tokens = null;

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

String response = this.bitPayClient.post("tokens", json);
HttpResponse response = this.bitPayClient.post("tokens", json);

String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());

List<Token> tokens = null;

try {
tokens = Arrays.asList(mapper.readValue(response, Token[].class));
tokens = Arrays.asList(mapper.readValue(jsonResponse, Token[].class));

// Expecting a single token resource.
if (tokens.size() != 1) {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/bitpay/sdk/client/BillClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public Bill create(
BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage());
}

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

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

Bill bill = null;
String jsonResponse = this.bitPayClient.get("bills/" + billId, params, signRequest);

HttpResponse response = this.bitPayClient.get("bills/" + billId, params, signRequest);
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());

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

try {
String jsonResponse = this.bitPayClient.get("bills", params);
HttpResponse response = this.bitPayClient.get("bills", params);
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());

bills = Arrays.asList(
JsonMapperFactory.create().readValue(jsonResponse, Bill[].class));
} catch (JsonProcessingException e) {
Expand All @@ -179,7 +184,8 @@ public List<Bill> getBills() throws BitPayGenericException, BitPayApiException {
ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT));

List<Bill> bills = null;
String jsonResponse = this.bitPayClient.get("bills", params);
HttpResponse response = this.bitPayClient.get("bills", params);
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());

try {
bills = Arrays.asList(
Expand Down Expand Up @@ -218,7 +224,8 @@ public Bill update(
BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage());
}

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

try {
bill = mapper.readerForUpdating(bill).readValue(jsonResponse);
Expand Down Expand Up @@ -260,7 +267,9 @@ public String deliver(
BitPayExceptionProvider.throwEncodeException(e.getMessage());
}

String response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest);
return response.replace("\"", "");
HttpResponse response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest);
String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody());

return jsonResponse.replace("\"", "");
}
}
116 changes: 44 additions & 72 deletions src/main/java/com/bitpay/sdk/client/BitPayClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.bitcoinj.core.ECKey;

/**
Expand Down Expand Up @@ -68,7 +66,7 @@ public BitPayClient(
* @throws BitPayGenericException BitPayGenericException class
* @throws BitPayApiException BitPayApiException class
*/
public String get(
public HttpResponse get(
final String uri,
final List<BasicNameValuePair> parameters
) throws BitPayApiException, BitPayGenericException {
Expand All @@ -85,13 +83,11 @@ public String get(
* @throws BitPayGenericException BitPayGenericException
* @throws BitPayApiException BitPayApiException
*/
public String get(
public HttpResponse get(
final String uri,
final List<BasicNameValuePair> parameters,
final boolean signatureRequired
) throws BitPayApiException, BitPayGenericException {
String jsonResponse = null;

try {
String fullUrl = this.baseUrl + uri;
final HttpGet httpGet = this.httpRequestFactory.createHttpGet(fullUrl);
Expand All @@ -101,32 +97,22 @@ public String get(
httpGet.setURI(new URI(fullUrl));
}

this.addDefaultHeaders(httpGet);
if (signatureRequired) {
httpGet.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl));
httpGet.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
this.addSignatureRequiredHeaders(httpGet, fullUrl);
}

httpGet.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
httpGet.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
httpGet.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
httpGet.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);

LoggerProvider.getLogger().logRequest(HttpGet.METHOD_NAME, fullUrl, null);

HttpResponse response = this.httpClient.execute(httpGet);
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpGet));

final HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity, "UTF-8");

LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, fullUrl, jsonString);

jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, fullUrl, response.getBody());

return response;
} catch (IOException | URISyntaxException e) {
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
throw new BitPayApiException(e.getMessage(), null);
}

return jsonResponse;
}

/**
Expand All @@ -137,7 +123,7 @@ public String get(
* @throws BitPayApiException BitPayApiException
* @throws BitPayGenericException BitPayGenericException
*/
public String get(final String uri) throws BitPayApiException, BitPayGenericException {
public HttpResponse get(final String uri) throws BitPayApiException, BitPayGenericException {
return this.get(uri, null, false);
}

Expand All @@ -150,12 +136,10 @@ public String get(final String uri) throws BitPayApiException, BitPayGenericExce
* @throws BitPayApiException BitPayApiException
* @throws BitPayGenericException BitPayGenericException
*/
public String delete(
public HttpResponse delete(
final String uri,
final List<BasicNameValuePair> parameters
) throws BitPayApiException, BitPayGenericException {
String jsonResponse = null;

try {
String fullUrl = this.baseUrl + uri;
final HttpDelete httpDelete = this.httpRequestFactory.createHttpDelete(fullUrl);
Expand All @@ -165,28 +149,20 @@ public String delete(
httpDelete.setURI(new URI(fullUrl));
}

httpDelete.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
httpDelete.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
httpDelete.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
httpDelete.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
httpDelete.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl));
httpDelete.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
this.addDefaultHeaders(httpDelete);
this.addSignatureRequiredHeaders(httpDelete, fullUrl);

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

HttpResponse response = this.httpClient.execute(httpDelete);

final HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity, "UTF-8");
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpDelete));

LoggerProvider.getLogger().logResponse(HttpDelete.METHOD_NAME, fullUrl, jsonString);
LoggerProvider.getLogger().logResponse(HttpDelete.METHOD_NAME, fullUrl, response.getBody());

jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
return response;
} catch (IOException | URISyntaxException e) {
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
throw new BitPayApiException(e.getMessage(), null);
}

return jsonResponse;
}

/**
Expand All @@ -198,7 +174,7 @@ public String delete(
* @throws BitPayApiException BitPayApiException
* @throws BitPayGenericException BitPayGenericException
*/
public String post(
public HttpResponse post(
final String uri,
final String json
) throws BitPayApiException, BitPayGenericException {
Expand All @@ -215,45 +191,33 @@ public String post(
* @throws BitPayApiException BitPayApiException
* @throws BitPayGenericException BitPayGenericException
*/
public String post(
public HttpResponse post(
final String uri,
final String json,
final boolean signatureRequired
) throws BitPayApiException, BitPayGenericException {
String jsonResponse = null;

try {
final String endpoint = this.baseUrl + uri;
final HttpPost httpPost = this.httpRequestFactory.createHttpPost(endpoint);

httpPost.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8)));

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

httpPost.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
httpPost.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
httpPost.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
httpPost.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
httpPost.addHeader("Content-Type", "application/json");

LoggerProvider.getLogger().logRequest(HttpPost.METHOD_NAME, endpoint, httpPost.toString());

HttpResponse response = this.httpClient.execute(httpPost);

final HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity, "UTF-8");
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpPost));

LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, endpoint, jsonString);
LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, endpoint, response.getBody());

jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
return response;
} catch (IOException e) {
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
throw new BitPayApiException(e.getMessage(), null);
}

return jsonResponse;
}

/**
Expand All @@ -265,7 +229,7 @@ public String post(
* @throws BitPayApiException BitPayApiException
* @throws BitPayGenericException BitPayGenericException
*/
public String update(
public HttpResponse update(
final String uri,
final String json
) throws BitPayApiException, BitPayGenericException {
Expand All @@ -277,8 +241,7 @@ public String update(

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

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

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

HttpResponse response = this.httpClient.execute(httpPut);

final HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity, "UTF-8");
HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpPut));

LoggerProvider.getLogger().logResponse(HttpPut.METHOD_NAME, endpoint, jsonString);
LoggerProvider.getLogger().logResponse(HttpPut.METHOD_NAME, endpoint, response.getBody());

jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString);
return response;
} catch (IOException e) {
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
throw new BitPayApiException(e.getMessage(), null);
}
}

private void addDefaultHeaders(AbstractHttpMessage httpMessage) {
httpMessage.addHeader("x-accept-version", Config.BITPAY_API_VERSION);
httpMessage.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME);
httpMessage.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION);
httpMessage.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO);
}

return jsonResponse;
private void addSignatureRequiredHeaders(AbstractHttpMessage httpMessage, String uri)
throws BitPayGenericException {
httpMessage.addHeader("x-signature", KeyUtils.sign(this.ecKey, uri));
httpMessage.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey()));
}
}
Loading

0 comments on commit 381703f

Please sign in to comment.