BitcoinCoIDWrapper.java

NuBotTrading / NuBot / src / com / nubits / nubot / trading / wrappers / BitcoinCoIDWrapper.java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package com.nubits.nubot.trading.wrappers;

import com.nubits.nubot.exchanges.Exchange;
import com.nubits.nubot.global.Constant;
import com.nubits.nubot.global.Global;
import com.nubits.nubot.models.*;
import com.nubits.nubot.models.Currency;
import com.nubits.nubot.trading.ServiceInterface;
import com.nubits.nubot.trading.Ticker;
import com.nubits.nubot.trading.TradeInterface;
import com.nubits.nubot.trading.TradeUtils;
import com.nubits.nubot.trading.keys.ApiKeys;
import com.nubits.nubot.utils.ErrorManager;
import java.io.*;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.NumberFormat;
import java.util.*;
import java.util.logging.Logger;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.codec.binary.Hex;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 * Created by sammoth on 19/01/15.
 */
public class BitcoinCoIDWrapper implements TradeInterface {

    private static final Logger LOG = Logger.getLogger(BitcoinCoIDWrapper.class.getName());
    //Class fields
    private ApiKeys keys;
    private Exchange exchange;
    private final String SIGN_HASH_FUNCTION = "HmacSHA512";
    private final String ENCODING = "UTF-8";
    //API Paths
    private final String API_BASE_URL = "https://vip.bitcoin.co.id/tapi";
    private final String API_TICKER_URL = "https://vip.bitcoin.co.id/api/";
    private final String API_TICKER = "ticker";
    private final String API_GET_INFO = "getInfo";
    private final String API_TRADE = "trade";
    private final String API_OPEN_ORDERS = "openOrders";
    private final String API_CANCEL_ORDER = "cancelOrder";
    private final String API_TRADE_HISTORY = "tradeHistory";
    //Errors
    private ErrorManager errors = new ErrorManager();
    private final String TOKEN_ERR = "error";
    private final String TOKEN_BAD_RETURN = "No Connection With Exchange";
    private final String TOKEN_CODE = "success";

    public BitcoinCoIDWrapper() {
        setupErrors();
    }

    public BitcoinCoIDWrapper(ApiKeys keys, Exchange exchange) {
        this.keys = keys;
        this.exchange = exchange;
        setupErrors();
    }

    private void setupErrors() {
        errors.setExchangeName(exchange);
    }

    private ApiResponse getQuery(String url, String method, HashMap<String, String> query_args, boolean isGet) {
        ApiResponse apiResponse = new ApiResponse();
        String queryResult = query(url, method, query_args, isGet);
        if (queryResult == null) {
            apiResponse.setError(errors.nullReturnError);
            return apiResponse;
        }
        if (queryResult.equals(TOKEN_BAD_RETURN)) {
            apiResponse.setError(errors.noConnectionError);
            return apiResponse;
        }
        JSONParser parser = new JSONParser();

        try {
            JSONObject httpAnswerJson = (JSONObject) (parser.parse(queryResult));
            int code = 0;
            try {
                code = Integer.parseInt(httpAnswerJson.get(TOKEN_CODE).toString());
            } catch (ClassCastException cce) {
                apiResponse.setError(errors.genericError);
            }

            if (code == 0) {
                String errorMessage = (String) httpAnswerJson.get(TOKEN_ERR);
                ApiError apiError = errors.apiReturnError;
                apiError.setDescription(errorMessage);
                //LOG.severe("AllCoin API returned an error : " + errorMessage);
                apiResponse.setError(apiError);
            } else {
                apiResponse.setResponseObject(httpAnswerJson);
            }
        } catch (ClassCastException cce) {
            //if casting to a JSON object failed, try a JSON Array
            try {
                JSONArray httpAnswerJson = (JSONArray) (parser.parse(queryResult));
                apiResponse.setResponseObject(httpAnswerJson);
            } catch (ParseException pe) {
                LOG.severe("httpResponse: " + queryResult + " \n" + pe.toString());
                apiResponse.setError(errors.parseError);
            }
        } catch (ParseException pe) {
            LOG.severe("httpResponse: " + queryResult + " \n" + pe.toString());
            apiResponse.setError(errors.parseError);
            return apiResponse;
        }
        return apiResponse;
    }

    @Override
    public ApiResponse getAvailableBalances(CurrencyPair pair) {
        return getBalanceImpl(null, pair);
    }

    @Override
    public ApiResponse getAvailableBalance(Currency currency) {
        return getBalanceImpl(currency, null);
    }

    private ApiResponse getBalanceImpl(Currency currency, CurrencyPair pair) {
        ApiResponse apiResponse = new ApiResponse();
        boolean isGet = false;
        String url = API_BASE_URL;
        String method = API_GET_INFO;
        HashMap<String, String> query_args = new HashMap<>();

        ApiResponse response = getQuery(url, method, query_args, isGet);
        if (response.isPositive()) {
            JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
            JSONObject data = (JSONObject) httpAnswerJson.get("return");
            JSONObject balances = (JSONObject) data.get("balance");
            if (currency == null) { //get the balances for the pair
                double pegAvail = Double.parseDouble(balances.get(pair.getPaymentCurrency().getCode().toLowerCase()).toString());
                Amount PEGAvail = new Amount(pegAvail, pair.getPaymentCurrency());
                double nbtAvail = Double.parseDouble(balances.get(pair.getOrderCurrency().getCode().toLowerCase()).toString());
                Amount NBTAvail = new Amount(nbtAvail, pair.getOrderCurrency());
                double pegOnOrder = 0;
                double nbtOnOrder = 0;
                ArrayList<Order> orders = (ArrayList) getActiveOrders(pair).getResponseObject();
                for (Iterator<Order> order = orders.iterator(); order.hasNext();) {
                    Order thisOrder = order.next();
                    if (thisOrder.getType().equals(Constant.SELL)) {
                        nbtOnOrder += thisOrder.getAmount().getQuantity();
                    } else {
                        pegOnOrder += thisOrder.getAmount().getQuantity();
                    }
                }
                Amount PEGonOrder = new Amount(pegOnOrder, pair.getPaymentCurrency());
                Amount NBTonOrder = new Amount(nbtOnOrder, pair.getOrderCurrency());
                Balance balance = new Balance(PEGAvail, NBTAvail, PEGonOrder, NBTonOrder);
                apiResponse.setResponseObject(balance);
            } else {
                double balance = Double.parseDouble(balances.get(currency.getCode().toLowerCase()).toString());
                Amount total = new Amount(balance, currency);
                apiResponse.setResponseObject(total);
            }
        } else {
            apiResponse = response;
        }
        return apiResponse;
    }

    @Override
    public ApiResponse getLastPrice(CurrencyPair pair) {
        ApiResponse apiResponse = new ApiResponse();
        String url = API_TICKER_URL + pair.toString("_") + "/" + API_TICKER;
        HashMap<String, String> query_args = new HashMap<>();
        boolean isGet = true;

        double last = -1;
        double ask = -1;
        double bid = -1;
        Ticker ticker = new Ticker();

        String queryResult = query(url, query_args, isGet);
        JSONParser parser = new JSONParser();
        JSONObject httpAnswerJson = null;
        try {
            httpAnswerJson = (JSONObject) parser.parse(queryResult);
        } catch (ParseException pe) {
            LOG.severe(pe.toString());
            ApiError error = errors.apiReturnError;
            error.setDescription("Error parsing ticker response");
            apiResponse.setError(error);
            return apiResponse;
        }
        JSONObject tick = (JSONObject) httpAnswerJson.get("ticker");
        last = Double.parseDouble(tick.get("last").toString());
        ask = Double.parseDouble(tick.get("sell").toString());
        bid = Double.parseDouble(tick.get("buy").toString());
        ticker.setLast(last);
        ticker.setAsk(ask);
        ticker.setBid(bid);
        apiResponse.setResponseObject(ticker);
        return apiResponse;
    }

    @Override
    public ApiResponse sell(CurrencyPair pair, double amount, double rate) {
        return enterOrder(Constant.SELL, pair, amount, rate);
    }

    @Override
    public ApiResponse buy(CurrencyPair pair, double amount, double rate) {
        return enterOrder(Constant.BUY, pair, amount, rate);
    }

    private ApiResponse enterOrder(String type, CurrencyPair pair, double amount, double price) {
        ApiResponse apiResponse = new ApiResponse();
        String url = API_BASE_URL;
        String method = API_TRADE;
        HashMap<String, String> args = new HashMap<>();
        boolean isGet = false;
        String order_id = null;

        args.put("pair", pair.toString("_"));
        args.put("type", type.toLowerCase());
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumFractionDigits(8);
        args.put("price", nf.format(price));
        if (type.equals(Constant.SELL)) {
            args.put(pair.getPaymentCurrency().getCode().toLowerCase(), nf.format(amount * price));
            args.put(pair.getOrderCurrency().getCode().toLowerCase(), nf.format(amount));
        } else {
            args.put(pair.getPaymentCurrency().getCode().toLowerCase(), nf.format(amount));
        }

        ApiResponse response = getQuery(url, method, args, isGet);
        if (response.isPositive()) {
            JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
            JSONObject data = (JSONObject) httpAnswerJson.get("return");
            order_id = data.get("order_id").toString();
            apiResponse.setResponseObject(order_id);
        } else {
            apiResponse = response;
        }
        return apiResponse;
    }

    @Override
    public ApiResponse getActiveOrders() {
        return getActiveOrdersImpl(null);
    }

    @Override
    public ApiResponse getActiveOrders(CurrencyPair pair) {
        return getActiveOrdersImpl(pair);
    }

    private ApiResponse getActiveOrdersImpl(CurrencyPair pair) {
        ApiResponse apiResponse = new ApiResponse();
        String url = API_BASE_URL;
        String method = API_OPEN_ORDERS;
        ArrayList<Order> orderList = new ArrayList<>();
        HashMap<String, String> query_args = new HashMap<>();
        boolean isGet = false;


        //only handles openOrders with given pair
        if (pair != null) {
            query_args.put("pair", pair.toString("_"));
        } else {
            query_args.put("pair", Global.options.getPair().toString("_"));
        }

        ApiResponse response = getQuery(url, method, query_args, isGet);
        if (response.isPositive()) {
            JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
            JSONObject data = (JSONObject) httpAnswerJson.get("return");
            JSONArray orders = (JSONArray) data.get("orders");
            if (orders != null) {
                for (Iterator<JSONObject> order = orders.iterator(); order.hasNext();) {
                    JSONObject thisOrder = order.next();
                    orderList.add(parseOrder(thisOrder));
                }
            }
            apiResponse.setResponseObject(orderList);
        } else {
            apiResponse = response;
        }
        return apiResponse;
    }

    private Order parseOrder(JSONObject in) {
        Order out = new Order();
        ;
        out.setId(in.get("order_id").toString());
        Date insertedDate = new Date(Long.parseLong(in.get("submit_time").toString()) * 1000L);
        out.setInsertedDate(insertedDate);
        out.setType(in.get("type").toString().equals("buy") ? Constant.BUY : Constant.SELL);
        String cur;
        Amount amount;
        Amount price = new Amount(Double.parseDouble(in.get("price").toString()), Global.options.getPair().getPaymentCurrency());
        if (out.getType().equals(Constant.BUY)) {
            cur = Global.options.getPair().getPaymentCurrency().getCode().toLowerCase();
            amount = new Amount(Double.parseDouble(in.get("order_" + cur).toString()) / price.getQuantity(), Global.options.getPair().getOrderCurrency());
        } else {
            cur = Global.options.getPair().getOrderCurrency().getCode().toLowerCase();
            amount = new Amount(Double.parseDouble(in.get("order_" + cur).toString()), Global.options.getPair().getOrderCurrency());
        }
        out.setAmount(amount);
        out.setPrice(price);
        out.setCompleted(amount.getQuantity() == Double.parseDouble(in.get("remain_" + cur).toString()));

        return out;
    }

    @Override
    public ApiResponse getOrderDetail(String orderID) {
        ApiResponse apiResponse = new ApiResponse();
        ArrayList<Order> activeOrders = (ArrayList<Order>) getActiveOrders().getResponseObject();
        boolean found = false;
        for (Iterator<Order> order = activeOrders.iterator(); order.hasNext();) {
            Order thisOrder = order.next();
            if (thisOrder.getId().equals(orderID)) {
                found = true;
                apiResponse.setResponseObject(thisOrder);
                break;
            }
        }

        if (!found) {
            ApiError err = errors.apiReturnError;
            err.setDescription("Order " + orderID + " does not exists");
            apiResponse.setError(err);
        }
        return apiResponse;
    }

    @Override
    public ApiResponse cancelOrder(String orderID, CurrencyPair pair) {
        ApiResponse apiResponse = new ApiResponse();
        String url = API_BASE_URL;
        String method = API_CANCEL_ORDER;
        boolean isGet = false;
        HashMap<String, String> query_args = new HashMap<>();

        query_args.put("pair", pair.toString("_"));
        query_args.put("order_id", orderID);
        Order currentOrder = (Order) getOrderDetail(orderID).getResponseObject();
        query_args.put("type", currentOrder.getType().toLowerCase());

        ApiResponse response = getQuery(url, method, query_args, isGet);
        if (response.isPositive()) {
            JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
            JSONObject data = (JSONObject) httpAnswerJson.get("return");
            if (data.get("order_id").toString().equals(orderID)) {
                apiResponse.setResponseObject(true);
            } else {
                apiResponse.setResponseObject(false);
            }
        } else {
            apiResponse = response;
        }

        return apiResponse;
    }

    @Override
    public ApiResponse getTxFee() {
        double defaultFee = 0.0;
        return new ApiResponse(true, defaultFee, null);
    }

    @Override
    public ApiResponse getTxFee(CurrencyPair pair) {
        double defaultFee = 0.0;
        return new ApiResponse(true, defaultFee, null);
    }

    @Override
    public ApiResponse getLastTrades(CurrencyPair pair) {
        return getLastTradesImpl(pair, 0);
    }

    @Override
    public ApiResponse getLastTrades(CurrencyPair pair, long startTime) {
        return getLastTradesImpl(pair, startTime);
    }

    private ApiResponse getLastTradesImpl(CurrencyPair pair, long startTime) {
        ApiResponse apiResponse = new ApiResponse();
        String url = API_BASE_URL;
        String method = API_TRADE_HISTORY;
        boolean isGet = false;
        HashMap<String, String> query_args = new HashMap<>();
        ArrayList<Trade> tradeList = new ArrayList<>();

        query_args.put("pair", pair.toString("_"));
        if (startTime > 0) {
            query_args.put("since", Objects.toString(startTime));
        }

        ApiResponse response = getQuery(url, method, query_args, isGet);
        if (response.isPositive()) {
            JSONObject httpAnswerJson = (JSONObject) response.getResponseObject();
            JSONObject data = (JSONObject) httpAnswerJson.get("return");
            JSONArray trades = (JSONArray) data.get("trades");
            for (Iterator<JSONObject> trade = trades.iterator(); trade.hasNext();) {
                JSONObject thisTrade = trade.next();
                tradeList.add(parseTrade(thisTrade, pair));
            }
            apiResponse.setResponseObject(tradeList);
        } else {
            apiResponse = response;
        }


        return apiResponse;
    }

    private Trade parseTrade(JSONObject in, CurrencyPair pair) {
        Trade out = new Trade();

        out.setId(in.get("trade_id").toString());
        out.setType(in.get("type").toString().equals("buy") ? Constant.BUY : Constant.SELL);
        Date tradeDate = new Date(Long.parseLong(in.get("trade_time").toString()) * 1000L);
        out.setDate(tradeDate);
        out.setExchangeName(Global.exchange.getName());
        Amount fee = new Amount(Double.parseDouble(in.get("fee").toString()), pair.getPaymentCurrency());
        out.setFee(fee);
        Amount amount = new Amount(Double.parseDouble(in.get(pair.getOrderCurrency().getCode().toLowerCase()).toString()), pair.getOrderCurrency());
        out.setAmount(amount);
        Amount price = new Amount(Double.parseDouble(in.get("price").toString()), pair.getPaymentCurrency());
        out.setPrice(price);
        out.setPair(pair);

        return out;
    }

    @Override
    public ApiResponse isOrderActive(String id) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse = getOrderDetail(id);
        if (apiResponse.isPositive()) {
            Order order = (Order) apiResponse.getResponseObject();
            if (order.isCompleted()) {
                apiResponse.setResponseObject(true);
            } else {
                apiResponse.setResponseObject(false);
            }
        } else {
            apiResponse.setResponseObject(false);
        }
        return apiResponse;
    }

    @Override
    public ApiResponse clearOrders(CurrencyPair pair) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setResponseObject(true);
        ApiResponse getOrders = getActiveOrders();

        if (getOrders.isPositive()) {
            ArrayList<Order> orders = (ArrayList) getOrders.getResponseObject();
            for (Iterator<Order> order = orders.iterator(); order.hasNext();) {
                if (!(boolean) cancelOrder(order.next().getId(), pair).getResponseObject()) {
                    apiResponse.setResponseObject(false);
                }
            }
        } else {
            apiResponse = getOrders;
        }

        return apiResponse;
    }

    @Override
    public ApiError getErrorByCode(int code) {
        return null;
    }

    @Override
    public String getUrlConnectionCheck() {
        return API_BASE_URL;
    }

    @Override
    public String query(String url, HashMap<String, String> args, boolean isGet) {
        BitcoinCoIdService query = new BitcoinCoIdService(url, args);
        String queryResult;
        if (exchange.getLiveData().isConnected()) {
            queryResult = query.executeQuery(false, isGet);
        } else {
            LOG.severe("The bot will not execute the query, there is no connection to BitcoinCoId");
            queryResult = TOKEN_BAD_RETURN;
        }
        return queryResult;
    }

    @Override
    public String query(String base, String method, HashMap<String, String> args, boolean isGet) {
        BitcoinCoIdService query = new BitcoinCoIdService(base, method, args, keys);
        String queryResult;
        if (exchange.getLiveData().isConnected()) {
            queryResult = query.executeQuery(true, isGet);
        } else {
            LOG.severe("The bot will not execute the query, there is no connection to BitcoinCoId");
            queryResult = TOKEN_BAD_RETURN;
        }
        return queryResult;
    }

    @Override
    public String query(String url, TreeMap<String, String> args, boolean isGet) {
        return null;
    }

    @Override
    public String query(String base, String method, TreeMap<String, String> args, boolean isGet) {
        return null;
    }

    @Override
    public void setKeys(ApiKeys keys) {
    }

    @Override
    public void setExchange(Exchange exchange) {
    }

    @Override
    public void setApiBaseUrl(String apiBaseUrl) {
    }

    private class BitcoinCoIdService implements ServiceInterface {

        protected String url;
        protected HashMap args;
        protected ApiKeys keys;
        protected String method;

        public BitcoinCoIdService(String url, String method, HashMap<String, String> args, ApiKeys keys) {
            this.url = url;
            this.args = args;
            this.keys = keys;
            this.method = method;
        }

        private BitcoinCoIdService(String url, HashMap<String, String> args) {
            //Used for ticker, does not require auth
            this.url = url;
            this.args = args;
        }

        @Override
        public String executeQuery(boolean needAuth, boolean isGet) {
            HttpsURLConnection connection = null;
            URL queryUrl = null;
            String post_data = "";
            boolean httpError = false;
            String output;
            int response = 200;
            String answer = null;

            try {
                queryUrl = new URL(url);
            } catch (MalformedURLException mal) {
                LOG.severe(mal.toString());
                return null;
            }

            if (needAuth) {
                args.put("nonce", Objects.toString(System.currentTimeMillis()));
                args.put("method", method);
                post_data = TradeUtils.buildQueryString(args, ENCODING);
            }

            try {
                connection = (HttpsURLConnection) queryUrl.openConnection();
                connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("User-Agent", Global.settings.getProperty("app_name"));

                if (needAuth) {
                    connection.setRequestProperty("Key", keys.getApiKey());
                    connection.setRequestProperty("Sign", signRequest(keys.getPrivateKey(), post_data));
                }

                connection.setDoOutput(true);
                connection.setDoInput(true);

                if (isGet) {
                    connection.setRequestMethod("GET");
                } else {
                    connection.setRequestMethod("POST");
                    DataOutputStream os = new DataOutputStream(connection.getOutputStream());
                    os.writeBytes(post_data);
                    os.flush();
                    os.close();
                }
            } catch (ProtocolException pe) {
                LOG.severe(pe.toString());
                return null;
            } catch (IOException io) {
                LOG.severe((io.toString()));
                return null;
            }


            BufferedReader br = null;
            try {
                if (connection.getResponseCode() >= 400) {
                    httpError = true;
                    response = connection.getResponseCode();
                    br = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                } else {
                    answer = "";
                    br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                }
            } catch (IOException io) {
                LOG.severe(io.toString());
                return null;
            }

            if (httpError) {
                LOG.severe("Query to : " + url + " (method = " + method + " )"
                        + "\nData : \" + post_data"
                        + "\nHTTP Response : " + Objects.toString(response));
            }

            try {
                while ((output = br.readLine()) != null) {
                    answer += output;
                }
            } catch (IOException io) {
                LOG.severe(io.toString());
                return null;
            }

            connection.disconnect();
            connection = null;

            return answer;
        }

        @Override
        public String signRequest(String secret, String hash_data) {
            String sign = "";
            try {
                Mac mac;
                SecretKeySpec key;
                // Create a new secret key
                key = new SecretKeySpec(secret.getBytes(ENCODING), SIGN_HASH_FUNCTION);
                // Create a new mac
                mac = Mac.getInstance(SIGN_HASH_FUNCTION);
                // Init mac with key.
                mac.init(key);
                sign = Hex.encodeHexString(mac.doFinal(hash_data.getBytes(ENCODING)));
            } catch (UnsupportedEncodingException uee) {
                LOG.severe("Unsupported encoding exception: " + uee.toString());
            } catch (NoSuchAlgorithmException nsae) {
                LOG.severe("No such algorithm exception: " + nsae.toString());
            } catch (InvalidKeyException ike) {
                LOG.severe("Invalid key exception: " + ike.toString());
            }
            return sign;
        }
    }
}