From 7dbab2a7af396fd02012cc1527e006425e7d3aa4 Mon Sep 17 00:00:00 2001
From: P15GEN2\59518 <lilith@highdatas.com>
Date: 星期六, 18 十月 2025 12:51:53 +0800
Subject: [PATCH] no message

---
 foundation.icall/src/foundation/icall/connector/BaiduAIConn.java |  130 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/foundation.icall/src/foundation/icall/connector/BaiduAIConn.java b/foundation.icall/src/foundation/icall/connector/BaiduAIConn.java
new file mode 100644
index 0000000..5e13606
--- /dev/null
+++ b/foundation.icall/src/foundation/icall/connector/BaiduAIConn.java
@@ -0,0 +1,130 @@
+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();
+	}
+}

--
Gitblit v1.8.0