I am using the requests.put() method within my script to automatically update a webpage. The problem being that the automation script is not completely automated as it should be. Let me show you the snippet of the code which is causing the issue:
import requests
import json
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup
headers = {
'Content-Type': 'application/json',
}
# Just a string payload being extracted from previous lines of code not shown here
pass_string = str(soup).replace('\"', '\\"')
data = '{"id":"525424594","type":"page", "title":"Update status","space":{"key":"CSSAI"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":44}}'
response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data,
auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
So, in the JSON string called data we have the the key called "version":{"number":44} which is used to update the webpage such that we do not have any conflicts regarding page versions. And it should be updated every time the contents of the webpage changes. There are two cases which will change the "version" of the webpage:
For case 1 I can have a .txt file which will record the previous version of the webpage such that each time I execute the script I can read from .txt file the previous version and the version automatically increments it by 1 within script, writes that version to the .txt file, and executes the command using the incremented version. But for case 2 I would not know if someone had changed the version in the webpage itself and so it would be hard to know the current version of that webpage to increment to. Any ideas on how I can solve this issue?
After pondering some more I found the required solution. If anyone has a better solution please post it here.
import requests
import json
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup
headers = {
'Content-Type': 'application/json',
}
# Just a string payload being extracted from previous lines of code not shown here
pass_string = str(soup).replace('\"', '\\"')
data = '{"id":"525424594","type":"page", "title":"Update status","space":{"key":"CSSAI"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":2}}'
response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data,
auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
if response.json()["statusCode"] == 409:
error_message = "Version must be incremented on update. Current version is: "
if error_message in response.json()["message"]:
current_version = response.json()["message"].split(error_message)[1]
version_num = int(current_version) + 1
data = '{"id":"525424594","type":"page", "title":"Update Status","space":{"key":"CSSAI"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":' + str(
version_num) + '}}'
response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data,
auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
else:
print(response.json())
sys.exit(1)
Basically I am gathering the current version number from the failed response and incrementing it by one to send the new request. However, this does mean that our first request will always be a failed request.