def _validate_multiple_choice_param( params: RequestParams, key: str, choices: Iterable) -> Tuple[RequestParams, Optional[str]]: """Verify that a multiple-choice request parameter contains valid value(s); if not, return an error message. Returns: Parameters with modifications (if any), and a validation error message (if any) """ def is_valid(value, choices): if not value: return True if not isinstance(value, list): value = [value] return all([v in choices for v in value]) def normalize(value): if not value: return value if isinstance(value, list): return [v.replace(' ', '_') for v in value] return value.replace(' ', '_') error_msg = None if key in params: params[key] = normalize(params[key]) if not is_valid(params.get(key), choices): error_msg = MULTIPLE_CHOICE_ERROR_MSG.format(key, choices, params[key]) return params, error_msg
def convert_observation_fields(params: RequestParams) -> RequestParams: """Translate simplified format of observation field values into API-compatible format""" if "observation_fields" in params: params["observation_field_values_attributes"] = params.pop("observation_fields") obs_fields = params.get("observation_field_values_attributes") if isinstance(obs_fields, dict): params["observation_field_values_attributes"] = [ {"observation_field_id": k, "value": v} for k, v in obs_fields.items() ] return params
def convert_observation_field_params(params: RequestParams) -> RequestParams: """Translate simplified format of observation field values into API-compatible format""" if 'observation_fields' in params: params['observation_field_values_attributes'] = params.pop( 'observation_fields') obs_fields = params.get('observation_field_values_attributes') if isinstance(obs_fields, dict): params['observation_field_values_attributes'] = [{ 'observation_field_id': k, 'value': v } for k, v in obs_fields.items()] return params