|
5 | 5 | import json
|
6 | 6 |
|
7 | 7 | from requests import HTTPError
|
| 8 | +import requests |
8 | 9 | from deprecated import deprecated
|
9 | 10 | from atlassian import utils
|
10 | 11 | from .errors import ApiError, ApiNotFoundError, ApiPermissionError, ApiValueError, ApiConflictError, ApiNotAcceptable
|
@@ -2433,7 +2434,11 @@ def get_page_as_pdf(self, page_id):
|
2433 | 2434 | url = "spaces/flyingpdf/pdfpageexport.action?pageId={pageId}".format(pageId=page_id)
|
2434 | 2435 | if self.api_version == "cloud":
|
2435 | 2436 | url = self.get_pdf_download_url_for_confluence_cloud(url)
|
2436 |
| - |
| 2437 | + if not url: |
| 2438 | + log.error("Failed to get download PDF url.") |
| 2439 | + raise ApiNotFoundError("Failed to export page as PDF", reason="Failed to get download PDF url.") |
| 2440 | + # To download the PDF file, the request should be with no headers of authentications. |
| 2441 | + return requests.get(url, timeout=75).content |
2437 | 2442 | return self.get(url, headers=headers, not_json_response=True)
|
2438 | 2443 |
|
2439 | 2444 | def get_page_as_word(self, page_id):
|
@@ -2669,45 +2674,40 @@ def get_pdf_download_url_for_confluence_cloud(self, url):
|
2669 | 2674 | :param url: URL to initiate PDF export
|
2670 | 2675 | :return: Download url for PDF file
|
2671 | 2676 | """
|
2672 |
| - download_url = None |
2673 | 2677 | try:
|
2674 |
| - long_running_task = True |
| 2678 | + running_task = True |
2675 | 2679 | headers = self.form_token_headers
|
2676 | 2680 | log.info("Initiate PDF export from Confluence Cloud")
|
2677 | 2681 | response = self.get(url, headers=headers, not_json_response=True)
|
2678 | 2682 | response_string = response.decode(encoding="utf-8", errors="strict")
|
2679 | 2683 | task_id = response_string.split('name="ajs-taskId" content="')[1].split('">')[0]
|
2680 |
| - poll_url = "runningtaskxml.action?taskId={0}".format(task_id) |
2681 |
| - while long_running_task: |
2682 |
| - long_running_task_response = self.get(poll_url, headers=headers, not_json_response=True) |
2683 |
| - long_running_task_response_parts = long_running_task_response.decode( |
2684 |
| - encoding="utf-8", errors="strict" |
2685 |
| - ).split("\n") |
2686 |
| - percentage_complete = long_running_task_response_parts[6].strip() |
2687 |
| - is_successful = long_running_task_response_parts[7].strip() |
2688 |
| - is_complete = long_running_task_response_parts[8].strip() |
2689 |
| - log.info("Sleep for 5s.") |
2690 |
| - time.sleep(5) |
| 2684 | + poll_url = "/services/api/v1/task/{0}/progress".format(task_id) |
| 2685 | + while running_task: |
2691 | 2686 | log.info("Check if export task has completed.")
|
2692 |
| - if is_complete == "<isComplete>true</isComplete>": |
2693 |
| - if is_successful == "<isSuccessful>true</isSuccessful>": |
2694 |
| - log.info(percentage_complete) |
2695 |
| - log.info("Downloading content...") |
2696 |
| - log.debug("Extract taskId and download PDF.") |
2697 |
| - current_status = long_running_task_response_parts[3] |
2698 |
| - download_url = current_status.split("href="/wiki/")[1].split(""")[0] |
2699 |
| - long_running_task = False |
2700 |
| - elif is_successful == "<isSuccessful>false</isSuccessful>": |
| 2687 | + progress_response = self.get(poll_url) |
| 2688 | + percentage_complete = int(progress_response.get("progress", 0)) |
| 2689 | + task_state = progress_response.get("state") |
| 2690 | + if percentage_complete == 100: |
| 2691 | + running_task = False |
| 2692 | + log.info(f"Task completed - {task_state}") |
| 2693 | + if task_state == "FAILED": |
2701 | 2694 | log.error("PDF conversion not successful.")
|
2702 | 2695 | return None
|
| 2696 | + log.debug("Extract task results to download PDF.") |
| 2697 | + task_result_url = progress_response.get("result") |
2703 | 2698 | else:
|
2704 |
| - log.info(percentage_complete) |
| 2699 | + log.info(f"{percentage_complete}% - {task_state}") |
| 2700 | + time.sleep(3) |
| 2701 | + log.debug("Task successfully done, querying the task result for the download url") |
| 2702 | + # task result url starts with /wiki, remove it. |
| 2703 | + task_content = self.get(task_result_url[5:], not_json_response=True) |
| 2704 | + download_url = task_content.decode(encoding="utf-8", errors="strict") |
| 2705 | + log.debug("Successfully got the download url") |
| 2706 | + return download_url |
2705 | 2707 | except IndexError as e:
|
2706 | 2708 | log.error(e)
|
2707 | 2709 | return None
|
2708 | 2710 |
|
2709 |
| - return download_url |
2710 |
| - |
2711 | 2711 | def audit(
|
2712 | 2712 | self,
|
2713 | 2713 | start_date=None,
|
|
0 commit comments