[docs]defis_application_json(content_type:str)->bool:""" Whether the given content type is `application/json`. This is needed because mime types can contain additional options which are ignored here. """main_type=_parse_header(content_type)returnmain_type=="application/json"
[docs]defvalidate_that(condition:bool,msg:str)->None:""" Validate that the given condition is true, raising a ValidationError with the given message if it is not. This is implemented to write concise validating assertions. :raises ValidationError: if the condition is false """ifnotcondition:raiseValidationError(msg)
def_parse_header(line:str)->str:"""Parse a Content-type like header. Return the main content-type. """# Adapted from https://github.com/python/cpython/blob/3.12/Lib/cgi.py#L238# by adding type hints and removing the parsing of additional mime type options.return_parseparam(";"+line).__next__()def_parseparam(s:str)->Iterator[str]:# Adapted from https://github.com/python/cpython/blob/3.12/Lib/cgi.py#L226# by adding type hints.whiles[:1]==";":s=s[1:]end=s.find(";")whileend>0and(s.count('"',0,end)-s.count('\\"',0,end))%2:end=s.find(";",end+1)ifend<0:end=len(s)f=s[:end]yieldf.strip()s=s[end:]