from tools.http_client import ApiError, http_get

from config.constants import PlayerServerInfo

WHITELIST_URL_ENDPOINT = "iswhitelist/"


class WhiteList:
    @staticmethod
    def check_whitelist(url: str, discord_user_id: str) -> None:
        try:
            api_data = http_get(f"{url}{WHITELIST_URL_ENDPOINT}{discord_user_id}").json()
        except ApiError:
            raise
        except ValueError as exc:
            raise ApiError(
                "Réponse JSON invalide lors de la vérification whitelist.",
                url=f"{url}{WHITELIST_URL_ENDPOINT}{discord_user_id}",
            ) from exc

        PlayerServerInfo.is_whitelist = api_data.get("whitelisted", False)
        PlayerServerInfo.is_staff = api_data.get("isStaff", False)
