class GetStatus:
    def __init__(self):
        self.response = None
        self.token = None
        self.config = Config()
        self.header = self.__header()

    def __token(self):
        w = Token()
        return w.getToken()

    def __header(self):
        auth_token = self.__token()
        header = {
            'Content-type': 'application/json',
            'Authorization': 'Bearer ' + auth_token
        }
        return header

    def getResponse(self):
        dict_json = self.response
        if dict_json is not None:
            lstContatos = dict_json['Data']['contacts']
            lstResponses = []
            for values in lstContatos:
                row = values['input'] + ';' + values['status']
                lstResponses.append(str(row))
            return lstResponses
        else:
            return None

    def __url(self):
        return self.config.urlContact()

    def __proxy(self):
        return self.config.proxy()

    def request(self, dict_nu_terminal):
        try:
            body = {'contacts': dict_nu_terminal}
            #print(self.header)
            #resp = requests.post(self.__url(),proxies=self.__proxy(), json=body, headers=self.header)
            resp = requests.post(self.__url(), json=body, headers=self.header)
            if not resp is None:
                if 'json' in resp.headers.get('Content-Type'):
                    self.response = resp.json()
                else:
                    print('Response content is not in JSON format.')
            else:
                print('Resp is None')
        except requests.exceptions.HTTPError as errh:
            print("Http Error:", errh)
        except requests.exceptions.ConnectionError as errc:
            print("Error Connecting:", errc)
        except requests.exceptions.Timeout as errt:
            print("Timeout Error:", errt)
        except requests.exceptions.RequestException as err:
            print("OOps: Something Else", err)
class Token:
    def __init__(self):
        self.token = None
        self.config = Config()
        self.response = None

    def __url(self):
        return self.config.urlToken()

    def __proxy(self):
        return self.config.proxy()

    def __body(self):
        return {'user': self.config.user(), 'password': self.config.passwd()}

    def __request(self):
        try:
            header = header = {'Content-type': 'application/json'}
            #resp = requests.post(self.__url(),proxies=self.__proxy(), json=self.__body(), headers=header)
            resp = requests.post(self.__url(),
                                 json=self.__body(),
                                 headers=header)
            self.response = resp.json()
            #print(resp.json())
        except requests.exceptions.HTTPError as errh:
            print("Http Error:", errh)
        except requests.exceptions.ConnectionError as errc:
            print("Error Connecting:", errc)
        except requests.exceptions.Timeout as errt:
            print("Timeout Error:", errt)
        except requests.exceptions.RequestException as err:
            print("OOps: Something Else", err)

    def getToken(self):
        self.__request()
        dict_json = self.response
        if dict_json is not None:
            #print(str(dict_json))
            code = dict_json['Code']
            description = dict_json['Description']
            if code == 808:
                token = dict_json['Data']['token']
                return token
            else:
                return description
        else:
            return None