Skip to content

Commit b08a969

Browse files
committed
Deduplicate method
1 parent aec6608 commit b08a969

File tree

3 files changed

+29
-56
lines changed

3 files changed

+29
-56
lines changed

atlassian/confluence/base.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import copy
44
import logging
5-
5+
from requests import HTTPError
66
from ..rest_client import AtlassianRestAPI
77

88
log = logging.getLogger(__name__)
@@ -157,3 +157,31 @@ def _get_paged(
157157
trailing = False
158158

159159
return
160+
161+
def raise_for_status(self, response):
162+
"""
163+
Checks the response for errors and throws an exception if return code >= 400
164+
165+
Implementation for Confluence Server according to
166+
https://developer.atlassian.com/server/confluence/rest/v1002/intro/#about
167+
Implementation for Confluence Cloud according to
168+
https://developer.atlassian.com/cloud/confluence/rest/v2/intro/#about
169+
:param response:
170+
:return:
171+
"""
172+
if 400 <= response.status_code < 600:
173+
try:
174+
j = response.json()
175+
if "message" in j:
176+
error_msg = j["message"]
177+
if "detail" in j:
178+
error_msg = f"{error_msg}\n{str(j['detail'])}"
179+
else:
180+
error_msg = f"HTTP {response.status_code}: {response.reason}"
181+
except Exception as e:
182+
log.error(e)
183+
response.raise_for_status()
184+
else:
185+
raise HTTPError(error_msg, response=response)
186+
else:
187+
response.raise_for_status()

atlassian/confluence/cloud/base.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding=utf-8
22

33
import logging
4-
from requests import HTTPError
54

65
from ..base import ConfluenceBase
76

@@ -25,33 +24,6 @@ def __init__(self, url, *args, **kwargs):
2524
"""
2625
super(ConfluenceCloudBase, self).__init__(url, *args, **kwargs)
2726

28-
def raise_for_status(self, response):
29-
"""
30-
Checks the response for errors and throws an exception if return code >= 400
31-
32-
Implementation for Confluence Cloud according to
33-
https://developer.atlassian.com/cloud/confluence/rest/v2/intro/#about
34-
35-
:param response:
36-
:return:
37-
"""
38-
if 400 <= response.status_code < 600:
39-
try:
40-
j = response.json()
41-
if "message" in j:
42-
error_msg = j["message"]
43-
if "detail" in j:
44-
error_msg = f"{error_msg}\n{str(j['detail'])}"
45-
else:
46-
error_msg = f"HTTP {response.status_code}: {response.reason}"
47-
except Exception as e:
48-
log.error(e)
49-
response.raise_for_status()
50-
else:
51-
raise HTTPError(error_msg, response=response)
52-
else:
53-
response.raise_for_status()
54-
5527
def _get_paged(
5628
self,
5729
url,

atlassian/confluence/server/base.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding=utf-8
22

33
import logging
4-
from requests import HTTPError
54

65
from ..base import ConfluenceBase
76

@@ -25,32 +24,6 @@ def __init__(self, url, *args, **kwargs):
2524
"""
2625
super(ConfluenceServerBase, self).__init__(url, *args, **kwargs)
2726

28-
def raise_for_status(self, response):
29-
"""
30-
Checks the response for errors and throws an exception if return code >= 400
31-
32-
Implementation for Confluence Server according to
33-
https://developer.atlassian.com/server/confluence/rest/v1002/intro/#about
34-
35-
:param response:
36-
:return:
37-
"""
38-
if 400 <= response.status_code < 600:
39-
try:
40-
j = response.json()
41-
if "message" in j:
42-
error_msg = j["message"]
43-
if "detail" in j:
44-
error_msg = f"{error_msg}\n{str(j['detail'])}"
45-
else:
46-
error_msg = f"HTTP {response.status_code}: {response.reason}"
47-
except Exception as e:
48-
log.error(e)
49-
response.raise_for_status()
50-
else:
51-
raise HTTPError(error_msg, response=response)
52-
else:
53-
response.raise_for_status()
5427

5528
def _get_paged(
5629
self,

0 commit comments

Comments
 (0)