Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support markdown and attachments #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pyconfluence/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
22 changes: 13 additions & 9 deletions pyconfluence/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,37 @@ 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
will add the base_url to this, so 'url' should be something like '/ips'.
"""
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'}
Expand All @@ -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)

Expand Down