Last active
August 29, 2015 14:27
-
-
Save sirolf2009/4f815efc75e582949969 to your computer and use it in GitHub Desktop.
A request class for the apache http client library. Use it by adding your api keys and go client.execute(new HttpBitfinex("/margin_infos")). Requires commons codec, commons io, gson and apache httpclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sirolf2009.bitfinex.calls.auth; | |
import java.io.UnsupportedEncodingException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.crypto.Mac; | |
import org.apache.commons.codec.binary.Base64; | |
import org.apache.commons.codec.binary.Hex; | |
import org.apache.commons.codec.digest.HmacUtils; | |
import org.apache.http.Header; | |
import org.apache.http.client.methods.HttpPost; | |
import com.google.gson.Gson; | |
public class HttpBitfinex extends HttpPost { | |
private static final String publicKey = ""; | |
private static final String secretKey = ""; | |
private static final String baseUrl = "https://api.bitfinex.com/v1"; | |
private static final String basePath = "/v1"; | |
public HttpBitfinex(String request) { | |
super(baseUrl+request); | |
addHeader("request", basePath+request); | |
addHeader("nonce", getNonce()); | |
} | |
@Override | |
public void addHeader(Header header) { | |
super.addHeader(header); | |
updatePayload(); | |
} | |
public void updatePayload() { | |
try { | |
Map<String, String> headers = new HashMap<String, String>(); | |
for(Header header : getAllHeaders()) { | |
headers.put(header.getName(), header.getValue()); | |
} | |
byte[] payload = new Base64().encode(new Gson().toJson(headers).getBytes("UTF-8")); | |
Mac mac = HmacUtils.getHmacSha384(secretKey.getBytes("UTF-8")); | |
byte[] signature = mac.doFinal(payload); | |
addHeader("X-BFX-APIKEY", publicKey); | |
addHeader("X-BFX-PAYLOAD", new String(payload)); | |
addHeader("X-BFX-SIGNATURE", Hex.encodeHexString(signature)); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
} | |
public String getNonce() { | |
return System.currentTimeMillis()+""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment