Don't like ads? PRO users don't see any ads ;-)
Guest

btc-e.com trade API getInfo call

By: a guest on Aug 4th, 2012  |  syntax: Java  |  size: 3.32 KB  |  hits: 818  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1.    private static long _nonce;
  2.  
  3.    // Create a unixtime nonce for the new API.
  4.    _nonce = ( TradeApp.getApp().getCurrentGMTTimeMicros() / 1000000);
  5.  
  6.     /**
  7.      * Execute a authenticated query on btc-e.
  8.      *
  9.      * @param method The method to execute.
  10.      * @param arguments The arguments to pass to the server.
  11.      *
  12.      * @return The returned data as JSON or null, if the request failed.
  13.      *
  14.      * @see http://pastebin.com/K25Nk2Sv
  15.      */
  16.     private final JSONObject authenticatedHTTPRequest( String method, Map<String, String> arguments) {
  17.         HashMap<String, String> headerLines = new HashMap<String, String>();  // Create a new map for the header lines.
  18.         Mac mac;
  19.         SecretKeySpec key = null;
  20.  
  21.         if( arguments == null) {  // If the user provided no arguments, just create an empty argument array.
  22.             arguments = new HashMap<String, String>();
  23.         }
  24.        
  25.         arguments.put( "method", method);  // Add the method to the post data.
  26.         arguments.put( "nonce",  "" + ++_nonce);  // Add the dummy nonce.
  27.  
  28.         String postData = "";
  29.  
  30.         for( Iterator argumentIterator = arguments.entrySet().iterator(); argumentIterator.hasNext(); ) {
  31.             Map.Entry argument = (Map.Entry)argumentIterator.next();
  32.            
  33.             if( postData.length() > 0) {
  34.                 postData += "&";
  35.             }
  36.             postData += argument.getKey() + "=" + argument.getValue();
  37.         }
  38.  
  39.         // Create a new secret key
  40.         try {
  41.             key = new SecretKeySpec( _secret.getBytes( "UTF-8"), "HmacSHA512" );
  42.         } catch( UnsupportedEncodingException uee) {
  43.             System.err.println( "Unsupported encoding exception: " + uee.toString());
  44.             return null;
  45.         }
  46.  
  47.         // Create a new mac
  48.         try {
  49.             mac = Mac.getInstance( "HmacSHA512" );
  50.         } catch( NoSuchAlgorithmException nsae) {
  51.             System.err.println( "No such algorithm exception: " + nsae.toString());
  52.             return null;
  53.         }
  54.  
  55.         // Init mac with key.
  56.         try {
  57.             mac.init( key);
  58.         } catch( InvalidKeyException ike) {
  59.             System.err.println( "Invalid key exception: " + ike.toString());
  60.             return null;
  61.         }
  62.  
  63.         // Add the key to the header lines.
  64.         headerLines.put( "Key", _key);
  65.  
  66.         // Encode the post data by the secret and encode the result as base64.
  67.         try {
  68.             headerLines.put( "Sign", Hex.encodeHexString( mac.doFinal( postData.getBytes( "UTF-8"))));
  69.         } catch( UnsupportedEncodingException uee) {
  70.             System.err.println( "Unsupported encoding exception: " + uee.toString());
  71.             return null;
  72.         }
  73.        
  74.         // Now do the actual request
  75.         String requestResult = HttpUtils.httpPost( "https://" + DOMAIN + "/tapi", headerLines, postData);
  76.  
  77.         if( requestResult != null) {   // The request worked
  78.  
  79.             try {
  80.                 // Convert the HTTP request return value to JSON to parse further.
  81.                 JSONObject jsonResult = JSONObject.fromObject( requestResult);
  82.  
  83.                 // Check, if the request was successful
  84.                 int success = jsonResult.getInt( "success");
  85.  
  86.                 if( success == 0) {  // The request failed.
  87.                     String errorMessage = jsonResult.getString( "error");
  88.                    
  89.                     System.err.println( "btc-e.com trade API request failed: " + errorMessage);
  90.  
  91.                     return null;
  92.                 } else {  // Request succeeded!
  93.                     return jsonResult.getJSONObject( "return");
  94.                 }
  95.  
  96.             } catch( JSONException je) {
  97.                 System.err.println( "Cannot parse json request result: " + je.toString());
  98.  
  99.                 return null;  // An error occured...
  100.             }
  101.         }
  102.  
  103.         return null;  // The request failed.
  104.     }