<commit_before># -*- encoding: utf-8

import json

from flask import Response, jsonify


class ContextResponse(Response):
    """
    This class adds the "@context" parameter to JSON responses before
    they're sent to the user.

    For an explanation of how this works/is used, read
    https://blog.miguelgrinberg.com/post/customizing-the-flask-response-class
    """
    context_url = "https://api.wellcomecollection.org/storage/v1/context.json"

    def __init__(self, response, **kwargs):
        # Here we unmarshal the response as provided by Flask-RESTPlus, add
        # the @context parameter, then repack it.
        rv = json.loads(response)

        # The @context may already be provided if we've been through the
        # force_type method below.
        if "@context" in rv:
            return super(ContextResponse, self).__init__(response, **kwargs)
        else:
            rv["@context"] = self.context_url
            return super(ContextResponse, self).__init__(json.dumps(rv), **kwargs)

    @classmethod
    def force_type(cls, rv, environ=None):
        # All of our endpoints should be returning a dictionary to be
        # serialised as JSON.
        assert isinstance(rv, dict)

        assert "@context" not in rv, rv
        rv["@context"] = cls.context_url
<commit_msg>Handle a Werkzeug ClosingIterator (as exposed by the tests)<commit_after># -*- encoding: utf-8

import json

from flask import Response, jsonify
from werkzeug.wsgi import ClosingIterator


class ContextResponse(Response):
    """
    This class adds the "@context" parameter to JSON responses before
    they're sent to the user.

    For an explanation of how this works/is used, read
    https://blog.miguelgrinberg.com/post/customizing-the-flask-response-class
    """
    context_url = "https://api.wellcomecollection.org/storage/v1/context.json"

    def __init__(self, response, *args, **kwargs):
        """
        Unmarshal the response as provided by Flask-RESTPlus, add the
        @context parameter, then repack it.
        """
        if isinstance(response, ClosingIterator):
            response = b''.join([char for char in response])

        rv = json.loads(response)

        # The @context may already be provided if we've been through the
        # force_type method below.
        if "@context" in rv:
            return super(ContextResponse, self).__init__(response, **kwargs)
        else:
            rv["@context"] = self.context_url
            json_string = json.dumps(rv)
            return super(ContextResponse, self).__init__(json_string, **kwargs)

    @classmethod
    def force_type(cls, rv, environ=None):
        # All of our endpoints should be returning a dictionary to be
        # serialised as JSON.
        assert isinstance(rv, dict)

        assert "@context" not in rv, rv
        rv["@context"] = cls.context_url

{'fraction_non_alphanumeric': 0.09766306243460063, 'fraction_numerical': 0.0013951866062085804, 'mean_word_length': 2.724675324675325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4921346', 'n_tokens_mistral': 805, 'n_tokens_neox': 783, 'n_words': 262}