import requests
from django.conf import settings
from requests.auth import HTTPBasicAuth


class APIHandler:
    def __init__(self):
        with open(settings.CONFIG_FILE_PATH, 'r') as config_file:
            self.organisation_id = config_file.read().replace('\n', '')

    def call_api(self, url):
        return requests.get(url, auth=HTTPBasicAuth(
            settings.API_BASIC_USERNAME, settings.API_BASIC_PASSWORD
        ))

    def get_themes(self):
        url = "{}/api/organisations/{}/themes/".format(
            settings.BO_API_URL, self.organisation_id
        )
        response = self.call_api(url)
        response.raise_for_status()
        return response.json()

    def check_qrcode_validity(self, token):
        url = "{}/api/organisations/{}/user/{}".format(
            settings.BO_API_URL, self.organisation_id, token
        )
        response = self.call_api(url)
        response.raise_for_status()
        return response.json()
