import requests
from pprint import pprint

import functools

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("--- call remote ---")
        pprint(url)
        pprint(payload)
        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())



from requests.auth import HTTPBasicAuth

SLA_MAP = {
        "BRK":"61.7.161.157",
        "SRI":"61.7.161.156",	
        "RBI":"61.7.161.158",	
        "KKN":"61.7.161.160",	
        "RBI BV-110":"61.7.161.162",	
        "NSN":"61.7.161.164",	
        "CBI":"61.7.161.163",	
        "CMI":"61.7.161.171,61.7.161.172",	
        "HYI":"61.7.161.180",	
        "NMA":"61.7.161.179",	
        "CRI":"61.7.161.178",	
        "SNI":"61.7.161.177",	
        "SPI":"61.7.161.175",	
        "PTY BV-110":"61.7.161.174",	
        "PTY":"61.7.161.159,61.7.161.161",	
        "PLK":"61.7.161.169",	
        "HYI _BV-100":"61.7.161.168",	
        "CMI BV-110":"61.7.161.167",	
        "KKN BV-110":"61.7.161.165",	
        "UBN":"61.7.161.166",	
        "NSN BV-110":"61.7.161.170"
        }

class Mikrotik:
    # BASE_URL = "https://103.10.230.196/rest/"
    BASE_URL = "https://110.77.145.239/rest/"
    basic = HTTPBasicAuth('brix', 'Digitins_01')

    headers = {
      'Authorization': 'Basic YWRtaW46RGlnaXRpbnNfMDE='
    }

    def __init__(self, ip=None):
        if ip:
            self.BASE_URL = f"https://{ip}/rest/"

    def call_remote(self, cmd, payload={}):
        url = self.BASE_URL + cmd
        pprint(url)
        response = requests.get(url, auth=self.basic,  data=payload, verify=False, timeout=3)
        return 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()

    mkt = Mikrotik()
    pprint("--- ip/route ---")
    pprint(mkt.call_remote("ip/route"))
    pprint("--- ip/address -- ")
    pprint(mkt.call_remote("ip/address"))

