|
| 1 | +from typing import Optional, List |
| 2 | +from defusedxml.ElementTree import fromstring |
| 3 | +import xml.etree.ElementTree as ET |
| 4 | + |
| 5 | + |
| 6 | +class ExtractItem: |
| 7 | + """ |
| 8 | + An extract refresh task item. |
| 9 | +
|
| 10 | + Attributes |
| 11 | + ---------- |
| 12 | + id : str |
| 13 | + The ID of the extract refresh task |
| 14 | + priority : int |
| 15 | + The priority of the task |
| 16 | + type : str |
| 17 | + The type of extract refresh (incremental or full) |
| 18 | + workbook_id : str, optional |
| 19 | + The ID of the workbook if this is a workbook extract |
| 20 | + datasource_id : str, optional |
| 21 | + The ID of the datasource if this is a datasource extract |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, priority: int, refresh_type: str, workbook_id: Optional[str] = None, datasource_id: Optional[str] = None |
| 26 | + ): |
| 27 | + self._id: Optional[str] = None |
| 28 | + self._priority = priority |
| 29 | + self._type = refresh_type |
| 30 | + self._workbook_id = workbook_id |
| 31 | + self._datasource_id = datasource_id |
| 32 | + |
| 33 | + @property |
| 34 | + def id(self) -> Optional[str]: |
| 35 | + return self._id |
| 36 | + |
| 37 | + @property |
| 38 | + def priority(self) -> int: |
| 39 | + return self._priority |
| 40 | + |
| 41 | + @property |
| 42 | + def type(self) -> str: |
| 43 | + return self._type |
| 44 | + |
| 45 | + @property |
| 46 | + def workbook_id(self) -> Optional[str]: |
| 47 | + return self._workbook_id |
| 48 | + |
| 49 | + @property |
| 50 | + def datasource_id(self) -> Optional[str]: |
| 51 | + return self._datasource_id |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def from_response(cls, resp: str, ns: dict) -> List["ExtractItem"]: |
| 55 | + """Create ExtractItem objects from XML response.""" |
| 56 | + parsed_response = fromstring(resp) |
| 57 | + return cls.from_xml_element(parsed_response, ns) |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def from_xml_element(cls, parsed_response: ET.Element, ns: dict) -> List["ExtractItem"]: |
| 61 | + """Create ExtractItem objects from XML element.""" |
| 62 | + all_extract_items = [] |
| 63 | + all_extract_xml = parsed_response.findall(".//t:extract", namespaces=ns) |
| 64 | + |
| 65 | + for extract_xml in all_extract_xml: |
| 66 | + extract_id = extract_xml.get("id", None) |
| 67 | + priority = int(extract_xml.get("priority", 0)) |
| 68 | + refresh_type = extract_xml.get("type", "") |
| 69 | + |
| 70 | + # Check for workbook or datasource |
| 71 | + workbook_elem = extract_xml.find(".//t:workbook", namespaces=ns) |
| 72 | + datasource_elem = extract_xml.find(".//t:datasource", namespaces=ns) |
| 73 | + |
| 74 | + workbook_id = workbook_elem.get("id", None) if workbook_elem is not None else None |
| 75 | + datasource_id = datasource_elem.get("id", None) if datasource_elem is not None else None |
| 76 | + |
| 77 | + extract_item = cls(priority, refresh_type, workbook_id, datasource_id) |
| 78 | + extract_item._id = extract_id |
| 79 | + |
| 80 | + all_extract_items.append(extract_item) |
| 81 | + |
| 82 | + return all_extract_items |
0 commit comments