1616import java .net .URISyntaxException ;
1717import java .nio .charset .StandardCharsets ;
1818import java .util .List ;
19- import org .apache .http .HttpEntity ;
20- import org .apache .http .HttpResponse ;
2119import org .apache .http .client .HttpClient ;
2220import org .apache .http .client .methods .HttpDelete ;
2321import org .apache .http .client .methods .HttpGet ;
2422import org .apache .http .client .methods .HttpPost ;
2523import org .apache .http .client .methods .HttpPut ;
2624import org .apache .http .client .utils .URLEncodedUtils ;
2725import org .apache .http .entity .ByteArrayEntity ;
26+ import org .apache .http .message .AbstractHttpMessage ;
2827import org .apache .http .message .BasicNameValuePair ;
29- import org .apache .http .util .EntityUtils ;
3028import 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