atlassian-api / atlassian-python-api

Atlassian Python REST API wrapper
https://atlassian-python-api.readthedocs.io
Apache License 2.0
1.34k stars 662 forks source link

[Improvement] Confluence `update_or_create()` #1060

Open y0urself opened 1 year ago

y0urself commented 1 year ago

It seems, that the argument parent_id https://github.com/atlassian-api/atlassian-python-api/blob/b24f7dbab2d6b6892e8a29543de5cb52c2820c6a/atlassian/confluence.py#L1704 could be optional, since if it is not passed or with None, it will be requested in https://github.com/atlassian-api/atlassian-python-api/blob/b24f7dbab2d6b6892e8a29543de5cb52c2820c6a/atlassian/confluence.py#L1729 anyways.

gonchik commented 1 year ago

Hi @y0urself , you're right. if you don't mind we can adjust it.

stdedos commented 1 week ago

It is very "unorthodox" to have to "re-implement the method", if one wants to update_or_create() a page that is "top-level" inside its space.

Or maybe I have misread the function, but at least

In [10]: c.update_or_create(c.get_space("~624d930b699649006aeabe6a")["id"], "title", "body")
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
File .venv/lib/python3.9/site-packages/atlassian/confluence.py:347, in Confluence.get_page_by_id(self, page_id, expand, status, version)
    346 try:
--> 347     response = self.get(url, params=params)
    348 except HTTPError as e:

File .venv/lib/python3.9/site-packages/atlassian/rest_client.py:341, in AtlassianRestAPI.get(self, path, data, flags, params, headers, not_json_response, trailing, absolute, advanced_mode)
    328 """
    329 Get request based on the python-requests module. You can override headers, and also, get not json response
    330 :param path:
   (...)
    339 :return:
    340 """
--> 341 response = self.request(
    342     "GET",
    343     path=path,
    344     flags=flags,
    345     params=params,
    346     data=data,
    347     headers=headers,
    348     trailing=trailing,
    349     absolute=absolute,
    350     advanced_mode=advanced_mode,
    351 )
    352 if self.advanced_mode or advanced_mode:

File .venv/lib/python3.9/site-packages/atlassian/rest_client.py:313, in AtlassianRestAPI.request(self, method, path, data, json, flags, params, headers, files, trailing, absolute, advanced_mode)
    311     return response
--> 313 self.raise_for_status(response)
    314 return response

File .venv/lib/python3.9/site-packages/atlassian/confluence.py:3343, in Confluence.raise_for_status(self, response)
   3342 else:
-> 3343     raise HTTPError(error_msg, response=response)

HTTPError: com.atlassian.confluence.api.service.exceptions.NotFoundException: No content found with id : 2467824518

During handling of the above exception, another exception occurred:

ApiError                                  Traceback (most recent call last)
Cell In[10], line 1
----> 1 c.update_or_create(c.get_space("~624d930b699649006aeabe6a")["id"], "mds-to-confluence", "body")

File .venv/lib/python3.9/site-packages/atlassian/confluence.py:1946, in Confluence.update_or_create(self, parent_id, title, body, representation, minor_edit, version_comment, editor, full_width)
   1923 def update_or_create(
   1924     self,
   1925     parent_id,
   (...)
   1932     full_width=False,
   1933 ):
   1934     """
   1935     Update page or create a page if it is not exists
   1936     :param parent_id:
   (...)
   1944     :return:
   1945     """
-> 1946     space = self.get_page_space(parent_id)
   1948     if self.page_exists(space, title):
   1949         page_id = self.get_page_id(space, title)

File .venv/lib/python3.9/site-packages/atlassian/confluence.py:259, in Confluence.get_page_space(self, page_id)
    253 def get_page_space(self, page_id):
    254     """
    255     Provide space key from content id.
    256     :param page_id: content ID
    257     :return:
    258     """
--> 259     return ((self.get_page_by_id(page_id, expand="space") or {}).get("space") or {}).get("key") or None

File .venv/lib/python3.9/site-packages/atlassian/confluence.py:351, in Confluence.get_page_by_id(self, page_id, expand, status, version)
    348 except HTTPError as e:
    349     if e.response.status_code == 404:
    350         # Raise ApiError as the documented reason is ambiguous
--> 351         raise ApiError(
    352             "There is no content with the given id, "
    353             "or the calling user does not have permission to view the content",
    354             reason=e,
    355         )
    357     raise
    359 return response

ApiError: There is no content with the given id, or the calling user does not have permission to view the content

won't work