Created
July 20, 2018 10:33
-
-
Save enryold/cc91d517a9889fdb39c38c5992d5aa4b to your computer and use it in GitHub Desktop.
CoinTracking API - Java Example
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
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SignatureException; | |
import java.util.Date; | |
import java.util.Formatter; | |
public class CointrackingAPI { | |
private static final String endpoint = "https://cointracking.info/api/v1/"; | |
private static final String HMAC_SHA512 = "HmacSHA512"; | |
private String client; | |
private String secret; | |
public CointrackingAPI(String client, String secret) | |
{ | |
this.client = client; | |
this.secret = secret; | |
} | |
private static String calculateHMAC(String data, String key) | |
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException | |
{ | |
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512); | |
Mac mac = Mac.getInstance(HMAC_SHA512); | |
mac.init(secretKeySpec); | |
return toHexString(mac.doFinal(data.getBytes())); | |
} | |
private static String toHexString(byte[] bytes) { | |
Formatter formatter = new Formatter(); | |
for (byte b : bytes) { | |
formatter.format("%02x", b); | |
} | |
return formatter.toString(); | |
} | |
public String executePost(String method, CointrackingAPIParams params) { | |
HttpURLConnection connection = null; | |
try { | |
params.addParam("method", method); | |
params.addParam("nonce", String.valueOf(new Date().getTime())); | |
String postParameters = params.getQuery(); | |
String signature = calculateHMAC(postParameters, secret); | |
//Create connection | |
URL url = new URL(endpoint); | |
connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
connection.setRequestProperty("Content-Length", Integer.toString(postParameters.getBytes().length)); | |
connection.setRequestProperty("Key", client); | |
connection.setRequestProperty("Sign", signature.replaceAll("\n", "")); | |
connection.setUseCaches(false); | |
connection.setDoOutput(true); | |
//Send request | |
DataOutputStream wr = new DataOutputStream (connection.getOutputStream()); | |
wr.writeBytes(postParameters); | |
wr.close(); | |
//Get Response | |
InputStream is = connection.getInputStream(); | |
BufferedReader rd = new BufferedReader(new InputStreamReader(is)); | |
StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+ | |
String line; | |
while ((line = rd.readLine()) != null) { | |
response.append(line); | |
response.append('\r'); | |
} | |
rd.close(); | |
return response.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} finally { | |
if (connection != null) { | |
connection.disconnect(); | |
} | |
} | |
} | |
} |
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
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.util.AbstractMap; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class CointrackingAPIParams { | |
private List<AbstractMap.SimpleEntry<String, String>> entries = new ArrayList<>(); | |
public CointrackingAPIParams withParam(String key, String value) | |
{ | |
addParam(key, value); | |
return this; | |
} | |
public void addParam(String key, String value) | |
{ | |
entries.add(new AbstractMap.SimpleEntry<>(key, value)); | |
} | |
public String getQuery() throws UnsupportedEncodingException | |
{ | |
StringBuilder result = new StringBuilder(); | |
boolean first = true; | |
for (AbstractMap.SimpleEntry<String, String> pair : entries) | |
{ | |
if (first) | |
first = false; | |
else | |
result.append("&"); | |
result.append(URLEncoder.encode(pair.getKey(), "UTF-8")); | |
result.append("="); | |
result.append(URLEncoder.encode(pair.getValue(), "UTF-8")); | |
} | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment