Created
February 20, 2019 17:01
-
-
Save Ronkiro/2f70d409f0e472c934f3b69018ef4c32 to your computer and use it in GitHub Desktop.
Initializer Pattern - Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class APIService(): | |
""" Manipula uma API """ | |
def __init__(self, | |
is_live=False, | |
API=None): | |
""" Inicia serviços de API. | |
:param API: Constrói uma API child. | |
""" | |
if not(API): | |
raise ValueError("Nenhuma API foi descrita") | |
self._is_live = is_live | |
#Inicia APIs | |
try: | |
self._api = { | |
"itau": Itau, | |
"infobip": Infobip, | |
"allin": Allin, | |
"maxipago": Maxipago, | |
"mundipagg": Mundipagg, | |
"velip": Velip, | |
"zenvia": ZEnvia | |
}[API.lower()](is_live) | |
except KeyError: | |
raise KeyError("API não encontrada ou inexistente.") | |
# Atribue o apontador da classe para a nova classe. | |
# Mantém os métodos definidos nessa classe | |
self.__class__ = self._api.__class__ | |
# Pegar variáveis da instância da API e readicionar à instância atual. | |
self.__dict__ = dict(self.__dict__, **self._api.__dict__) | |
del self._api |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment