swh.web.api.views.utils module#
- swh.web.api.views.utils.api_lookup(lookup_fn: Callable[[...], Any], *args: Any, notfound_msg: Optional[str] = 'Object not found', enrich_fn: Optional[Union[Callable[[Dict[str, str], Optional[HttpRequest]], Dict[str, str]], Callable[[Tuple[List[Dict[str, Any]], Optional[str]], Optional[HttpRequest]], Tuple[List[Dict[str, Any]], Optional[str]]]]] = None, request: Optional[HttpRequest] = None, **kwargs: Any)[source]#
- Capture a redundant behavior of:
looking up the backend with a criteria (be it an identifier or checksum) passed to the function lookup_fn
if nothing is found, raise an NotFoundExc exception with error message notfound_msg.
- Otherwise if something is returned:
either as list, map or generator, map the enrich_fn function to it and return the resulting data structure as list.
either as dict and pass to enrich_fn and return the dict enriched.
- Parameters:
lookup_fn (-) – function expects one criteria and optional supplementary *args.
*args (-) – supplementary arguments to pass to lookup_fn.
notfound_msg (-) – if nothing matching the criteria is found, raise NotFoundExc with this error message.
enrich_fn (-) – Function to use to enrich the result returned by lookup_fn. Default to the identity function if not provided.
request (-) – Input HTTP request that will be provided as parameter to enrich_fn.
- Raises:
NotFoundExp or whatever lookup_fn raises. –