-
Notifications
You must be signed in to change notification settings - Fork 0
/
confluence.py
154 lines (118 loc) · 4.41 KB
/
confluence.py
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
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
REQUEST_TIMEOUT_IN_SECS = 10
class Confluence:
'''A class holds many methods which send HTTP requests to Confluence and return the response.
Args:
base_url (str): Confluence Base URL
account (str, str): Confluence Account(ID, PW)
Attributes:
_api_base_url (str) : Confluence REST API Base URL (concat with base_url argument)
_session (requests.Session): Authenticaed Confluence Sesison using HTTP Basic Authentication
'''
def __init__(self, base_url: str, account: tuple[str, str]):
self._api_base_url = base_url + '/rest/api'
self._session = requests.Session()
self._session.auth = account
def get_pages(self, space_key: str) -> list:
'''Get all Confluence pages in the given space
Args:
space_key (str): Space's key where pages belong
Returns:
list: Pages
'''
results = []
start_index = 0
while True:
params = {
'spaceKey' : space_key,
'type': 'page',
'limit': 25,
'start': start_index
}
response = self._session.get(
f'{self._api_base_url}/content',
params=params,
verify=False,
timeout=REQUEST_TIMEOUT_IN_SECS).json()
if response['size'] == 0:
break
results.extend(list(response['results']))
start_index = int(response['start']) + int(response['size'])
return results
def get_content(self, content_id: str) -> dict:
'''Get a Confluence content bt the given Content ID
Args:
content_id (str): Content's ID to fetch
Returns:
dict: Content
'''
return self._session.get(
f'{self._api_base_url}/content/{content_id}',
verify=False,
timeout=REQUEST_TIMEOUT_IN_SECS).json()
def create_page(self,
space_key: str,
title: str,
body: str,
parent_page_id: str = None) -> dict:
'''Create a new Confluence page
Args:
space_key (str): Space's key where page will be created
title (str): Page title (must UNIQUE in a space)
body (str): Storage Formatted Body
parent_page_id (str): (Optional) Parent page's ID
(new page will be child of the parent page)
Returns:
dict: Newly created content
'''
req_data = {
'space': { 'key': space_key },
'type': 'page',
'title': title,
'body': {
'storage': {
'value': body,
'representation': 'storage'
}
}
}
if parent_page_id:
req_data['ancestors'] = []
req_data['ancestors'].append({ 'id': parent_page_id })
return self._session.post(
f'{self._api_base_url}/content',
json=req_data,
verify=False,
timeout=REQUEST_TIMEOUT_IN_SECS).json()
def update_page(self, page_content_id: str, new_body: str, new_title: str = None) -> dict:
'''Update a page content only by the given page id
Args:
page_content_id (str): Page to update
new_body (str): New Storage Formatterd Body
new_title (str): (Optional) New title (to keep the original title, then pass None)
Returns:
dict: Updated content
'''
ori_content = self.get_content(content_id=page_content_id)
title = title if new_title else ori_content['title']
ori_version = int(ori_content['version']['number'])
req_data = {
'type': 'page',
'title': title,
'version': {
'number': ori_version + 1
},
'body': {
'storage': {
'value': new_body,
'representation': 'storage'
}
}
}
return self._session.put(
f'{self._api_base_url}/content/{page_content_id}',
json=req_data,
verify=False,
timeout=REQUEST_TIMEOUT_IN_SECS).json()