import  WEBSERVICE_PARAMS
from custom_exception.key import KeyNotFoundError
from utils.payload import *
class Constructor():
    def __init__(self):
        self.header = Header()
        self.body = Body()
        self.params_body = ()
    
    def set_payload(self, token: int, action: str, **kargs):
        self.header.set_token(token)
        self.header.set_action(action)
        
        self.body.set_body(kargs=kargs)
        self.params_body = WEBSERVICE_PARAMS[action]
        
        
    def verify(self):
        for k, v in self.body:
            for param in self.params_body:
                if param["param"] == k:
                    if not param["type"] == type(v):
                        raise ValueError(f"Valor do parametro {k} da action {self.header.action} está com o a tipagem errada")
                raise KeyNotFoundError(k, f"Não existe o parametro {k} na action {self.header.action}")
   
    def to_dict(self):
        payload = {
            "Header": self.header.to_dict(),
            "Body": self.body.to_dict(),
        }
        return payload
        