[docs]classIterableGCSResponse(IterableResponse):""" Response class for non-paged list oriented resources. Allows top level fields to be accessed normally via standard item access, and also provides a convenient way to iterate over the sub-item list in the ``data`` key: >>> print("Path:", r["path"]) >>> # Equivalent to: for item in r["data"] >>> for item in r: >>> print(item["name"], item["type"]) """default_iter_key="data"
[docs]classUnpackingGCSResponse(GlobusHTTPResponse):""" An "unpacking" response looks for a "data" array in the response data, which is expected to have dict elements. The "data" is traversed until the first matching object is found, and this is presented as the ``data`` property of the response. The full response data is available as ``full_data``. If the expected datatype is not found in the array, or the array is missing, the ``data`` will be the full response data (identical to ``full_data``). :param match: Either a string containing a DATA_TYPE prefix, or an arbitrary callable which does the matching """def_default_unpacking_match(self,spec:str)->t.Callable[[dict[str,t.Any]],bool]:ifnotre.fullmatch(r"\w+",spec):raiseValueError("Invalid UnpackingGCSResponse specification.")defmatch_func(data:dict[str,t.Any])->bool:ifnot("DATA_TYPE"indataandisinstance(data["DATA_TYPE"],str)):returnFalseif"#"notindata["DATA_TYPE"]:returnFalsename,_version=data["DATA_TYPE"].split("#",1)returnname==specreturnmatch_funcdef__init__(self,response:GlobusHTTPResponse,match:str|t.Callable[[dict[str,t.Any]],bool],):super().__init__(response)ifcallable(match):self._match_func=matchelse:self._match_func=self._default_unpacking_match(match)self._unpacked_data:dict[str,t.Any]|None=Noneself._did_unpack=False@propertydeffull_data(self)->t.Any:""" The full, parsed JSON response data. ``None`` if the data cannot be parsed as JSON. """returnself._parsed_jsondef_unpack(self)->dict[str,t.Any]|None:""" Unpack the response from the `"data"` array, returning the first match found. If no matches are founds, or the data is the wrong shape, return None. """ifisinstance(self._parsed_json,dict)andisinstance(self._parsed_json.get("data"),list):foriteminself._parsed_json["data"]:ifisinstance(item,dict)andself._match_func(item):returnitemreturnNone@propertydefdata(self)->t.Any:# only do the unpacking operation once, as it may be expensive on large payloadsifnotself._did_unpack:self._unpacked_data=self._unpack()self._did_unpack=Trueifself._unpacked_dataisnotNone:returnself._unpacked_datareturnself._parsed_json