Skip to content

Commit

Permalink
Add a singleton metaclass for IncogniaAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
aninhalbuquerque committed Sep 19, 2024
1 parent 9eed693 commit 27f24ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion incognia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from .exceptions import IncogniaHTTPError, IncogniaError
from .json_util import encode
from .models import Coordinates, StructuredAddress, TransactionAddress, PaymentValue, PaymentMethod
from .singleton import Singleton
from .token_manager import TokenManager
from .base_request import BaseRequest, JSON_CONTENT_HEADER


class IncogniaAPI:
class IncogniaAPI(metaclass=Singleton):
def __init__(self, client_id: str, client_secret: str):
self.__token_manager = TokenManager(client_id, client_secret)
self.__request = BaseRequest()
Expand Down
8 changes: 8 additions & 0 deletions incognia/singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class Singleton(type):
_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]

0 comments on commit 27f24ec

Please sign in to comment.