-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfluence_api_client.py
More file actions
262 lines (231 loc) · 8.67 KB
/
confluence_api_client.py
File metadata and controls
262 lines (231 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""Confluence REST API 客户端封装。"""
from __future__ import annotations
import base64
from pathlib import Path
from typing import Any
import httpxyz
from pydantic import BaseModel, Field
DEFAULT_TIMEOUT_SECONDS = 30.0
class ConfluenceApiError(RuntimeError):
"""Confluence API 错误。"""
def __init__(
self,
message: str,
*,
status_code: int | None = None,
payload: Any | None = None,
) -> None:
super().__init__(message)
self.status_code = status_code
self.payload = payload
class ConfluenceConfig(BaseModel):
"""Confluence 连接配置。"""
base_url: str = Field(description="Confluence 基础地址。")
token: str = Field(description="API Token 或 PAT。")
username: str | None = Field(default=None, description="登录用户名或邮箱。")
timeout_seconds: float = Field(
default=DEFAULT_TIMEOUT_SECONDS,
gt=0,
description="请求超时时间(秒)。",
)
cloud: bool | None = Field(default=None, description="是否使用 Cloud 模式。")
verify_ssl: bool = Field(default=True, description="是否校验证书。")
class ConfluenceApiClient:
"""Confluence REST API 薄封装。"""
def __init__(self, config: ConfluenceConfig) -> None:
self.config = config
self.client = httpxyz.Client(
base_url=config.base_url.rstrip("/") + "/",
timeout=config.timeout_seconds,
verify=config.verify_ssl,
headers=self._build_headers(config),
)
@staticmethod
def _build_headers(config: ConfluenceConfig) -> dict[str, str]:
headers = {"Accept": "application/json"}
if config.username:
raw = f"{config.username}:{config.token}".encode("utf-8")
headers["Authorization"] = f"Basic {base64.b64encode(raw).decode('utf-8')}"
else:
headers["Authorization"] = f"Bearer {config.token}"
return headers
@staticmethod
def _raise_for_error(response: httpxyz.Response, context: str) -> None:
if response.is_success:
return
payload: Any
try:
payload = response.json()
except ValueError:
payload = response.text
raise ConfluenceApiError(
f"{context} failed with status {response.status_code}",
status_code=response.status_code,
payload=payload,
)
@staticmethod
def _encode_body(body: str, representation: str) -> dict[str, Any]:
return {representation: {"value": body, "representation": representation}}
def _get(self, path: str, *, params: dict[str, Any] | None = None) -> Any:
response = self.client.get(path, params=params)
self._raise_for_error(response, f"GET {path}")
return response.json()
def _post(
self,
path: str,
*,
json_data: dict[str, Any] | None = None,
files: Any | None = None,
headers: dict[str, str] | None = None,
) -> Any:
response = self.client.post(path, json=json_data, files=files, headers=headers)
self._raise_for_error(response, f"POST {path}")
return response.json()
def _put(
self,
path: str,
*,
json_data: dict[str, Any],
params: dict[str, Any] | None = None,
) -> Any:
response = self.client.put(path, json=json_data, params=params)
self._raise_for_error(response, f"PUT {path}")
return response.json()
def list_spaces(self, start: int = 0, limit: int = 25, expand: str | None = None) -> Any:
params: dict[str, Any] = {"start": start, "limit": limit}
if expand:
params["expand"] = expand
return self._get("rest/api/space", params=params)
def get_space(self, space_key: str, expand: str | None = None) -> Any:
params = {"expand": expand} if expand else None
return self._get(f"rest/api/space/{space_key}", params=params)
def get_page(self, page_id: str, expand: str | None = None) -> Any:
params = {"expand": expand} if expand else None
return self._get(f"rest/api/content/{page_id}", params=params)
def get_page_by_title(
self,
space_key: str,
title: str,
expand: str | None = None,
) -> Any:
params: dict[str, Any] = {"spaceKey": space_key, "title": title}
if expand:
params["expand"] = expand
return self._get("rest/api/content", params=params)
def get_page_children(
self,
page_id: str,
start: int = 0,
limit: int = 25,
expand: str | None = None,
) -> Any:
params: dict[str, Any] = {"start": start, "limit": limit}
if expand:
params["expand"] = expand
return self._get(f"rest/api/content/{page_id}/child/page", params=params)
def get_page_attachments(
self,
page_id: str,
start: int = 0,
limit: int = 25,
expand: str | None = None,
) -> Any:
params: dict[str, Any] = {"start": start, "limit": limit}
if expand:
params["expand"] = expand
return self._get(f"rest/api/content/{page_id}/child/attachment", params=params)
def create_page(
self,
space_key: str,
title: str,
body: str,
parent_id: str | None = None,
representation: str = "storage",
) -> Any:
data: dict[str, Any] = {
"type": "page",
"title": title,
"space": {"key": space_key},
"body": self._encode_body(body, representation),
}
if parent_id:
data["ancestors"] = [{"type": "page", "id": parent_id}]
return self._post("rest/api/content", json_data=data)
def update_page(
self,
page_id: str,
title: str,
body: str,
parent_id: str | None = None,
representation: str = "storage",
always_update: bool = False,
) -> Any:
current = self.get_page(page_id, expand="version")
version = current.get("version", {}).get("number")
if not isinstance(version, int):
raise ConfluenceApiError(f"Failed to resolve current page version for {page_id}")
data: dict[str, Any] = {
"id": page_id,
"type": "page",
"title": title,
"version": {"number": version + 1},
"body": self._encode_body(body, representation),
}
if parent_id:
data["ancestors"] = [{"type": "page", "id": parent_id}]
if always_update:
data["version"]["minorEdit"] = False
return self._put(f"rest/api/content/{page_id}", json_data=data, params={"status": "current"})
def attach_file(
self,
page_id: str,
file_path: str,
title: str | None = None,
comment: str | None = None,
) -> Any:
path = Path(file_path)
if not path.exists():
raise ConfluenceApiError(f"Attachment file not found: {file_path}")
filename = title or path.name
existing_attachment_id = self._find_attachment_id(page_id, filename)
with path.open("rb") as file_obj:
files = {
"file": (filename, file_obj, "application/octet-stream"),
"minorEdit": (None, "true"),
}
if comment:
files["comment"] = (None, comment)
target_path = (
f"rest/api/content/{page_id}/child/attachment/{existing_attachment_id}/data"
if existing_attachment_id
else f"rest/api/content/{page_id}/child/attachment"
)
return self._post(
target_path,
files=files,
headers={"X-Atlassian-Token": "no-check"},
)
def search_cql(
self,
cql: str,
start: int = 0,
limit: int = 25,
expand: str | None = None,
) -> Any:
params: dict[str, Any] = {"cql": cql, "start": start, "limit": limit}
if expand:
params["expand"] = expand
return self._get("rest/api/search", params=params)
def _find_attachment_id(self, page_id: str, filename: str) -> str | None:
payload = self.get_page_attachments(page_id, start=0, limit=200)
results = payload.get("results") if isinstance(payload, dict) else None
if not isinstance(results, list):
return None
for item in results:
if not isinstance(item, dict):
continue
if str(item.get("title", "")) == filename:
attachment_id = item.get("id")
if attachment_id is not None:
return str(attachment_id)
return None