import requests
import base64
import logging

logger = logging.getLogger(__name__)

class MenMiceLogin():
    base_url = "https://ipam-api.eu.boehringer.com/mmws/api"
    #base_url = "https://10.183.177.24/mmws/api"

    def __init__(self, username, password) -> None:
        self.auth = base64.b64encode(f"{username}:{password}".encode()).decode()
        self.authenticate()

    def authenticate(self):
        # Código para autenticar
        headers = {
            "Authorization": f"Basic {self.auth}",
            "Content-Type": "application/json"
        }
        login_url = f"{self.base_url}/login"
        response = requests.get(login_url, headers=headers, verify=False)
        if response.ok:
            r_json = response.json()
            self.token = r_json["result"]["session"]
            logger.info("Login succesful to Men&Mice")
            return self.token            
        else:
            print("Fallo al hacer loggin")
            raise Exception(f"Can't log to Men&Mice\r\n {r_json} \r\n {self.auth} \r\n  {self.token}")
        

'''Código para probar el login'''

'''if __name__ == "__main__":
    username = "x2ipmgmtsoar4pa"
    password = "6g9e:q+!X&b~'W~@:~diO7z0qb.lQX"
    men = MenMiceLogin(username, password)
    response = men.authenticate()
    print(response)'''