def _parse_body_part(part: ResponseLike, parser: Parser) -> Optional[Object]: headers = part.headers try: content_type = headers['content-type'] except KeyError: return None if 'text/xml' in content_type: try: parser.parse_xml(part) except RetsApiError as e: if e.reply_code == 20403: # No object found return None raise elif 'text/html' in content_type: raise RetsParseError(part.content) location = headers.get('location') if location: data = None else: data = part.content or None return Object( mime_type=_guess_mime_type(headers), content_id=headers.get('content-id'), description=headers.get('content-description'), object_id=headers['object-id'], url=location, preferred='Preferred' in headers, data=data, )
def parse_response(response: Response_) -> Any: content_type = response.headers['content-type'] if 'text/xml' in content_type: return parse_xml(response) elif 'multipart/parallel' in content_type: return parse_multipart(response) elif 'text/html' in content_type: raise RetsParseError(response.content) return parse_body_part(response)
def _get_parser(data_type: str, interpretation: str): if interpretation == _LOOKUP_TYPE: return _LOOKUP_PARSER elif interpretation in _LOOKUP_MULTI_TYPES: return _LOOKUP_MULTI_PARSER try: return _DATA_TYPE_PARSERS[data_type] except KeyError: raise RetsParseError('unknown data type %s' % data_type) from None
def _get_decoder(data_type: str, interpretation: str, include_tz: bool = False): if interpretation == _LOOKUP_TYPE: return str elif interpretation in _LOOKUP_MULTI_TYPES: return lambda value: value.split(',') if data_type in _TIMEZONE_AWARE_DECODERS: return partial(_TIMEZONE_AWARE_DECODERS[data_type], include_tz=include_tz) try: return _DECODERS[data_type] except KeyError: raise RetsParseError('unknown data type %s' % data_type) from None
def _find_or_raise(elem: Element, child_elem_name: str) -> Element: child = elem.find(child_elem_name) if child is None: raise RetsParseError('Missing %s element' % child_elem_name) return child