Skip to content

Commit

Permalink
Merge pull request #72 from inloco/singleton-metaclass
Browse files Browse the repository at this point in the history
Add a singleton metaclass for IncogniaAPI
  • Loading branch information
aninhalbuquerque authored Sep 20, 2024
2 parents 201aa24 + 965dc68 commit 963507e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/continuous.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ jobs:
- name: Publish Python distribution to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository_url: https://test.pypi.org/legacy/
repository-url: https://test.pypi.org/legacy/
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
skip_existing: true
print_hash: true
skip-existing: true
print-hash: true
verbose: true
- name: Publish Python distribution to PyPI
if: startsWith(github.ref, 'refs/tags/v')
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
print_hash: true
print-hash: true
verbose: true
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]
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ class TestIncogniaAPI(TestCase):
})
DEFAULT_PARAMS: Final[None] = None

def test_metaclass_singleton_should_always_return_the_same_instance(self):
api1 = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)
api2 = IncogniaAPI(self.CLIENT_ID, self.CLIENT_SECRET)

self.assertEqual(api1, api2)

@patch.object(BaseRequest, 'post')
@patch.object(TokenManager, 'get', return_value=TOKEN_VALUES)
def test_register_new_signup_when_request_token_is_valid_should_return_a_valid_dict(
Expand Down

0 comments on commit 963507e

Please sign in to comment.