# Path: credentials.py
# RUBRIK_USER = 'youraccount'
# 
# Path: credentials.py
# RUBRIK_PASSWORD = 'yourpassword'


import json
import time
import requests
import logging.config
from requests.auth import HTTPBasicAuth
from credentials import RUBRIK_USER  # Login info now stored in credentials.py
from credentials import RUBRIK_PASSWORD  # Login info now stored in credentials.py
class MonitorJSON:
    """This is a simple class passed to Monitor threads so we can access the current JSON data in that thread"""
    def __init__(self):
        self.json = ""
        self.session = None
        self.data = RubrikData()
        self.token = None
        self.detail_report_id = None


def get_rubrik_token(rubrik_monitor):
    """This function will create a request session and get a login session token from the Rubrik
       According to the docs the session token is valid for 3 hours after last use."""

    logger = logging.getLogger(__name__)

    # Create Session object to persist JSESSIONID cookie (so the next requests to Rubrik will work)
    rubrik_monitor.session = requests.Session()
    rubrik_monitor.session.headers.update({'content-type': 'application/json'})
    rubrik_monitor.session.auth = (RUBRIK_USER, RUBRIK_PASSWORD)

    try:
        # For all our requests, use the verify=False to ignore certificate errors
        r = rubrik_monitor.session.post(RUBRIK_URL + "/api/v1/session", verify=False)
    except (requests.exceptions.Timeout,
            requests.exceptions.ConnectionError,
            requests.exceptions.HTTPError) as e:
        logger.warning("Error making request to " + RUBRIK_URL + "/login.")
        return None


# Path: credentials.py
# RUBRIK_USER = 'youraccount'
# 
# Path: credentials.py
# RUBRIK_PASSWORD = 'yourpassword'


# Path: rubrik.py
import json
import time
import requests
import logging.config
from requests.auth import HTTPBasicAuth
from credentials import RUBRIK_USER  # Login info now stored in credentials.py
from credentials import RUBRIK_PASSWORD  # Login info now stored in credentials.py

class MonitorJSON:
    """This is a simple class passed to Monitor threads so we can access the current JSON data in that thread"""
    def __init__(self):
        self.json = ""
        self.session = None
        self.data = RubrikData()
        self.token = None
        self.detail_report_id = None


def get_rubrik_token(rubrik_monitor):
    """This function will create a request session and get a login session token from the Rubrik
       According to the docs the session token is valid for 3 hours after last use."""

    logger = logging.getLogger(__name__)

    # Create Session object to persist JSESSIONID cookie (so the next requests to Rubrik will work)
    rubrik_monitor.session = requests.Session()
    rubrik_monitor.session.headers.update({'content-type': 'application/json'})
    rubrik_monitor.session.auth = (RUBRIK_USER, RUBRIK_PASSWORD)

    try:
        # For all our requests, use the verify=False to ignore certificate errors
        r = rubrik_monitor.session.post(RUBRIK_URL + "/api/v1/session", verify=False)
    except (requests.exceptions.Timeout,
            requests.exceptions.ConnectionError,
            requests.exceptions.HTTPError) as e:
        logger.warning("Error making request to " + RUBRIK_URL + "/login.")
        return None


    result = json.loads(r.text)