check if page exists -confluence

136 views Asked by At

I am trying to create a python script that takes part of code from confluence.py (https://github.com/atlassian-api/atlassian-python-api/blob/master/atlassian/confluence.py#L105)

Specifically, I am trying to recreate in a script from scratch the function "page_exists" but in general I would like to create my own code from a page from scratch.

However, I am getting several errors. Such us an error on the variable self 'not defined' on the line:

response = self.get(url, params=params)

In your opnion, which is the best way to recreate this function? My goal is to be able in future to customize such kind of confluence functions based on my own requirements.

Below my code:

from atlassian import Confluence

f = open('log.txt','w')
print('file open', file=f) # Python 3.x



confluence = Confluence(
    url='https://site_name.atlassian.net',
    username='email',
    password='Token_API',
    cloud=True)
    

print('connetion established', file=f) # Python 3.x

        
space='TEAM'
title="new page"


print('variables defined', file=f) # Python 3.x

url = "rest/api/content"
params = {}
if space is not None:
            params["spaceKey"] = str(space)
if title is not None:
            params["title"] = str(title)
if type is not None:
            params["type"] = str(type)

try:
            response = self.get(url, params=params)
except HTTPError as e:
            if e.response.status_code == 404:
                raise ApiPermissionError(
                    "The calling user does not have permission to view the content",
                    reason=e,
                )

            raise



print('bgin the if', file=f) # Python 3.x

if response.get("results"):
            print('page exists', file=f) # Python 3.x
else:
           print('page does not exist', file=f) # Python 3.x
            
            



1

There are 1 answers

0
supertosti On

I have been able to accomplish a positive outcome with the below:


from atlassian import Confluence




confluence = Confluence(
    url='https://site_name.atlassian.net',
    username='email',
    password='API_token',
    cloud=True)
 

f = open('log.txt','w') 

print('connetion established', file=f) # Python 3.x


        
space='TEAM'
title="new page"


print('variables defined', file=f) # Python 3.x

url = "rest/api/content"
params = {}
if space is not None:
            params["spaceKey"] = str(space)
if title is not None:
            params["title"] = str(title)
if type is not None:
            params["tipe"] = str(type)


            response = confluence.get(url, params=params)




if response.get("results"):
            print('page exists', file=f) # Python 3.x
else:
           print('page does not exist', file=f) # Python 3.x