def get_id_from_object(obj: Dict[str, Any], key, name=None, required=False) -> Optional[UUID]: ''' Given a dict, get a sample ID from the dict if it exists. If None or an empty dict is passed to the method, None is returned. :param obj: the dict wherein the ID can be found. :param key: the key in the dict for the ID value. :param name: the name of the ID to use in an exception, defaulting to the key. :param required: if no ID is present, throw an exception. :returns: the ID, if it exists, or None. :raises MissingParameterError: if the ID is required but not present. :raises IllegalParameterError: if the ID is provided but is invalid. ''' id_ = None _check_string(key, 'key') name = name if name else key if required and (not obj or not obj.get(key)): raise _MissingParameterError(name) if obj and obj.get(key): id_ = validate_sample_id(obj[key], name) return id_
def get_id_from_object(obj: Dict[str, Any], required=False) -> Optional[UUID]: ''' Given a dict, get a sample ID from the dict if it exists, using the key 'id'. If None or an empty dict is passed to the method, None is returned. :param obj: The dict wherein the ID can be found. :param required: If no ID is present, throw an exception. :returns: The ID, if it exists, or None. :raises MissingParameterError: If the ID is required but not present. :raises IllegalParameterError: If the ID is provided but is invalid. ''' id_ = None if required and (not obj or not obj.get(ID)): raise _MissingParameterError('Sample ID') if obj and obj.get(ID): if type(obj[ID]) != str: raise _IllegalParameterError( f'Sample ID {obj[ID]} must be a UUID string') try: id_ = UUID(obj[ID]) except ValueError as _: # noqa F841 raise _IllegalParameterError( f'Sample ID {obj[ID]} must be a UUID string') return id_
def _check_string_int(params: Dict[str, Any], key: str, required=False) -> Optional[str]: v = params.get(key) if v is None: if required: raise _MissingParameterError(key) else: return None if type(v) != str: raise _IllegalParameterError(f'{key} key is not a string as required') return _cast(str, v)
def get_version_from_object(params: Dict[str, Any], required: bool = False) -> Optional[int]: ''' Given a dict, get a sample version from the dict if it exists, using the key 'version'. :param params: the unmarshalled JSON recieved from the API as part of the API call. :param required: if True, throw and exception if the version is not supplied. :returns: the version or None if no version was provided. :raises MissingParameterError: if the version is required and not present. :raises IllegalParameterError: if the version is not an integer or < 1. ''' _check_params(params) ver = params.get('version') if ver is None and required: raise _MissingParameterError('version') if ver is not None and (type(ver) != int or ver < 1): raise _IllegalParameterError(f'Illegal version argument: {ver}') return ver