swh.web.api.views.utils module

class swh.web.api.views.utils.EnrichFunction(*args, **kwds)[source]

Bases: typing_extensions.Protocol

swh.web.api.views.utils.api_lookup(lookup_fn: Callable[[...], Any], *args: Any, notfound_msg: Optional[str] = 'Object not found', enrich_fn: Optional[swh.web.api.views.utils.EnrichFunction] = None, request: Optional[django.http.request.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.

  • 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.


NotFoundExp or whatever lookup_fn raises.

swh.web.api.views.utils.api_home(self, request, *args, **kwargs)[source]

Display the list of opened api endpoints.