一、Open Api本地服务开发指南
1.1 使用方式
- 安装并登录DICloak浏览器
- 打开更多设置 – 开放Api – 获取本地接口URL和密钥
- 获取示例代码,运行脚本
1.2 接口使用概述
- 接口请求方式为post时,传参方式均为body传参,传递json格式数据,不是form-data,也不是url传参数
- 接口请求方式为get时,传参为form-data
- 接口请求方式为put/patch/delete具体传参参考接口文档定义
- 接口请求成功返回200时,返回json对象,code为0作成功,如有返回数据,附加在data对象中
- 接口返回json对象,code为0作成功,如有返回数据,附加在data对象中
- 接口非200时均异常,异常代码参考http错误码,code 不为 0 时,表示失败,失败信息会附加到 msg 字段中,错误码字段参考下表
- 接口所有请求URL均需加上 /openapi,见示例代码
{
'code': 0,
'msg': 'success',
'data': {
'serial_number': 2,
'pid': '27028',
'debug_port': 17539
}
}
{
'code': 500,
'msg': 'fetch fail'
}
- 所有请求都需要加上请求头:
X-API-KEY
,值为从客户端复制出来的密钥
1.3 Python示例代码
DICloak内核用的是120内核,需要先下载 chromedriver:
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# port and api key
BASE_URL = "http://127.0.0.1:52140/openapi"
API_KEY = "*****"
def open_browser(port):
"""
hold profile
:param port:
:return:
"""
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('debuggerAddress', f"127.0.0.1:{port}")
s = Service(r"G:\chromedriver.exe")
browser = webdriver.Chrome(options=chrome_options, service=s)
#示例url代码
browser.get("https://www.zhihu.com")
def get_evn_list():
headers = {
"X-API-KEY": API_KEY,
}
params = {
"page_no": 1,
"page_size": 20
}
response = requests.get(f"{BASE_URL}/v1/env/list", params=params, headers=headers)
return response.json()
def open_env(env_id):
"""
open profile
:param env_id: id
:return:
"""
headers = {
"X-API-KEY": API_KEY,
}
response = requests.patch(f"{BASE_URL}/v1/env/{env_id}/open", headers=headers)
return response.json()
def close_env(env_id):
"""
close profile
:param env_id:
:return:
"""
headers = {
"X-API-KEY": API_KEY,
}
response = requests.patch(f"{BASE_URL}/v1/env/{env_id}/close", headers=headers)
return response.json()
if __name__ == '__main__':
resp = get_evn_list()
if resp['code'] != 0:
print(resp.msg)
exit(0)
# profile list
list = resp['data']['list']
if len(list) == 0:
print('no profile')
exit(0)
print(f"open profile:{list[0]['id']}")
open_resp = open_env(list[0]['id'])
if open_resp['code'] != 0:
print(open_resp['msg'])
exit(0)
print(f"open_resp : {open_resp}")
# Automatic
open_browser(open_resp['data']['debug_port'])
time.sleep(3)
# close profile
close_env(list[0]['id'])
1.4 java示例代码
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
import org.json.JSONObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
// 端口和API Key,替换为实际的值
private static final String BASE_URL = "http://127.0.0.1:52140/openapi";
private static final String API_KEY = "****";
public static void main(String[] args) {
JSONObject resp = getEvnList();
if (resp.getInt("code")!= 0) {
System.out.println(resp.getString("msg"));
System.exit(0);
}
JSONArray list = resp.getJSONObject("data").getJSONArray("list");
if (list.length() == 0) {
System.out.println("no profile");
System.exit(0);
}
System.out.println("open profile:" + list.getJSONObject(0).getString("id"));
JSONObject openResp = openEnv(list.getJSONObject(0).getString("id"));
if (openResp.getInt("code")!= 0) {
System.out.println(openResp.getString("msg"));
System.exit(0);
}
System.out.println("open_resp : " + openResp);
// 打开浏览器
WebDriver browser = openBrowser(openResp.getJSONObject("data").getString("debug_port"));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭环境
closeEnv(list.getJSONObject(0).getString("id"));
// 关闭浏览器
browser.quit();
}
public static JSONObject getEvnList() {
try {
URL url = new URL(BASE_URL + "/v1/env/list?page_no=1&page_size=20");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-API-KEY", API_KEY);
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder response = new StringBuilder();
while (scanner.hasNextLine()) {
response.append(scanner.nextLine());
}
scanner.close();
return new JSONObject(response.toString());
}
throw new RuntimeException("请求失败,状态码: " + responseCode);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static JSONObject openEnv(String envId) {
try {
URL url = new URL(BASE_URL + "/v1/env/" + envId + "/open");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-API-KEY", API_KEY);
connection.setRequestMethod("PATCH");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder response = new StringBuilder();
while (scanner.hasNextLine()) {
response.append(scanner.nextLine());
}
scanner.close();
return new JSONObject(response.toString());
}
throw new RuntimeException("请求失败,状态码: " + responseCode);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static JSONObject closeEnv(String envId) {
try {
URL url = new URL(BASE_URL + "/v1/env/" + envId + "/close");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-API-KEY", API_KEY);
connection.setRequestMethod("PATCH");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder response = new StringBuilder();
while (scanner.hasNextLine()) {
response.append(scanner.nextLine());
}
scanner.close();
return new JSONObject(response.toString());
}
throw new RuntimeException("请求失败,状态码: " + responseCode);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static WebDriver openBrowser(String port) {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("debuggerAddress", "127.0.0.1:" + port);
return new ChromeDriver(chromeOptions);
}
}