Skip to content

Instantly share code, notes, and snippets.

@Ronkiro
Created February 20, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ronkiro/2f70d409f0e472c934f3b69018ef4c32 to your computer and use it in GitHub Desktop.
Save Ronkiro/2f70d409f0e472c934f3b69018ef4c32 to your computer and use it in GitHub Desktop.
Initializer Pattern - Python
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