def zip_resource(self, resource: str, api_params: DartApiParamType, extract_to: PathLike, *, timeout: Any = None) -> None: url = furl(self._dart_openapi_base_url + f"{resource}.xml") url.args.update({"crtfc_key": self.api_key, **api_params}) timeout = timeout or self.default_timeout res = self._get_client().get(url.tostr(), timeout=timeout) res.raise_for_status() content_type = res.headers.get("Content-Type") if content_type == "application/xml" or res.headers.get( "Content-Disposition") is None: etree = ElementTree.parse(BytesIO(res.content)) raise_for_dart_status( etree.find("status").text, etree.find("message").text) with TemporaryFile() as tempf: for chunk in res.iter_bytes(): if chunk is not None: tempf.write(chunk) with ZipFile(tempf) as zipf: zipf.extractall(extract_to)
def json_resource(self, resource: str, api_params: DartApiParamType, *, timeout: Any = None) -> Mapping: url = furl(self._dart_openapi_base_url + f"{resource}.json") url.args.update({"crtfc_key": self.api_key, **api_params}) timeout = timeout or self.default_timeout res = self._get_client().get(url.tostr(), timeout=timeout) res.raise_for_status() json = res.json() raise_for_dart_status(json.get("status"), json.get("message")) del json["status"] del json["message"] return json
def xml_resource(self, resource: str, api_params: DartApiParamType, *, timeout: Any = None) -> ElementTree: url = furl(self._dart_openapi_base_url + f"{resource}.xml") url.args.update({"crtfc_key": self.api_key, **api_params}) timeout = timeout or self.default_timeout res = self._get_client().get(url.tostr(), timeout=timeout) res.raise_for_status() etree = ElementTree.parse(BytesIO(res.content)) status_element = etree.find("status") message_element = etree.find("message") raise_for_dart_status(status_element.text, message_element.text) etree.getroot().remove(status_element) etree.getroot().remove(message_element) return etree