def convert_pagination_params(params: RequestParams) -> RequestParams: """Allow ``count_only=True`` as a slightly more intuitive shortcut to only get a count of results""" if params.pop('count_only', False) is True: params['per_page'] = 0 if params.pop('reverse', False) is True: params['order'] = 'descending' return params
def translate_rank_range(params: RequestParams) -> RequestParams: """If min and/or max rank is specified in params, translate into a list of ranks""" def _get_rank_index(rank: str) -> int: if rank not in RANKS: raise ValueError("Invalid rank") return RANKS.index(rank) min_rank, max_rank = params.pop("min_rank", None), params.pop("max_rank", None) if min_rank or max_rank: # Use indices in RANKS list to determine range of ranks to include min_rank_index = _get_rank_index(min_rank) if min_rank else 0 max_rank_index = _get_rank_index(max_rank) + 1 if max_rank else len(RANKS) params["rank"] = RANKS[min_rank_index:max_rank_index] return params
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
def split_common_params( params: RequestParams) -> Tuple[RequestParams, RequestParams]: """Split out common keyword args (for pyinaturalist functions) from request params (for API)""" kwargs = {k: params.pop(k, None) for k in COMMON_PARAMS} return params, kwargs