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 ,
26+ priority : int ,
27+ refresh_type : str ,
28+ workbook_id : Optional [str ] = None ,
29+ datasource_id : Optional [str ] = None
30+ ):
31+ self ._id : Optional [str ] = None
32+ self ._priority = priority
33+ self ._type = refresh_type
34+ self ._workbook_id = workbook_id
35+ self ._datasource_id = datasource_id
36+
37+ @property
38+ def id (self ) -> Optional [str ]:
39+ return self ._id
40+
41+ @property
42+ def priority (self ) -> int :
43+ return self ._priority
44+
45+ @property
46+ def type (self ) -> str :
47+ return self ._type
48+
49+ @property
50+ def workbook_id (self ) -> Optional [str ]:
51+ return self ._workbook_id
52+
53+ @property
54+ def datasource_id (self ) -> Optional [str ]:
55+ return self ._datasource_id
56+
57+ @classmethod
58+ def from_response (cls , resp : str , ns : dict ) -> List ["ExtractItem" ]:
59+ """Create ExtractItem objects from XML response."""
60+ parsed_response = fromstring (resp )
61+ return cls .from_xml_element (parsed_response , ns )
62+
63+ @classmethod
64+ def from_xml_element (cls , parsed_response : ET .Element , ns : dict ) -> List ["ExtractItem" ]:
65+ """Create ExtractItem objects from XML element."""
66+ all_extract_items = []
67+ all_extract_xml = parsed_response .findall (".//t:extract" , namespaces = ns )
68+
69+ for extract_xml in all_extract_xml :
70+ extract_id = extract_xml .get ("id" , None )
71+ priority = int (extract_xml .get ("priority" , 0 ))
72+ refresh_type = extract_xml .get ("type" , None )
73+
74+ # Check for workbook or datasource
75+ workbook_elem = extract_xml .find (".//t:workbook" , namespaces = ns )
76+ datasource_elem = extract_xml .find (".//t:datasource" , namespaces = ns )
77+
78+ workbook_id = workbook_elem .get ("id" , None ) if workbook_elem is not None else None
79+ datasource_id = datasource_elem .get ("id" , None ) if datasource_elem is not None else None
80+
81+ extract_item = cls (priority , refresh_type , workbook_id , datasource_id )
82+ extract_item ._id = extract_id
83+
84+ all_extract_items .append (extract_item )
85+
86+ return all_extract_items
0 commit comments