"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 19.08.22

"""
import json

import requests
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse, HttpRequest


@login_required
def map_client_proxy_view(request: HttpRequest):
    """ Provides proxy functionality for NETGIS map client.

    Used for fetching content of a provided url

    Args:
        request (HttpRequest): The incoming request

    Returns:

    """
    url = request.META.get("QUERY_STRING")
    response = requests.get(url)
    body = json.loads(response.content)
    if response.status_code != 200:
        return JsonResponse({
            "status_code": response.status_code,
            "content": body,
        })
    return JsonResponse(body)
