1414def format_response (response : Any , show_header : bool , fields : Optional [List [str ]] = None ) -> List [List [str ]]:
1515 """Convert structured response object into a 'table' (where the length of each inner list is the same)"""
1616
17+ action_id : Optional [int ] = _get_action_id (response )
1718 response = _extract_primary (response )
1819
1920 if isinstance (response , list ):
@@ -40,6 +41,10 @@ def object_to_list(row: Dict[str, Any], columns: List[str]) -> List[Any]:
4041 data = [["name" , "value" ]] if show_header else []
4142 data += [_flatten (item , True ) for item in response .to_dict ().items ()]
4243
44+ # If response contained an action ID, prepend it to the formatted data
45+ if action_id :
46+ data .insert (1 if show_header else 0 , ["action_id" , str (action_id )])
47+
4348 return data
4449
4550
@@ -127,3 +132,18 @@ def _flatten_dict(item: Dict[str, Any], single_object: bool) -> str:
127132
128133 # Generic handler
129134 return "<object>" if not single_object else "\n " .join ([f"{ key } : { value } " for key , value in item .items ()])
135+
136+
137+ def _get_action_id (response : Any ) -> Optional [int ]:
138+ """Return response.links.actions[0].id or None"""
139+
140+ if not (links := getattr (response , "links" , None )):
141+ return None
142+
143+ if not (actions := getattr (links , "actions" , None )) or not isinstance (actions , list ):
144+ return None
145+
146+ if not (action_id := getattr (actions [0 ], "id" , None )) or not isinstance (action_id , int ):
147+ return None
148+
149+ return action_id
0 commit comments