diff --git a/pom.xml b/pom.xml
index 2496dc9b..0e84e8c2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -239,6 +239,11 @@
jackson-databind
2.14.2
+
+ com.fasterxml.jackson.datatype
+ jackson-datatype-jsr310
+ 2.16.0
+
org.slf4j
slf4j-api
diff --git a/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java b/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java
index 542576ff..3c11fe5b 100644
--- a/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java
+++ b/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java
@@ -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 tokens = null;
@@ -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 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) {
diff --git a/src/main/java/com/bitpay/sdk/client/BillClient.java b/src/main/java/com/bitpay/sdk/client/BillClient.java
index a0775559..0612aca1 100644
--- a/src/main/java/com/bitpay/sdk/client/BillClient.java
+++ b/src/main/java/com/bitpay/sdk/client/BillClient.java
@@ -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);
@@ -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);
@@ -157,7 +160,9 @@ public List getBills(String status) throws BitPayGenericException, BitPayA
List 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) {
@@ -179,7 +184,8 @@ public List getBills() throws BitPayGenericException, BitPayApiException {
ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT));
List 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(
@@ -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);
@@ -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("\"", "");
}
}
diff --git a/src/main/java/com/bitpay/sdk/client/BitPayClient.java b/src/main/java/com/bitpay/sdk/client/BitPayClient.java
index ec906245..2b076db3 100644
--- a/src/main/java/com/bitpay/sdk/client/BitPayClient.java
+++ b/src/main/java/com/bitpay/sdk/client/BitPayClient.java
@@ -16,8 +16,6 @@
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;
@@ -25,8 +23,8 @@
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;
/**
@@ -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 parameters
) throws BitPayApiException, BitPayGenericException {
@@ -85,13 +83,11 @@ public String get(
* @throws BitPayGenericException BitPayGenericException
* @throws BitPayApiException BitPayApiException
*/
- public String get(
+ public HttpResponse get(
final String uri,
final List parameters,
final boolean signatureRequired
) throws BitPayApiException, BitPayGenericException {
- String jsonResponse = null;
-
try {
String fullUrl = this.baseUrl + uri;
final HttpGet httpGet = this.httpRequestFactory.createHttpGet(fullUrl);
@@ -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;
}
/**
@@ -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);
}
@@ -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 parameters
) throws BitPayApiException, BitPayGenericException {
- String jsonResponse = null;
-
try {
String fullUrl = this.baseUrl + uri;
final HttpDelete httpDelete = this.httpRequestFactory.createHttpDelete(fullUrl);
@@ -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;
}
/**
@@ -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 {
@@ -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;
}
/**
@@ -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 {
@@ -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");
@@ -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()));
}
}
diff --git a/src/main/java/com/bitpay/sdk/client/CurrencyClient.java b/src/main/java/com/bitpay/sdk/client/CurrencyClient.java
index e48c765e..046661e4 100644
--- a/src/main/java/com/bitpay/sdk/client/CurrencyClient.java
+++ b/src/main/java/com/bitpay/sdk/client/CurrencyClient.java
@@ -22,20 +22,20 @@
public class CurrencyClient implements ResourceClient {
private static CurrencyClient instance;
- private final BitPayClient client;
+ private final BitPayClient bitPayClient;
private List