"""Validators for request query parameters.""" from oto import response as oto_response from oto import status as oto_status from availability.constants import error from availability.constants import stores def validate_product_ids_parameter(query_params): """Validate products_id query parameter, should be list of ints. Function is intended to usage for GET /status endpoint. Args: query_params (ImmutableMultiDict): dict with supplied params. Returns: oto.response.Response: .message with list of product ids on success, .errors on failure. """ if not len(query_params) == 1 or not query_params.get('product_ids'): return oto_response.create_error_response( code=error.ERROR_CODE_QUERY_PARAM, message=error.ERROR_INVALID_QUERY_PARAM) try: product_ids = [int(i) for i in query_params['product_ids'].split(',')] except ValueError: # means we've got not integer product ids return oto_response.create_error_response( code=error.ERROR_CODE_QUERY_PARAM, message=error.ERROR_INVALID_QUERY_PARAM) return oto_response.Response(product_ids) def validate_store_id(store_id): """Check whether store with provided id exists. Args: store_id (int): PK of particular store. Returns: oto.response.Response: .status == 204 on success, .errors on failure. """ try: store_id = int(store_id) except (TypeError, ValueError): return oto_response.create_not_found_response( error.ERROR_MESSAGE_STORE_NOT_FOUND) if store_id not in stores.STORE_IDS: return oto_response.create_not_found_response( error.ERROR_MESSAGE_STORE_NOT_FOUND) return oto_response.Response(status=oto_status.OK, message=store_id)