Skip to content

Commit 27eeaf9

Browse files
author
liuyuzhe.316
committed
feat: 支持代理
1 parent b4fd8fc commit 27eeaf9

File tree

1 file changed

+54
-9
lines changed
  • volcengine-java-sdk-llmshield/src/main/java/com/volcengine/llmshield

1 file changed

+54
-9
lines changed

volcengine-java-sdk-llmshield/src/main/java/com/volcengine/llmshield/ApiClient.java

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,58 @@
11
package com.volcengine.llmshield;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.apache.http.HttpHost;
45
import org.apache.http.HttpResponse;
56
import org.apache.http.client.HttpClient;
67
import org.apache.http.client.methods.HttpPost;
78
import org.apache.http.client.utils.URIBuilder;
89
import org.apache.http.entity.StringEntity;
10+
import org.apache.http.impl.client.CloseableHttpClient;
911
import org.apache.http.impl.client.HttpClientBuilder;
1012
import org.apache.http.util.EntityUtils;
1113

1214
import java.io.IOException;
15+
import java.net.MalformedURLException;
1316
import java.net.URI;
14-
import java.net.URISyntaxException;
15-
import java.util.HashMap;
16-
import java.util.Map;
17+
import java.net.URL;
1718
import java.util.concurrent.TimeUnit;
1819

19-
2020
// 客户端类
2121

2222
public class ApiClient {
2323
private final String CONTENT_TYPE_HEADER = "application/json";
24-
25-
2624
private final String url;
2725
private final String ak;
2826
private final String sk;
2927
private final String region;
30-
private HttpClient httpClient;
28+
private CloseableHttpClient httpClient;
29+
30+
private ApiClient(String url, String ak, String sk, String region, long timeout) {
31+
this.url = url;
32+
this.ak = ak;
33+
this.sk = sk;
34+
this.region = region;
35+
this.httpClient = HttpClientBuilder.create()
36+
.setConnectionTimeToLive(timeout, TimeUnit.MILLISECONDS)
37+
.build();
38+
}
3139

32-
private ApiClient(String url, String ak,String sk,String region, long timeout) {
40+
private ApiClient(String url, String ak, String sk, String region, long timeout, String proxy) throws MalformedURLException {
41+
URL purl = new URL(proxy);
42+
String p_protocol = purl.getProtocol(); // 协议(http/https 等)
43+
String p_host = purl.getHost(); // 主机名(域名或 IP)
44+
int p_port = purl.getPort(); // 显式指定的端口号
45+
if (p_port < 0) {
46+
p_port = purl.getDefaultPort();// 协议默认端口
47+
}
48+
HttpHost httpsProxy = new HttpHost(p_host, p_port, p_protocol);
3349
this.url = url;
3450
this.ak = ak;
3551
this.sk = sk;
3652
this.region = region;
3753
this.httpClient = HttpClientBuilder.create()
3854
.setConnectionTimeToLive(timeout, TimeUnit.MILLISECONDS)
55+
.setProxy(httpsProxy)
3956
.build();
4057
}
4158

@@ -49,10 +66,38 @@ private ApiClient(String url, String ak,String sk,String region, long timeout)
4966
* @param timeout 连接超时时间(毫秒)
5067
* @return 客户端实例
5168
*/
52-
public static ApiClient New(String url, String ak,String sk , String region, long timeout) {
69+
public static ApiClient New(String url, String ak, String sk, String region, long timeout) {
5370
return new ApiClient(url, ak,sk,region, timeout);
5471
}
5572

73+
/**
74+
* 创建新的客户端实例
75+
*
76+
* @param url API 请求的基础 URL
77+
* @param ak 访问密钥
78+
* @param sk 密钥
79+
* @param region 区域
80+
* @param proxy 代理地址
81+
* @param timeout 连接超时时间(毫秒)
82+
* @return 客户端实例
83+
*/
84+
public static ApiClient New(String url, String ak, String sk, String region, long timeout, String proxy) throws MalformedURLException {
85+
return new ApiClient(url, ak, sk, region, timeout, proxy);
86+
}
87+
88+
/**
89+
* 关闭客户端
90+
*
91+
* @return 无
92+
*/
93+
public void Close() throws IOException {
94+
try {
95+
this.httpClient.close();
96+
} catch (IOException e) {
97+
throw new RuntimeException(e);
98+
}
99+
}
100+
56101
/**
57102
* 多模态、多轮对话审核
58103
*

0 commit comments

Comments
 (0)