def _get_api_response_element_from_element(element): """ >>> d = ApiDecoder() >>> isinstance(_get_api_response_element_from_element('string element'), ApiContentStringElement) True >>> isinstance(_get_api_response_element_from_element(123123.0), ApiContentNumberElement) True >>> isinstance(_get_api_response_element_from_element(False), ApiContentBooleanElement) True """ if isinstance(element, str): return ApiContentElementFactory.get_response_element_by_type('string') elif isinstance(element, bool): element = ApiContentElementFactory.get_response_element_by_type('boolean') return element elif isinstance(element, float) or isinstance(element, int): return ApiContentElementFactory.get_response_element_by_type('number') raise ApiParsingException( message="input element type(%s) does not match any ApiResponseElement" % type(element) )
def _get_api_response_element_from_element_and_comment(element, comment): if comment is None: return _get_api_response_element_from_element(element) else: assert isinstance(comment, ApiDecoder.ApiComment) if 'type' in comment: assert _validate_element_with_string_type(element, comment.get('type')), \ 'The type defined in comment does not match.' api_response_element = ApiContentElementFactory.get_response_element_by_type(comment.get('type')) else: api_response_element = _get_api_response_element_from_element(element) assert comment.get('required', None) is not None, 'Process error: getting comment without required tag' api_response_element.required = comment.get('required') return api_response_element