def scrub_response(secret):
    """Helper function that provides a filter function for stripping secrets
    from HTTP responses before they are stored by vcrpy

    Based on the example implementation found her:
    https://vcrpy.readthedocs.io/en/latest/advanced.html#custom-response-filtering

    Example:

        >>> def vcr_config():
        >>>     secret = "1234"
        >>>     return {
        >>>         "before_record_response": scrub_response(secret)
        >>>     }

    Args:
        secret (str): authentication token to be cleaned

    Returns:
        function:
            filter function that can be passed to vcrpy to clean all
            response bodies before they are stored to a cassette
    """
    def inner_scrub_response(response):
        """Inner function used to scrub an HTTP response structure

        Args:
            response (dict):
                data structure describing the contents of the HTTP response

        Returns:
            dict: cleaned version of the response description
        """
        # For running on CI or with local tests only, where we don't need
        # an authentication key, this scrubbing method need not do anything
        if not secret:
            return response

        # Scrub our authentication token from the response body
        # NOTE: for some reason vcrpy expects the response body to be formatted
        #       as a byte array instead of a unicode character string
        body = str(response["body"]["string"])
        if secret in body:
            response["body"]["string"] = \
                body.replace(secret, "DUMMY").encode("utf-8")

        # Scrub our authentication token from the response headers
        if "Location" in response["headers"]:
            # The default location header field has an upper case L
            loc_key = "Location"
        elif "location" in response["headers"]:
            # When the API rate limit has been reached the header field for
            # location gets changed to a lower case l for some weird reason
            loc_key = "location"
        else:
            loc_key = None
        if loc_key:
            temp = response["headers"][loc_key][0]
            response["headers"][loc_key][0] = temp.replace(secret, "DUMMY")

        # One final sanity check to make sure the token doesn't appear
        # anywhere else in the response data structure
        assert secret not in str(response)

        # Return the cleaned response data to the caller
        return response

    # Return our filter function to the caller
    return inner_scrub_response