<commit_before>import json
import os
from time import time as time_now
import redis
import requests

from django.conf import settings


class OccupEyeApi():
    """
    Python API for the Cad-Capture OccupEye backend.
    Data is cached as much as possible in Redis for performance.
    """
    # Keep survey data around in the cache for an hour
    SURVEY_TTL = 3600
    def __init__(self, test_mode=False):
        self.r = redis.StrictRedis(
            host=settings.REDIS_UCLAPI_HOST,
            charset="utf-8",
            decode_responses=True
        )
        self.deployment_id = os.environ["OCCUPEYE_DEPLOYMENT_ID"]
        self.deployment_name = os.environ["OCCUPEYE_DEPLOYMENT_NAME"]
        self.base_url = os.environ["OCCUPEYE_BASE_URL"]

        # If either of those variables come up false, make sure
        # we have an access token before we continue.
        # We don't want to do anything manually in test mode to avoid
        # hitting environment variables we may not have yet.
        if test_mode:
            self.access_token = None
            self.access_token_expiry = None
        else:
            self.access_token = self.r.get("occupeye:access_token")
            self.access_token_expiry = int(self.r.get("occupeye:access_token_expiry"))
            if not self.access_token or not self.access_token_expiry:
                self.get_token()

    def _str2bool(self, v):
        return v.lower() in ("yes", "true", "t", "1")

    def get_token(self):
        url = self.base_url + "/token"
        body = {
            "Grant_type": "password",
            "Username": os.environ["OCCUPEYE_USERNAME"],
            "Password": os.environ["<PASSWORD>"]
        }

        request = requests.post(
            url=url,<commit_msg>Adds some token expiry sanity checks<commit_after>import json
import os
from time import time as time_now
import redis
import requests

from django.conf import settings


class OccupEyeApi():
    """
    Python API for the Cad-Capture OccupEye backend.
    Data is cached as much as possible in Redis for performance.
    """
    # Keep survey data around in the cache for an hour
    SURVEY_TTL = 3600
    def __init__(self, test_mode=False):
        self.r = redis.StrictRedis(
            host=settings.REDIS_UCLAPI_HOST,
            charset="utf-8",
            decode_responses=True
        )
        self.deployment_id = os.environ["OCCUPEYE_DEPLOYMENT_ID"]
        self.deployment_name = os.environ["OCCUPEYE_DEPLOYMENT_NAME"]
        self.base_url = os.environ["OCCUPEYE_BASE_URL"]

        # If either of those variables come up false, make sure
        # we have an access token before we continue.
        # We don't want to do anything manually in test mode to avoid
        # hitting environment variables we may not have yet.
        if test_mode:
            self.access_token = None
            self.access_token_expiry = None
        else:
            self.access_token = self.r.get("occupeye:access_token")

            access_token_expiry = self.r.get("occupeye:access_token_expiry")
            # We can only cast this value to an int if we know it's not None
            if access_token_expiry:
                self.access_token_expiry = int(access_token_expiry)
            else:
                self.access_token_expiry = None
            if not self.access_token or not self.access_token_expiry:
                self.get_token()

    def _str2bool(self, v):
        return v.lower() in ("yes", "true", "t", "1")

    def get_token(self):
        url = self.base_url + "/token"
        body = {
            "Grant_type": "password",
            "Username": os.environ["OCCUPEYE_USERNAME"],
            "Password": <PASSWORD>["<PASSWORD>"]
        }

        request = requests.post(
            url=url,
{'fraction_non_alphanumeric': 0.08652070693748351, 'fraction_numerical': 0.0036929570034291742, 'mean_word_length': 2.2633390705679863, 'pattern_counts': {'":': 6, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730806', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1128, 'n_words': 316}