def json_or_msgpack(request, content, expires=None, headers=None):
    media_types = request.headers.get('Accept', JSON_MIME_TYPE).split(', ')
    for media_type in media_types:
        if (media_type == '*/*'):
            media_type = JSON_MIME_TYPE
            break
        if (media_type == MSGPACK_MIME_TYPE):
            break
        if (media_type == JSON_MIME_TYPE):
            break
    else:
        media_type = JSON_MIME_TYPE
    assert (media_type in {JSON_MIME_TYPE, MSGPACK_MIME_TYPE})
    content_as_dict = content.dict()
    with record_timing(request.state.metrics, 'tok'):
        etag = md5(str(content_as_dict).encode()).hexdigest()
    headers = (headers or {})
    headers['ETag'] = etag
    if (expires is not None):
        headers['Expires'] = expires.strftime(HTTP_EXPIRES_HEADER_FORMAT)
    if (request.headers.get('If-None-Match', '') == etag):
        return Response(status_code=304, headers=headers)
    if (media_type == 'application/x-msgpack'):
        return MsgpackResponse(content_as_dict, headers=headers, metrics=request.state.metrics)
    return NumpySafeJSONResponse(content_as_dict, headers=headers, metrics=request.state.metrics)
def json_or_msgpack(request, content, expires=None, headers=None):
    media_types = request.headers.get('Accept', JSON_MIME_TYPE).split(', ')
    for media_type in media_types:
        if (media_type == '*/*'):
            media_type = JSON_MIME_TYPE
            break
        if (media_type == MSGPACK_MIME_TYPE):
            break
        if (media_type == JSON_MIME_TYPE):
            break
    else:
        media_type = JSON_MIME_TYPE
    assert (media_type in {JSON_MIME_TYPE, MSGPACK_MIME_TYPE})
    content_as_dict = content.dict()
    with record_timing(request.state.metrics, 'tok'):
        etag = md5(str(content_as_dict).encode()).hexdigest()
    headers = (headers or {})
    headers['ETag'] = etag
    if (expires is not None):
        headers['Expires'] = expires.strftime(HTTP_EXPIRES_HEADER_FORMAT)
    if (request.headers.get('If-None-Match', ) == etag):
        return Response(status_code=304, headers=headers)
    if (media_type == 'application/x-msgpack'):
        return MsgpackResponse(content_as_dict, headers=headers, metrics=request.state.metrics)
    return NumpySafeJSONResponse(content_as_dict, headers=headers, metrics=request.state.metrics)