API 参考
SDK 与代码示例
我们提供了多种编程语言的 SDK 和代码示例,帮助您快速集成文档解析服务。
Python SDK
安装
pip install requests基本用法
import requests
import time
class DocumentParser:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "http://8.148.69.123:8088/api"
def create_task(self, file_url, **options):
"""创建解析任务"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"url": file_url,
"is_ocr": options.get("is_ocr", True),
"enable_formula": options.get("enable_formula", True),
"enable_table": options.get("enable_table", True),
}
response = requests.post(
f"{self.base_url}/v4/extract/task",
headers=headers,
json=data
)
result = response.json()
if result["code"] == 0:
return result["data"]["task_id"]
else:
raise Exception(f"创建任务失败: {result['msg']}")
def get_result(self, task_id):
"""获取任务结果"""
headers = {
"Authorization": f"Bearer {self.api_key}"
}
response = requests.get(
f"{self.base_url}/v4/extract/task/{task_id}",
headers=headers
)
return response.json()["data"]
def wait_for_completion(self, task_id, max_wait=300):
"""等待任务完成"""
start_time = time.time()
while time.time() - start_time < max_wait:
result = self.get_result(task_id)
if result["state"] == "done":
return result
elif result["state"] == "failed":
raise Exception(f"解析失败: {result.get('err_msg')}")
time.sleep(5)
raise Exception("任务超时")
# 使用示例
parser = DocumentParser("YOUR_API_KEY")
# 创建任务
task_id = parser.create_task(
"https://example.com/document.pdf",
is_ocr=True,
enable_formula=True
)
print(f"任务 ID: {task_id}")
# 等待完成
result = parser.wait_for_completion(task_id)
print(f"解析完成!下载链接: {result['full_zip_url']}")Node.js SDK
安装
npm install axios基本用法
const axios = require('axios');
class DocumentParser {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseURL = 'http://8.148.69.123:8088/api';
}
async createTask(fileUrl, options = {}) {
try {
const response = await axios.post(
`${this.baseURL}/v4/extract/task`,
{
url: fileUrl,
is_ocr: options.isOcr || true,
enable_formula: options.enableFormula || true,
enable_table: options.enableTable || true,
},
{
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
}
);
if (response.data.code === 0) {
return response.data.data.task_id;
} else {
throw new Error(`创建任务失败: ${response.data.msg}`);
}
} catch (error) {
throw new Error(`API 请求失败: ${error.message}`);
}
}
async getResult(taskId) {
const response = await axios.get(
`${this.baseURL}/v4/extract/task/${taskId}`,
{
headers: {
'Authorization': `Bearer ${this.apiKey}`,
},
}
);
return response.data.data;
}
async waitForCompletion(taskId, maxWait = 300000) {
const startTime = Date.now();
while (Date.now() - startTime < maxWait) {
const result = await this.getResult(taskId);
if (result.state === 'done') {
return result;
} else if (result.state === 'failed') {
throw new Error(`解析失败: ${result.err_msg}`);
}
await new Promise(resolve => setTimeout(resolve, 5000));
}
throw new Error('任务超时');
}
}
// 使用示例
(async () => {
const parser = new DocumentParser('YOUR_API_KEY');
try {
// 创建任务
const taskId = await parser.createTask(
'https://example.com/document.pdf',
{
isOcr: true,
enableFormula: true,
}
);
console.log(`任务 ID: ${taskId}`);
// 等待完成
const result = await parser.waitForCompletion(taskId);
console.log(`解析完成!下载链接: ${result.full_zip_url}`);
} catch (error) {
console.error('错误:', error.message);
}
})();Java SDK
import java.net.http.*;
import java.net.URI;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class DocumentParser {
private String apiKey;
private String baseUrl = "http://8.148.69.123:8088/api";
private HttpClient client;
private Gson gson;
public DocumentParser(String apiKey) {
this.apiKey = apiKey;
this.client = HttpClient.newHttpClient();
this.gson = new Gson();
}
public String createTask(String fileUrl, boolean isOcr, boolean enableFormula) throws Exception {
JsonObject requestBody = new JsonObject();
requestBody.addProperty("url", fileUrl);
requestBody.addProperty("is_ocr", isOcr);
requestBody.addProperty("enable_formula", enableFormula);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/v4/extract/task"))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(gson.toJson(requestBody)))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JsonObject result = gson.fromJson(response.body(), JsonObject.class);
if (result.get("code").getAsInt() == 0) {
return result.getAsJsonObject("data").get("task_id").getAsString();
} else {
throw new Exception("创建任务失败: " + result.get("msg").getAsString());
}
}
// 使用示例
public static void main(String[] args) {
try {
DocumentParser parser = new DocumentParser("YOUR_API_KEY");
String taskId = parser.createTask("https://example.com/document.pdf", true, true);
System.out.println("任务 ID: " + taskId);
} catch (Exception e) {
e.printStackTrace();
}
}
}