16
16
import java .net .URISyntaxException ;
17
17
import java .nio .charset .StandardCharsets ;
18
18
import java .util .List ;
19
- import org .apache .http .HttpEntity ;
20
- import org .apache .http .HttpResponse ;
21
19
import org .apache .http .client .HttpClient ;
22
20
import org .apache .http .client .methods .HttpDelete ;
23
21
import org .apache .http .client .methods .HttpGet ;
24
22
import org .apache .http .client .methods .HttpPost ;
25
23
import org .apache .http .client .methods .HttpPut ;
26
24
import org .apache .http .client .utils .URLEncodedUtils ;
27
25
import org .apache .http .entity .ByteArrayEntity ;
26
+ import org .apache .http .message .AbstractHttpMessage ;
28
27
import org .apache .http .message .BasicNameValuePair ;
29
- import org .apache .http .util .EntityUtils ;
30
28
import org .bitcoinj .core .ECKey ;
31
29
32
30
/**
@@ -68,7 +66,7 @@ public BitPayClient(
68
66
* @throws BitPayGenericException BitPayGenericException class
69
67
* @throws BitPayApiException BitPayApiException class
70
68
*/
71
- public String get (
69
+ public HttpResponse get (
72
70
final String uri ,
73
71
final List <BasicNameValuePair > parameters
74
72
) throws BitPayApiException , BitPayGenericException {
@@ -85,13 +83,11 @@ public String get(
85
83
* @throws BitPayGenericException BitPayGenericException
86
84
* @throws BitPayApiException BitPayApiException
87
85
*/
88
- public String get (
86
+ public HttpResponse get (
89
87
final String uri ,
90
88
final List <BasicNameValuePair > parameters ,
91
89
final boolean signatureRequired
92
90
) throws BitPayApiException , BitPayGenericException {
93
- String jsonResponse = null ;
94
-
95
91
try {
96
92
String fullUrl = this .baseUrl + uri ;
97
93
final HttpGet httpGet = this .httpRequestFactory .createHttpGet (fullUrl );
@@ -101,32 +97,22 @@ public String get(
101
97
httpGet .setURI (new URI (fullUrl ));
102
98
}
103
99
100
+ this .addDefaultHeaders (httpGet );
104
101
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 );
107
103
}
108
104
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
-
114
105
LoggerProvider .getLogger ().logRequest (HttpGet .METHOD_NAME , fullUrl , null );
115
106
116
- HttpResponse response = this .httpClient .execute (httpGet );
107
+ HttpResponse response = HttpResponseProvider . fromApacheHttpResponse ( this .httpClient .execute (httpGet ) );
117
108
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 ());
124
110
111
+ return response ;
125
112
} catch (IOException | URISyntaxException e ) {
126
113
BitPayExceptionProvider .throwApiExceptionWithMessage (e .getMessage ());
114
+ throw new BitPayApiException (e .getMessage (), null );
127
115
}
128
-
129
- return jsonResponse ;
130
116
}
131
117
132
118
/**
@@ -137,7 +123,7 @@ public String get(
137
123
* @throws BitPayApiException BitPayApiException
138
124
* @throws BitPayGenericException BitPayGenericException
139
125
*/
140
- public String get (final String uri ) throws BitPayApiException , BitPayGenericException {
126
+ public HttpResponse get (final String uri ) throws BitPayApiException , BitPayGenericException {
141
127
return this .get (uri , null , false );
142
128
}
143
129
@@ -150,12 +136,10 @@ public String get(final String uri) throws BitPayApiException, BitPayGenericExce
150
136
* @throws BitPayApiException BitPayApiException
151
137
* @throws BitPayGenericException BitPayGenericException
152
138
*/
153
- public String delete (
139
+ public HttpResponse delete (
154
140
final String uri ,
155
141
final List <BasicNameValuePair > parameters
156
142
) throws BitPayApiException , BitPayGenericException {
157
- String jsonResponse = null ;
158
-
159
143
try {
160
144
String fullUrl = this .baseUrl + uri ;
161
145
final HttpDelete httpDelete = this .httpRequestFactory .createHttpDelete (fullUrl );
@@ -165,28 +149,20 @@ public String delete(
165
149
httpDelete .setURI (new URI (fullUrl ));
166
150
}
167
151
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 );
174
154
175
155
LoggerProvider .getLogger ().logRequest (HttpDelete .METHOD_NAME , fullUrl , null );
176
156
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 ));
181
158
182
- LoggerProvider .getLogger ().logResponse (HttpDelete .METHOD_NAME , fullUrl , jsonString );
159
+ LoggerProvider .getLogger ().logResponse (HttpDelete .METHOD_NAME , fullUrl , response . getBody () );
183
160
184
- jsonResponse = ResponseParser . getJsonDataFromJsonResponse ( jsonString ) ;
161
+ return response ;
185
162
} catch (IOException | URISyntaxException e ) {
186
163
BitPayExceptionProvider .throwApiExceptionWithMessage (e .getMessage ());
164
+ throw new BitPayApiException (e .getMessage (), null );
187
165
}
188
-
189
- return jsonResponse ;
190
166
}
191
167
192
168
/**
@@ -198,7 +174,7 @@ public String delete(
198
174
* @throws BitPayApiException BitPayApiException
199
175
* @throws BitPayGenericException BitPayGenericException
200
176
*/
201
- public String post (
177
+ public HttpResponse post (
202
178
final String uri ,
203
179
final String json
204
180
) throws BitPayApiException , BitPayGenericException {
@@ -215,45 +191,33 @@ public String post(
215
191
* @throws BitPayApiException BitPayApiException
216
192
* @throws BitPayGenericException BitPayGenericException
217
193
*/
218
- public String post (
194
+ public HttpResponse post (
219
195
final String uri ,
220
196
final String json ,
221
197
final boolean signatureRequired
222
198
) throws BitPayApiException , BitPayGenericException {
223
- String jsonResponse = null ;
224
-
225
199
try {
226
200
final String endpoint = this .baseUrl + uri ;
227
201
final HttpPost httpPost = this .httpRequestFactory .createHttpPost (endpoint );
228
-
229
202
httpPost .setEntity (new ByteArrayEntity (json .getBytes (StandardCharsets .UTF_8 )));
230
203
204
+ this .addDefaultHeaders (httpPost );
205
+ httpPost .addHeader ("Content-Type" , "application/json" );
231
206
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 );
234
208
}
235
209
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
-
242
210
LoggerProvider .getLogger ().logRequest (HttpPost .METHOD_NAME , endpoint , httpPost .toString ());
243
211
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 ));
248
213
249
- LoggerProvider .getLogger ().logResponse (HttpGet .METHOD_NAME , endpoint , jsonString );
214
+ LoggerProvider .getLogger ().logResponse (HttpGet .METHOD_NAME , endpoint , response . getBody () );
250
215
251
- jsonResponse = ResponseParser . getJsonDataFromJsonResponse ( jsonString ) ;
216
+ return response ;
252
217
} catch (IOException e ) {
253
218
BitPayExceptionProvider .throwApiExceptionWithMessage (e .getMessage ());
219
+ throw new BitPayApiException (e .getMessage (), null );
254
220
}
255
-
256
- return jsonResponse ;
257
221
}
258
222
259
223
/**
@@ -265,7 +229,7 @@ public String post(
265
229
* @throws BitPayApiException BitPayApiException
266
230
* @throws BitPayGenericException BitPayGenericException
267
231
*/
268
- public String update (
232
+ public HttpResponse update (
269
233
final String uri ,
270
234
final String json
271
235
) throws BitPayApiException , BitPayGenericException {
@@ -277,8 +241,7 @@ public String update(
277
241
278
242
httpPut .setEntity (new ByteArrayEntity (json .getBytes (StandardCharsets .UTF_8 )));
279
243
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 );
282
245
httpPut .addHeader ("x-accept-version" , Config .BITPAY_API_VERSION );
283
246
httpPut .addHeader ("X-BitPay-Plugin-Info" , Config .BITPAY_PLUGIN_INFO );
284
247
httpPut .addHeader ("Content-Type" , "application/json" );
@@ -287,18 +250,27 @@ public String update(
287
250
288
251
LoggerProvider .getLogger ().logRequest (HttpPut .METHOD_NAME , endpoint , httpPut .toString ());
289
252
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 ));
294
254
295
- LoggerProvider .getLogger ().logResponse (HttpPut .METHOD_NAME , endpoint , jsonString );
255
+ LoggerProvider .getLogger ().logResponse (HttpPut .METHOD_NAME , endpoint , response . getBody () );
296
256
297
- jsonResponse = ResponseParser . getJsonDataFromJsonResponse ( jsonString ) ;
257
+ return response ;
298
258
} catch (IOException e ) {
299
259
BitPayExceptionProvider .throwApiExceptionWithMessage (e .getMessage ());
260
+ throw new BitPayApiException (e .getMessage (), null );
300
261
}
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
+ }
301
270
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 ()));
303
275
}
304
276
}
0 commit comments