P15GEN2\59518
2025-10-18 56638c01bb2cc61a92f5e03c9a1001be5b5d3699
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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);
    }
}