package foundation.icall.connector;
|
|
import com.tencentcloudapi.common.Credential;
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
import com.tencentcloudapi.common.profile.HttpProfile;
|
import com.tencentcloudapi.common.profile.Language;
|
import com.tencentcloudapi.ocr.v20181119.OcrClient;
|
|
import foundation.dao.DataPackage;
|
import foundation.dao.DataSource;
|
import foundation.dao.bizlogic.DocDescription;
|
import foundation.dao.bizlogic.IRequest;
|
import foundation.data.entity.Entity;
|
import foundation.icall.ContentType;
|
import foundation.icall.ICall;
|
import foundation.icall.callout.ICallRequest;
|
import foundation.io.template.Template;
|
import foundation.util.MapList;
|
import foundation.workflow.WorkStep;
|
|
public class TencentAIConn extends HttpServerConn {
|
|
private static TencentAIConn instance;
|
private static String monitorId = "腾讯AI";
|
public static int TimeOut_Second_Write = 10;
|
public static int TimeOut_Second_Read = 10;
|
public static boolean Debug = true;
|
public OcrClient client;
|
|
private TencentAIConn() {
|
|
}
|
|
public static synchronized TencentAIConn getInstance() {
|
if (instance == null) {
|
instance = new TencentAIConn();
|
}
|
|
return instance;
|
}
|
|
@Override
|
public void login(WorkStep step, ICall iCall) throws Exception {
|
createClient();
|
}
|
|
@Override
|
public void logout(WorkStep step, ICall iCall) throws Exception {
|
|
}
|
|
@Override
|
public ICallRequest createRequest(String url) {
|
String host = meta.getString("host");
|
ICallRequest request = new ICallRequest(host + url);
|
|
|
return request;
|
}
|
|
@Override
|
public IRequest buildBody(IRequest request, WorkStep step, ICall iCall) throws Exception {
|
DataPackage dataPackage = step.getDataPackage();
|
|
String jsonBody = null;
|
ContentType contentType = iCall.getContentType();
|
|
dataPackage.loadDataFromRequest();
|
Entity master = dataPackage.getMasterEntity(DataSource.Request);
|
|
MapList<String, Template> templateList = iCall.getRequestTemplateList();
|
|
if (templateList != null && templateList.size() > 0) {
|
Template template = templateList.get(0);
|
jsonBody = template.fillVariants(dataPackage);
|
}
|
|
if (master != null) {
|
request.setDocDescription(new DocDescription(master.getId(), master.getString("code")));
|
}
|
|
request.setContentType(contentType.getCode());
|
request.setJSONBody(jsonBody);
|
|
return request;
|
}
|
|
private void createClient() throws Exception {
|
String secretId = meta.getString("secret_id");
|
String secretKey = meta.getString("secret_key");
|
String region = meta.getString("region");
|
String host = meta.getString("host");
|
|
//1. credential
|
Credential credential = new Credential(secretId, secretKey);
|
|
//2. profile
|
HttpProfile httpProfile = new HttpProfile();
|
httpProfile.setWriteTimeout(TimeOut_Second_Write);
|
httpProfile.setReadTimeout(TimeOut_Second_Read);
|
httpProfile.setEndpoint(host);
|
|
ClientProfile clientProfile = new ClientProfile();
|
clientProfile.setHttpProfile(httpProfile);
|
clientProfile.setDebug(Debug);
|
clientProfile.setLanguage(Language.ZH_CN);
|
|
//3. client
|
this.client = new OcrClient(credential, region, clientProfile);
|
}
|
}
|