swh.web.api.apiresponse module#

Add Link header in returned value results.

Parameters:
  • request – a DRF Request object

  • rv (dict) –

    dictionary with keys:

    • headers: potential headers with ‘link-next’ and ‘link-prev’ keys

    • results: containing the result to return

  • options (dict) – the initial dict to update with result if any

Returns:

dictionary with optional keys ‘link-next’ and ‘link-prev’

Return type:

dict

swh.web.api.apiresponse.filter_by_fields(request: Request, data: Dict[str, Any]) Dict[str, Any][source]#

Extract a request parameter ‘fields’ if it exists to permit the filtering on the data dict’s keys.

If such field is not provided, returns the data as is.

swh.web.api.apiresponse.transform(rv: Dict[str, Any]) Dict[str, Any][source]#

Transform an eventual returned value with multiple layer of information with only what’s necessary.

If the returned value rv contains the ‘results’ key, this is the associated value which is returned.

Otherwise, return the initial dict without the potential ‘headers’ key.

swh.web.api.apiresponse.make_api_response(request: Request, data: Dict[str, Any], doc_data: Dict[str, Any] | None = None, options: Dict[str, Any] | None = None) HttpResponse[source]#

Generates an API response based on the requested mimetype.

Parameters:
  • request – a DRF Request object

  • data – raw data to return in the API response

  • doc_data – documentation data for HTML response

  • options – optional data that can be used to generate the response

Returns:

a DRF Response a object

swh.web.api.apiresponse.error_response(request: Request, exception: Exception, doc_data: Dict[str, Any]) HttpResponse[source]#

Private function to create a custom error response.

Parameters:
  • request – a DRF Request object

  • error – the exception that caused the error

  • doc_data – documentation data for HTML response

swh.web.api.apiresponse.error_response_handler(exc: Exception, context: Dict[str, Any]) HttpResponse | None[source]#

Custom DRF exception handler used to generate API error responses.