Atlassian Confluence REST API Making request and keep the session (Eng)

In this topic I’ll show you how to make session to Atlassian Confluence with python requests and keep it in memory.

Atlassian Confluence

Based on:

For someone who found this topic by “search machines”, there is some more detailed how-to – probably can help you:

Python requests module:

At first you should know and understand how requests works, basically.

Check how your confluence works:

Next – you should know the point, where is your Confluence server make authorisation, in my situation it lies here (this is looks cozy, but this is our company’s way. In your situation it can be simpler, as documented in links above):

https://docs.YOURCOMPANYMAME.com/docs/dashboard.action?os_username=configipedia_service_account&os_password=PASSWORD

Then you compose a request like:

import json
import requests

confluence_auth_url = 'https://docs.YOURCOMPANYMAME.com/docs/dashboard.action?os_username=configipedia_service_account&os_password=PASSWORD'
session = requests.session()
session.post(confluence_auth_url)

This will execute and save the session in memory, so you will not need to authorise each next request.

REST URL composing:

Then you compose the needed URL like it shown here: http://www.trianglesis.org.ua/atlassian-confluence-rest-api-logic-structure-eng

# 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

This will be useful to create this (in KB):

Create content
POST /rest/api/content
Get content
GET /rest/api/content
Update
PUT /rest/api/content/{contentId}
Get content by id
GET /rest/api/content/{id}
Delete
DELETE /rest/api/content/{id}

Session execute:

When you have all needed arguments and space id and parent-child pages of Atlassian Confluence, you can simply add them as like it shown in example above and execute:

In this example you pick up the session stored in memory and just send any needed URL request, then json decode the answer and shows the result. You can do it as many times as you need, the session is open and ready to get and post.

Sometimes its better, than send credentials each time when you GET\POST something.

# 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)

This example will show all spaces for current user in Atlassian Confluence, then show all pages on it, and all child items with theirs id, title, web link etc.

 

About trianglesis

Александр Брюндтзвельт - гений, филантроп, 100 гривен в кармане. Этот блог - "сток" моих мыслей и заметок. Достаточно одного взгляда на него, чтобы понять, что такой же бардак творится у меня в голове. Если вам этот бардак интересен - милости прошу.
Bookmark the permalink.