diff --git a/pyconfluence/actions.py b/pyconfluence/actions.py index 8022b22..cf25c24 100644 --- a/pyconfluence/actions.py +++ b/pyconfluence/actions.py @@ -243,6 +243,21 @@ def create_page(name, parent_id, space, content): data["body"] = {"storage": {"value": content, "representation": "storage"}} return _api.rest("/", "POST", _json.dumps(data)) +def attach_file(name, id, content, comment=None): + """Create attachment in Confluence. + Parameters: + - name of the attachment + - id: ID of the page to attach to + - content: conent of the attachment as io.BytesIO + - comment: comment for attachment (optional) + """ + data = { + "comment" : name if comment == None else comment + } + files = { + 'file' : (name, content) + } + return _api.rest(f"/{id}/child/attachment", "POST", data, files) def edit_page(id, name, space, content): """Update a page in Confluence. @@ -292,3 +307,14 @@ def delete_page_full(id): delete_page_full(i["id"]) return delete_page(id) + +def markdown2xhtml(content): + """Convert Markdown to xhtml + Parameters: + - content: the contnent to covert + """ + data = { + "value" : content, + "representation":"wiki", + } + return _api.rest("/../contentbody/convert/storage", "POST", _json.dumps(data)) \ No newline at end of file diff --git a/pyconfluence/api.py b/pyconfluence/api.py index 57ba315..2438f65 100644 --- a/pyconfluence/api.py +++ b/pyconfluence/api.py @@ -35,8 +35,7 @@ def load_variables(): base_url = ("https://" + os.environ["PYCONFLUENCE_ORG"] + ".atlassian" ".net/wiki/rest/api/content") - -def rest(url, req="GET", data=None): +def rest(url, req="GET", data=None, files=None): """Main function to be called from this module. send a request using method 'req' and to the url. the _rest() function @@ -44,27 +43,29 @@ def rest(url, req="GET", data=None): """ load_variables() - return _rest(base_url + url, req, data) + return _rest(base_url + url, req, data, files) -def _rest(url, req, data=None): +def _rest(url, req, data=None, files=None): """Send a rest rest request to the server.""" - if url.upper().startswith("HTTPS"): + if not url.upper().startswith("HTTPS"): print("Secure connection required: Please use HTTPS or https") return "" + if "../" in url: + url = url.replace("https://","") + url = "https://" + os.path.normpath(url) req = req.upper() if req != "GET" and req != "PUT" and req != "POST" and req != "DELETE": return "" - status, body = _api_action(url, req, data) + status, body = _api_action(url, req, data, files) if (int(status) >= 200 and int(status) <= 226): return body else: return body - -def _api_action(url, req, data=None): +def _api_action(url, req, data=None, files=None): """Take action based on what kind of request is needed.""" requisite_headers = {'Accept': 'application/json', 'Content-Type': 'application/json'} @@ -76,8 +77,11 @@ def _api_action(url, req, data=None): response = requests.put(url, headers=requisite_headers, auth=auth, data=data) elif req == "POST": + if files != None: + requisite_headers.pop('Content-Type',None) + requisite_headers["X-Atlassian-Token"] = "no-check" response = requests.post(url, headers=requisite_headers, auth=auth, - data=data) + data=data, files=files) elif req == "DELETE": response = requests.delete(url, headers=requisite_headers, auth=auth)