import requests
from pprint import pprint

class Exfo:
    BASE_URL = "https://159.192.8.11/API/REST"
    BASE_IP = "https://159.192.8.11"
    def __init__(self, user, passwd):
        self.user = user
        self.passwd = passwd
        self.cookies = None
        self.session = requests.Session()

    def login(self):
        pprint("--- login ---")
        payload = f'uname={self.user}&pword={self.passwd}&format=json&encryptpword=0'
        headers = {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Accept': 'application/x-www-form-urlencoded',
                }
        url = self.BASE_URL + "/Login"
        response = self.session.post(url, headers=headers, data=payload, verify=False)
       
        pprint(self.session.cookies.get_dict())
        pprint(response.text)
        pprint(response.cookies)
        #self.cookies = response.cookies
    def list_api(self):
        pprint("---- list_api ---")
        payload = {}
        headers = {
          'Accept': 'application/json'
        }
        url = self.BASE_URL + "/"
        response = self.session.get(url, headers=headers, data=payload, verify=False)
        pprint(response.json())
        return response

    def call_api(self, service, payload={}):
        
        headers = {
          'Accept': 'application/json'
        }
        if service == "sla":
            ep = "/SLAs/v1/SLA?size=0"
        url = self.BASE_URL + ep
        response = self.session.get(url, headers=headers, data=payload, verify=False)
        pprint(response.json())
        return response

    def call_remote_api(self, ep, payload={}):
        
        headers = {
          'Accept': 'application/json'
        }
        url = self.BASE_IP + ep
        pprint(url)
        response = self.session.get(url, headers=headers, data=payload, verify=False)
        pprint(response)
        return response

    def logout(self):
        pprint("---- logout ---")
        pprint(self.session.cookies.get_dict())
        payload = {}
        headers = {
          'Accept': 'application/json',
        }
        url = self.BASE_URL + "/Logoff?action=logoff"
        
        response = self.session.post(url, headers=headers, data=payload,  verify=False)
        pprint(response)
        pprint(response.text)

    def video_tier(self):
        pprint("------ vdo tier -------")
        url = self.BASE_URL + "/Verifiers/v2/VideoTier"

        payload = {}
        headers = {
          'Accept': 'application/json'
        }

        response = self.session.get(url, headers=headers, data=payload, verify=False)
        pprint(response.json())

    def test_implement(self):
        pprint('------- test_implement -----')
        url = self.BASE_URL + "/Test/v1"

        payload = {}
        headers = {
          'Accept': 'application/json'
        }
        pprint(self.session.cookies.get_dict()) 
        response = self.session.get(url, headers=headers, data=payload, verify=False)

        pprint(response.json)

    def test_avl_test_types(self):
        pprint('------- test_avl_test_types -----')
        url  = self.BASE_URL + "/Test/v1/TypeByName?list_all=true"

        payload = {}
        headers = {
          'Accept': 'application/json'
        }
        response = self.session.get(url, headers=headers, data=payload, verify=False)
        print(response.json())





if __name__ == '__main__':
    e = Exfo("administrator", "exf0w0rxC@t4dm!n")
    e.login()
    e.list_api()
    e.video_tier()
    e.test_implement()
    e.test_avl_test_types()
    e.logout()

