Source code for invenio_records_rest.serializers.response
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Serialization response factories.
Responsible for creating a HTTP response given the output of a serializer.
"""
from flask import current_app
[docs]def record_responsify(serializer, mimetype):
"""Create a Records-REST response serializer.
:param serializer: Serializer instance.
:param mimetype: MIME type of response.
:returns: Function that generates a record HTTP response.
"""
def view(pid, record, code=200, headers=None, links_factory=None):
response = current_app.response_class(
serializer.serialize(pid, record, links_factory=links_factory),
mimetype=mimetype,
)
response.status_code = code
response.cache_control.no_cache = True
response.set_etag(str(record.revision_id))
response.last_modified = record.updated
if headers is not None:
response.headers.extend(headers)
if links_factory is not None:
add_link_header(response, links_factory(pid))
return response
return view
[docs]def search_responsify(serializer, mimetype):
"""Create a Records-REST search result response serializer.
:param serializer: Serializer instance.
:param mimetype: MIME type of response.
:returns: Function that generates a record HTTP response.
"""
def view(
pid_fetcher,
search_result,
code=200,
headers=None,
links=None,
item_links_factory=None,
):
response = current_app.response_class(
serializer.serialize_search(
pid_fetcher,
search_result,
links=links,
item_links_factory=item_links_factory,
),
mimetype=mimetype,
)
response.status_code = code
if headers is not None:
response.headers.extend(headers)
if links is not None:
add_link_header(response, links)
return response
return view