Code Monkey home page Code Monkey logo

Comments (3)

karthikpanu avatar karthikpanu commented on July 24, 2024

Saw people reporting this issue but haven't found a resolution

from atlassian-python-api.

gonchik avatar gonchik commented on July 24, 2024

Let me check that end point on cloud

from atlassian-python-api.

maconfr avatar maconfr commented on July 24, 2024

The endpoint has changed from /services/api/v1/task/<TASK_ID>/progress to /rest/api/longtask/<TASK_ID>.
See API doc: Get long-running task

I solved this in my code by sub-classing the Confluence class and overwriting the two methods Confluence.get_page_as_pdf() and Confluence.get_pdf_download_url_for_confluence_cloud().

I just copied the original method bodies and ajusted the code.

In Confluence.get_page_as_pdf():

@@ -55,7 +54,7 @@ class CustomConfluence(Confluence):
         url = "spaces/flyingpdf/pdfpageexport.action?pageId={pageId}".format(
             pageId=page_id
         )
-        if self.api_version == "cloud":
+        if self.cloud:
             url = self.get_pdf_download_url_for_confluence_cloud(url)
             if not url:
                 log.error("Failed to get download PDF url.")
@@ -63,8 +62,6 @@ class CustomConfluence(Confluence):
                     "Failed to export page as PDF",
                     reason="Failed to get download PDF url.",
                 )
-            # To download the PDF file, the request should be with no headers of authentications.
-            return requests.get(url, timeout=75).content
         return self.get(url, headers=headers, not_json_response=True)

In Confluence.get_pdf_download_url_for_confluence_cloud():

     def get_pdf_download_url_for_confluence_cloud(self, url):
@@ -86,27 +83,38 @@ class CustomConfluence(Confluence):
             task_id = response_string.split('name="ajs-taskId" content="')[1].split(
                 '">'
             )[0]
-            poll_url = "/services/api/v1/task/{0}/progress".format(task_id)
+            poll_url = "/rest/api/longtask/{0}".format(task_id)
+            task_timeout = time.monotonic() + 180
             while running_task:
                 log.info("Check if export task has completed.")
                 progress_response = self.get(poll_url)
-                percentage_complete = int(progress_response.get("progress", 0))
-                task_state = progress_response.get("state")
-                if task_state == "FAILED":
-                    log.error("PDF conversion not successful.")
+                percentage_complete = progress_response["percentageComplete"]
+                if task_timeout < time.monotonic():
+                    raise Exception(
+                        f"Timeout exceeded while waiting for task '{task_id}'."
+                        f" Progress: {percentage_complete}%."
+                    )
+
+                task_successful = progress_response["successful"]
+                task_finished = progress_response["finished"]
+                task_messages = [
+                    msg["translation"] for msg in progress_response["messages"]
+                ]
+                if task_finished and not task_successful:
+                    log.error("PDF conversion not successful. %r", task_messages)
                     return None
                 elif percentage_complete == 100:
                     running_task = False
-                    log.info(
-                        "Task completed - {task_state}".format(task_state=task_state)
-                    )
+                    log.info("Task completed - successful")
                     log.debug("Extract task results to download PDF.")
-                    task_result_url = progress_response.get("result")
+                    download_url = (
+                        task_messages[0].split(' href="', 1)[1].split('"', 1)[0]
+                    )
+
                 else:
                     log.info(
-                        "{percentage_complete}% - {task_state}".format(
+                        "{percentage_complete}% complete".format(
                             percentage_complete=percentage_complete,
-                            task_state=task_state,
                         )
                     )
                     time.sleep(3)
@@ -114,8 +122,7 @@ class CustomConfluence(Confluence):
                 "Task successfully done, querying the task result for the download url"
             )
             # task result url starts with /wiki, remove it.
-            task_content = self.get(task_result_url[5:], not_json_response=True)
-            download_url = task_content.decode(encoding="utf-8", errors="strict")
+            download_url = download_url[5:]
             log.debug("Successfully got the download url")
             return download_url
         except IndexError as e:

from atlassian-python-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.