69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
Confluence API 客户端模块
|
||
|
|
"""
|
||
|
|
import requests
|
||
|
|
from typing import Optional
|
||
|
|
|
||
|
|
|
||
|
|
class ConfluenceClient:
|
||
|
|
"""Confluence REST API 客户端"""
|
||
|
|
|
||
|
|
def __init__(self, base_url: str, token: str):
|
||
|
|
"""
|
||
|
|
初始化客户端
|
||
|
|
|
||
|
|
参数:
|
||
|
|
base_url: Confluence API 基础URL (不包含 /content)
|
||
|
|
token: Bearer 认证令牌
|
||
|
|
"""
|
||
|
|
self.base_url = base_url.rstrip('/')
|
||
|
|
self.headers = {
|
||
|
|
'Authorization': f'Bearer {token}',
|
||
|
|
'Accept': 'application/json'
|
||
|
|
}
|
||
|
|
|
||
|
|
def fetch_content(self, content_id: str, expand: str = 'body.storage') -> dict:
|
||
|
|
"""
|
||
|
|
获取页面内容
|
||
|
|
|
||
|
|
参数:
|
||
|
|
content_id: 页面ID
|
||
|
|
expand: 展开字段
|
||
|
|
|
||
|
|
返回:
|
||
|
|
API 响应数据
|
||
|
|
"""
|
||
|
|
url = f'{self.base_url}/content/{content_id}'
|
||
|
|
params = {'expand': expand}
|
||
|
|
|
||
|
|
response = requests.get(url, headers=self.headers, params=params, timeout=30)
|
||
|
|
response.raise_for_status()
|
||
|
|
return response.json()
|
||
|
|
|
||
|
|
def get_html(self, content_id: str) -> str:
|
||
|
|
"""
|
||
|
|
获取页面HTML内容
|
||
|
|
|
||
|
|
参数:
|
||
|
|
content_id: 页面ID
|
||
|
|
|
||
|
|
返回:
|
||
|
|
HTML 字符串
|
||
|
|
"""
|
||
|
|
data = self.fetch_content(content_id)
|
||
|
|
return data.get('body', {}).get('storage', {}).get('value', '')
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
# 使用示例
|
||
|
|
import os
|
||
|
|
|
||
|
|
client = ConfluenceClient(
|
||
|
|
base_url='https://confluence.westwell-lab.com/rest/api',
|
||
|
|
token=os.getenv('CONFLUENCE_TOKEN', '')
|
||
|
|
)
|
||
|
|
|
||
|
|
html = client.get_html('155764524')
|
||
|
|
print(f'获取到 {len(html)} 字符的HTML内容')
|