Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/utils.py
Show All 9 Lines | |||||
from swh.web.common.exc import NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.api.apiurls import APIUrls, api_route | from swh.web.api.apiurls import APIUrls, api_route | ||||
def api_lookup(lookup_fn, *args, | def api_lookup(lookup_fn, *args, | ||||
notfound_msg='Object not found', | notfound_msg='Object not found', | ||||
enrich_fn=None): | enrich_fn=None): | ||||
""" | r""" | ||||
Capture a redundant behavior of: | Capture a redundant behavior of: | ||||
- looking up the backend with a criteria (be it an identifier or | - looking up the backend with a criteria (be it an identifier or | ||||
checksum) passed to the function lookup_fn | checksum) passed to the function lookup_fn | ||||
- if nothing is found, raise an NotFoundExc exception with error | - if nothing is found, raise an NotFoundExc exception with error | ||||
message notfound_msg. | message notfound_msg. | ||||
- Otherwise if something is returned: | - Otherwise if something is returned: | ||||
- either as list, map or generator, map the enrich_fn function to | - either as list, map or generator, map the enrich_fn function to | ||||
it and return the resulting data structure as list. | it and return the resulting data structure as list. | ||||
- either as dict and pass to enrich_fn and return the dict | - either as dict and pass to enrich_fn and return the dict | ||||
enriched. | enriched. | ||||
Args: | Args: | ||||
- lookup_fn: function expects one criteria and optional supplementary | - lookup_fn: function expects one criteria and optional supplementary | ||||
\*args. | \*args. | ||||
- notfound_msg: if nothing matching the criteria is found, | - notfound_msg: if nothing matching the criteria is found, | ||||
raise NotFoundExc with this error message. | raise NotFoundExc with this error message. | ||||
- enrich_fn: Function to use to enrich the result returned by | - enrich_fn: Function to use to enrich the result returned by | ||||
lookup_fn. Default to the identity function if not provided. | lookup_fn. Default to the identity function if not provided. | ||||
- \*args: supplementary arguments to pass to lookup_fn. | - \*args: supplementary arguments to pass to lookup_fn. | ||||
Raises: | Raises: | ||||
NotFoundExp or whatever `lookup_fn` raises. | NotFoundExp or whatever `lookup_fn` raises. | ||||
""" # noqa | """ | ||||
if enrich_fn is None: | if enrich_fn is None: | ||||
enrich_fn = (lambda x: x) | enrich_fn = (lambda x: x) | ||||
res = lookup_fn(*args) | res = lookup_fn(*args) | ||||
if res is None: | if res is None: | ||||
raise NotFoundExc(notfound_msg) | raise NotFoundExc(notfound_msg) | ||||
if isinstance(res, (map, list, GeneratorType)): | if isinstance(res, (map, list, GeneratorType)): | ||||
return [enrich_fn(x) for x in res] | return [enrich_fn(x) for x in res] | ||||
return enrich_fn(res) | return enrich_fn(res) | ||||
Show All 23 Lines |