import com.alibaba.fastjson.JSONObject;
import com.envision.apim.poseidon.config.PConfig;
import com.envision.apim.poseidon.core.Poseidon;
import com.envision.apim.poseidon.request.PoseidonRequest;
import com.google.common.net.HttpHeaders;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Sample {
private static String accessKey = "AccessKey of your APP";
private static String secretKey = "SecretKey of your APP";
private static String orgId = "yourOrgId";
private static String channelId = "yourChannelId";
private static String url = "https://{domain_url}";
private static String token = "";
private static class Request extends PoseidonRequest {
public void setQueryParam(String key, Object value) {
queryEncodeParams().put(key, value);
}
public void setMethod(String method) {
this.method = method;
}
public void setBodyParams(String key, Object value) {
bodyParams().put(key, value);
}
private String method;
@Override
public String baseUri() {
return "";
}
@Override
public String method() {
return method;
}
}
public static String getToken() {
Request request = new Request();
request.setMethod("POST");
long timestamp = System.currentTimeMillis();
String temp = accessKey + timestamp + secretKey;
request.bodyParams().put("encryption", string2Sha256(temp).toLowerCase());
request.bodyParams().put("timestamp", timestamp);
request.bodyParams().put("appKey", accessKey);
try {
JSONObject response = Poseidon.config(PConfig.init().appKey(accessKey).appSecret(secretKey).debug())
.url(url + "/apim-token-service/v2.0/token/get")
.getResponse(request, JSONObject.class);
return response.getJSONObject("data").getString("accessToken");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String string2Sha256(String str) {
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
byte[] hash = messageDigest.digest(str.getBytes(StandardCharsets.UTF_8));
encodeStr = Hex.encodeHexString(hash);
} catch (NoSuchAlgorithmException e) {
}
return encodeStr;
}
@Test
public void ReadData() {
token = getToken();
Request request = new Request();
request.setQueryParam("orgId", orgId);
request.setMethod("POST");
request.headerParams().put(HttpHeaders.AUTHORIZATION, "Bearer " + token);
request.headerParams().put(HttpHeaders.CONTENT_TYPE, "application/json");
request.setBodyParams("sqlQuery", "show schemas");
request.setBodyParams("queue", null);
request.setBodyParams("itemFormat", null);
JSONObject response = Poseidon.config(PConfig.init().appKey(accessKey).appSecret(secretKey))
.url(url + "/data-federation/v2.0/channels/read/" + channelId)
.getResponse(request, JSONObject.class);
System.out.println(response);
}
}