How to work with REST API Confluence.
What is this topic about?
- API structure in Atlassian Confluence.
- Objects, files, relations etc.
- How to compose API request in Atlassian Confluence?
- How to open session and hold it on – !
- Making headers with requests and – json !
- Send a file or file content with wiki markup on Confluence – !
- Interpret server answers.
- What is the difference between “representation“:”storage” and “markup” in Atlassian Confluence?
- Macro and markup
About “Atlassian Confluence” you can read here.
- https://www.atlassian.com/software/confluence
- https://wikipedia.org/wiki/Confluence
Who need this?
For those, who wants to automate documentation stage of DEV.
For those, who wants to understand how does python requests working?
For my own – to not forger this lesson.
When I started to make this bot – I know nothing, just like a John Snow, I also know nothing about requests and headers, and also I know nothing about this Confluence REST and how struggling can it be. You can find here how to make a working header for webserver with python and requests, how to use json, how to keep session in memory (useful when you can’t just login in every web request) also you can find here how to use Confluence formatting and how to make it better (difference between different mode).
This topic will consist of some parts, I’ll post them as soon as I have a time to compose them.
Topics available:
- Atlassian Confluence REST API Logic structure (Eng)
- Atlassian Confluence REST API Making request and keep the session (Eng)
- Atlassian Confluence REST API Wiki markup (Eng) – planned
RAW CODE:
But if you do not want to wait, or if you can understand everything just on code example, you can see my code below.
Note: this is not a properly working version, this is just a draft, yes, it works for me, and may work for you, but this is a draft with all mess in it.
import json import requests # Config HTTP session # request 0 confluence_auth_url = 'https://docs.companyname.com/docs/dashboard.action?os_username=SERVICE_USERNAME&os_password=SERVICE_PASSWORD' session = requests.session() session.post(confluence_auth_url) # Configipedia stats space_page = '625685389' # DocBot SPACE "title":"DocBot space", "space":{"id":624689178,"key":"~SERVICE_USERNAME","name":"Configipedia Automation","type":"global","_links":{"self":"https://docs.companyname.com/docs/rest/api/space/~SERVICE_USERNAME"} space_root_page = '625685397' # DocBot HOMEPADE "title":"Space root page (Parent 1)", "space":{"id":624689178,"key":"~SERVICE_USERNAME","name":"Configipedia Automation","type":"global","_links":{"self":"https://docs.companyname.com/docs/rest/api/space/~SERVICE_USERNAME"} space_id = '624689178' space_key = '~SERVICE_USERNAME' # {"id":624689178,"key":"~SERVICE_USERNAME","name":"Configipedia Automation" space_name = 'DocBot+space' child_page_1 = '625685424' child_page_2 = '625685428' confPedia_root = 'https://docs.companyname.com/docs' confPediaUser = '~SERVICE_USERNAME' # api keys # Usage example: "confPedia_root+conf_content+space_id" - request to get space homepage content api = '/rest/api/' conf_space = '/rest/api/space/' conf_content = '/rest/api/content/' conf_children = '/child/page/' # Usual URLS space_url = confPedia_root+conf_content+space_page space_child_items = confPedia_root+conf_content+space_root_page+conf_children space_post = confPedia_root+conf_content # GET HOMESPACE # request 1 r_homepage = session.get(space_url) r_homepage = r_homepage.json() # Check current location: r_homepage_loc = r_homepage['_links']['webui'] print("You are here now: "+confPedia_root+r_homepage_loc) # GET CHILDREN ITEMS # request 2 r_home_children = session.get(space_child_items) r_home_children = r_home_children.json() # Check current location r_home_children_loc = r_home_children['_links']['self'] print("You are looking through this branch: "+r_home_children_loc) # Getting results from "request 2" r_home_children_results = r_home_children['results'] product_pages_list = [] for item in r_home_children_results: id = item['id'] title = item['title'] web_link = item['_links']['webui'] post_link = item['_links']['self'] # product_page = {'id':id, 'title':title, 'web_link':web_link, 'post_link':post_link} product_page = {'id':id, 'title':title, 'web':confPedia_root+web_link, 'post':post_link} product_pages_list.append(product_page) print(product_page) files = open('THIS_IS_FILE_WHERE_WIKI_MARKUP.txt', 'r') file = files.read() file_string = str(file) file_string_repl = file_string.replace("\\r\\n", "<br/>") contentType = 'application/json' userAgent = 'DocBot/0.0.1' acceptType = 'application/json' header = {"Content-type":contentType} title = 'DOC NAME TITLE' post_object = {"type":"page","title":title,"space":{"key":space_key},"body":{"storage":{"value":file_string_repl,"representation":"storage"}}} data=json.dumps(post_object) # print(file_string) # print(data) # POST SOMETHING ON PAGE # request 3 print("POST something to: "+space_post) r_post_homepage = session.post(space_post, headers=header, data=data) r_post_homepage_answer = r_post_homepage.text print(r_post_homepage_answer)