Skip to content

Instantly share code, notes, and snippets.

@sirolf2009
Last active August 29, 2015 14:27
Show Gist options
  • Save sirolf2009/4f815efc75e582949969 to your computer and use it in GitHub Desktop.
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
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