package foundation.icall.connector;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.Date;
|
import java.util.Objects;
|
|
import foundation.icall.ICall;
|
import foundation.icall.callout.ICallRequest;
|
import foundation.icall.callout.JSONResponse;
|
import foundation.util.Util;
|
import foundation.workflow.WorkStep;
|
import okhttp3.MediaType;
|
import okhttp3.OkHttpClient;
|
import okhttp3.Request;
|
import okhttp3.RequestBody;
|
import okhttp3.Response;
|
import sun.misc.BASE64Encoder;
|
|
public class BaiduAIConn extends HttpServerConn {
|
|
private static BaiduAIConn instance;
|
private static String monitorId = "BaiduAIConn";
|
private static int TimeOutHour = 2;
|
public Date lastTime;
|
private String token;
|
|
private BaiduAIConn() {
|
|
}
|
|
public static synchronized BaiduAIConn getInstance() {
|
if (instance == null) {
|
instance = new BaiduAIConn();
|
}
|
|
return instance;
|
}
|
|
@Override
|
public void login(WorkStep step, ICall iCall) throws Exception {
|
// if (!tokenExpired()) {
|
// return;
|
// }
|
|
getToken();
|
}
|
|
@Override
|
public void logout(WorkStep step, ICall iCall) throws Exception {
|
|
}
|
|
@Override
|
public ICallRequest createRequest(String url) {
|
String host = meta.getString("base_url");
|
ICallRequest request = new ICallRequest(host + url + token);
|
return request;
|
}
|
|
private boolean tokenExpired() {
|
if (Util.isEmpty(token)) {
|
return true;
|
}
|
|
Date now = new Date();
|
boolean result = now.getTime() - lastTime.getTime() >= TimeOutHour * 60 * 60 * 1000;
|
|
return result;
|
}
|
|
private void getToken() throws Exception {
|
// 1. build request
|
String clientId = meta.getString("client_id");
|
String clientSecret = meta.getString("client_secret");
|
String grantType = meta.getString("grant_type");
|
String bodyStr = "grant_type=" + grantType + "&client_id=" + clientId + "&client_secret=" + clientSecret;
|
|
String tokenUrl = meta.getString("token_url");
|
Request request = new Request.Builder()
|
.url(tokenUrl)
|
.addHeader("Content-Type", "application/x-www-form-urlencoded")
|
.post(RequestBody.create(MediaType.get("application/x-www-form-urlencoded"), bodyStr))
|
.build();
|
|
// 2. send request
|
OkHttpClient httpClient = new OkHttpClient();
|
Response response = httpClient.newCall(request).execute();
|
|
if (!response.isSuccessful()) {
|
return ;
|
}
|
|
JSONResponse result = new JSONResponse(response);
|
token = result.getString("access_token");
|
}
|
|
private static String imageToBase64(File file) {
|
byte[] data = null;
|
|
InputStream in = null;
|
try {
|
// 1. bytes
|
in = new FileInputStream(file);
|
data = new byte[in.available()];
|
in.read(data);
|
in.close();
|
|
// 2. encode
|
BASE64Encoder encoder = new BASE64Encoder();
|
return encoder.encode(Objects.requireNonNull(data));
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
return null;
|
}
|
|
public String getName() {
|
return meta.getName();
|
}
|
}
|