|
1 | 1 | import logging
|
2 | 2 | import time
|
| 3 | +from urllib.parse import parse_qs, urlparse |
3 | 4 |
|
4 | 5 | import requests
|
5 | 6 |
|
@@ -32,6 +33,26 @@ def __init__(
|
32 | 33 | self.base_url = base_url.rstrip("/")
|
33 | 34 | self.headers = {"apikey": self.api_key}
|
34 | 35 |
|
| 36 | + def _extract_execution_id_from_url(self, url: str) -> str | None: |
| 37 | + """ |
| 38 | + Extract execution_id from a URL's query parameters. |
| 39 | +
|
| 40 | + Args: |
| 41 | + url: URL containing execution_id parameter |
| 42 | +
|
| 43 | + Returns: |
| 44 | + str | None: The execution_id if found, None otherwise |
| 45 | + """ |
| 46 | + try: |
| 47 | + parsed_url = urlparse(url) |
| 48 | + query_params = parse_qs(parsed_url.query) |
| 49 | + execution_ids = query_params.get("execution_id") |
| 50 | + if execution_ids: |
| 51 | + return execution_ids[0] # Get the first value |
| 52 | + except Exception as e: |
| 53 | + self.logger.warning("Failed to extract execution_id from URL: %s", e) |
| 54 | + return None |
| 55 | + |
35 | 56 | def process(
|
36 | 57 | self,
|
37 | 58 | endpoint: str,
|
@@ -76,6 +97,13 @@ def process(
|
76 | 97 |
|
77 | 98 | data = response.json()
|
78 | 99 | execution_id = data.get("execution_id")
|
| 100 | + |
| 101 | + # If execution_id is not directly available, try to extract from status_api |
| 102 | + if not execution_id: |
| 103 | + status_api = data.get("message", {}).get("status_api") |
| 104 | + if status_api: |
| 105 | + execution_id = self._extract_execution_id_from_url(status_api) |
| 106 | + |
79 | 107 | self.logger.info(
|
80 | 108 | "Processing started successfully. Execution ID: %s", execution_id
|
81 | 109 | )
|
@@ -119,7 +147,17 @@ def get_result(self, endpoint: str, execution_id: str) -> dict:
|
119 | 147 | )
|
120 | 148 | response = requests.get(url, headers=self.headers, params=params)
|
121 | 149 |
|
122 |
| - if response.status_code != 200: |
| 150 | + if response.status_code == 422: |
| 151 | + # Handle 422 status which may indicate processing in progress |
| 152 | + try: |
| 153 | + data = response.json() |
| 154 | + if "status" in data: |
| 155 | + return data |
| 156 | + except (ValueError, KeyError): |
| 157 | + # JSON parsing failed or status key missing, treat as error |
| 158 | + pass |
| 159 | + raise ApiHubClientException(response.text, response.status_code) |
| 160 | + elif response.status_code != 200: |
123 | 161 | raise ApiHubClientException(response.text, response.status_code)
|
124 | 162 |
|
125 | 163 | return response.json()
|
@@ -170,7 +208,7 @@ def wait_for_completion(
|
170 | 208 | ),
|
171 | 209 | None,
|
172 | 210 | )
|
173 |
| - elif status in ["PROCESSING", "IN_PROGRESS", "RUNNING"]: |
| 211 | + elif status in ["PROCESSING", "IN_PROGRESS", "RUNNING", "EXECUTING"]: |
174 | 212 | # Continue polling
|
175 | 213 | pass
|
176 | 214 | else:
|
|
0 commit comments