com.konfigthis.client.ApiClientCustom Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snaptrade-java-sdk Show documentation
Show all versions of snaptrade-java-sdk Show documentation
Connect brokerage accounts to your app for live positions and trading
This library was generated by https://konfigthis.com
package com.konfigthis.client;
import com.google.gson.Gson;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class ApiClientCustom {
protected void requestBeforeHook(String baseUrl, String path, String method, List queryParams,
List collectionQueryParams, Object body, Map headerParams,
Map cookieParams, Map formParams,
String[] authNames, ApiClient client) {
Pair timestamp = new Pair("timestamp", String.valueOf(System.currentTimeMillis() / 1000L));
queryParams.add(timestamp);
}
protected void requestAfterHook(String url, String path, String method, List queryParams,
List collectionQueryParams, Object body, Map headerParams,
Map cookieParams, Map formParams,
String[] authNames, String payload, ApiClient client) {
if (client.consumerKey == null)
throw new RuntimeException("Set the consumer key by configuring Configuration#consumerKey");
Gson gson = JSON.getGson();
Object map = body instanceof List ? body : gson.fromJson(payload, TreeMap.class);
String sortedJson = map == null ? "\"\"" : gson.toJson(map);
String queryString = url.split("\\?")[1];
String data = String.format("{\"content\":%s,\"path\":\"/api/v1%s\",\"query\":\"%s\"}",
payload == null || payload.equals("") || payload.equals("{}") ? "null" : sortedJson, path, queryString);
byte[] hmacSha256Bytes;
try {
hmacSha256Bytes = calculateHmacSha256(data, client.consumerKey);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException(e);
}
String hmacSha256Base64 = encodeBase64(hmacSha256Bytes);
headerParams.put("Signature", hmacSha256Base64);
}
private static byte[] calculateHmacSha256(String message, String key)
throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKeySpec);
return mac.doFinal(message.getBytes(StandardCharsets.UTF_8));
}
private static String encodeBase64(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
}