Skip to content

Commit

Permalink
refactor: moves capabilities singleton out of base class
Browse files Browse the repository at this point in the history
i wasn't quite happy with the idea that the singleton was part of the
base class that all the endpoints inherited from, this is now moved
into a class called Capabilties which has a class level variable called
CURRENT to represent the current capabilities of the server we are connected
to, this reads a lot better than before

REFS #5

we should also explore if it's possible to move back to using the __config__
constants being assigned by the endpoint classes as opposed to the
method returning the configuration

there may be lifecycle issues here
  • Loading branch information
devraj committed Dec 5, 2023
1 parent 66e99c4 commit 6dd7d72
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 24 deletions.
5 changes: 3 additions & 2 deletions gallagher/cc/alarms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from ..core import (
APIEndpoint,
EndpointConfig
EndpointConfig,
Capabilities
)

from ...dto.alarm import (
Expand All @@ -23,7 +24,7 @@ class Alarms(
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.alarms.alarms.href,
endpoint=Capabilities.CURRENT.features.alarms.alarms.href,
dto_list=AlarmResponse,
dto_retrieve=AlarmZoneSummary,
)
3 changes: 2 additions & 1 deletion gallagher/cc/alarms/day_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from ..core import (
Capabilities,
APIEndpoint,
EndpointConfig
)
Expand All @@ -20,7 +21,7 @@ class DayCategory(APIEndpoint):
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.day_categories
endpoint=Capabilities.CURRENT.features.day_categories
.day_categories.href,
dto_list=DayCategoryResponse,
dto_retrieve=DayCategoryResponse,
Expand Down
3 changes: 2 additions & 1 deletion gallagher/cc/alarms/divisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from ..core import (
Capabilities,
APIEndpoint,
EndpointConfig
)
Expand All @@ -24,7 +25,7 @@ class Division(APIEndpoint):
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.divisions.divisions.href,
endpoint=Capabilities.CURRENT.features.divisions.divisions.href,
dto_list=DivisionDetailResponse,
dto_retrieve=DivisionDetail,
)
5 changes: 3 additions & 2 deletions gallagher/cc/alarms/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from ..core import (
Capabilities,
APIEndpoint,
EndpointConfig,
)
Expand All @@ -23,7 +24,7 @@ class ItemsTypes(APIEndpoint):
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.items.item_types.href,
endpoint=Capabilities.CURRENT.features.items.item_types.href,
dto_list=ItemTypesResponse,
dto_retrieve=ItemTypesResponse,
)
Expand All @@ -40,7 +41,7 @@ class Item(APIEndpoint):
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.items.items.href,
endpoint=Capabilities.CURRENT.features.items.items.href,
dto_list=ItemsSummaryResponse,
dto_retrieve=ItemDetail,
)
3 changes: 2 additions & 1 deletion gallagher/cc/alarms/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from ..core import (
Capabilities,
APIEndpoint,
EndpointConfig
)
Expand All @@ -19,6 +20,6 @@ class Schedule(APIEndpoint):
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.schedules.schedules.href,
endpoint=Capabilities.CURRENT.features.schedules.schedules.href,
dto_list=ScheduleSummaryResponse,
)
3 changes: 2 additions & 1 deletion gallagher/cc/cardholders/card_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
from ..core import (
Capabilities,
APIEndpoint,
EndpointConfig
)
Expand All @@ -22,7 +23,7 @@ class CardType(APIEndpoint):
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.card_types.card_types.href,
endpoint=Capabilities.CURRENT.features.card_types.card_types.href,
dto_list=CardTypeResponse,
dto_retrieve=CardTypeResponse,
)
3 changes: 2 additions & 1 deletion gallagher/cc/cardholders/cardholders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
from ..core import (
Capabilities,
APIEndpoint,
EndpointConfig
)
Expand All @@ -23,7 +24,7 @@ class Cardholder(APIEndpoint):
@classmethod
def get_config(cls):
return EndpointConfig(
endpoint=APIEndpoint._capabilities.features.cardholders.cardholders.href,
endpoint=Capabilities.CURRENT.features.cardholders.cardholders.href,
dto_list=CardholderSummaryResponse,
dto_retrieve=CardholderDetail,
)
33 changes: 18 additions & 15 deletions gallagher/cc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,7 @@ def validate_endpoint(cls):
)


class APIEndpoint():
""" Base class for all API objects
All API endpoints must inherit from this class and provide a Config class
that automates the implementation of many of the API methods.
If the endpoints provide additional methods then they are to implement them
based on the same standards as this base class.
"""
class Capabilities():

# Discover response object, each endpoint will reference
# one of the instance variable Href property to get the
Expand All @@ -123,11 +114,23 @@ class APIEndpoint():
# to access the endpoint then the library will throw an exception
#
# This value is memoized and should perform
_capabilities = DiscoveryResponse(
CURRENT = DiscoveryResponse(
version="0.0.0", # Indicates that it's not been discovered
features=FeaturesDetail()
)


class APIEndpoint():
""" Base class for all API objects
All API endpoints must inherit from this class and provide a Config class
that automates the implementation of many of the API methods.
If the endpoints provide additional methods then they are to implement them
based on the same standards as this base class.
"""

# Do not set this variable in your class, this is set by the
# lifecycle methods and use to cache the configuration object
__config__ = None
Expand Down Expand Up @@ -158,12 +161,12 @@ def _discover(cls):
This differs per endpoint that we work with.
Note that references to APIEndpoint._capabilities as a singleton, while
Note that references to Capabilities.CURRENT as a singleton, while
cls.method when executing a class method.
"""

if APIEndpoint._capabilities.version != "0.0.0" and\
type(APIEndpoint._capabilities.good_known_since) is datetime:
if Capabilities.CURRENT.version != "0.0.0" and\
type(Capabilities.CURRENT.good_known_since) is datetime:
# We've already discovered the endpoint hence
# we can stop execution to improve performance
# and avoid network round trips.
Expand All @@ -183,7 +186,7 @@ def _discover(cls):

# Assign the capabilities to the class, this should
# result in the endpoint
APIEndpoint._capabilities = parsed_obj
Capabilities.CURRENT = parsed_obj

# Set this so the configuration is only discovered
# once per endpoint
Expand Down

0 comments on commit 6dd7d72

Please sign in to comment.