# (c) cavaliba.com  - data/api - ping.py

"""
/api/ (index, always denied) and /api/ping/ (unauthenticated liveness check).

Method inventory: index, ping.
"""

from django.views.decorators.csrf import csrf_exempt

import app_data.api.helper as helper
from app_data.ip import get_user_ip
from app_home.log import ERROR, log


#  ----------------------------------------------------------------------------
# /api/
#  ----------------------------------------------------------------------------
@csrf_exempt
def index(request):
    """Root of the API: always denied (no anonymous browsing of /api/)."""
    log(
        ERROR,
        app="api/ping",
        view="index",
        action=request.method,
        status="KO",
        data="denied",
        user_ip=get_user_ip(request),
    )
    return helper.send_denied()


#  ----------------------------------------------------------------------------
# /api/ping/
# NO AUTH
#  ----------------------------------------------------------------------------
@csrf_exempt
def ping(request):
    """Unauthenticated liveness check; always returns {"status": "OK"}."""

    reply = {"status": "OK"}
    # time.sleep(5)
    return helper.send_response(request, reply, 200)
