Skip to content

Commit

Permalink
refactor: discover api endpoints dynamically
Browse files Browse the repository at this point in the history
gallagher command centre makes available a set of hrefs that should be
dynamically references as opposed to statically referencing api endpoints
this is the start of the change to do so. as part of this it's recommended to

- drop the root api endpoint query to be a privat endpoint
- the utils package to be renamed as core
- move the discovery contant outside of the APIEndpoint class

REFS #5
  • Loading branch information
devraj committed Nov 24, 2023
1 parent e43f834 commit d48390a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gallagher/cc/alarms/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ItemsTypes(APIEndpoint):
"""

__config__ = EndpointConfig(
endpoint="items/types",
endpoint=cls.paths.features.alarms.alarms.href,
dto_list=ItemTypesResponse,
)

Expand Down
28 changes: 25 additions & 3 deletions gallagher/cc/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
""" Utilities for the Gallagher Command Centre API
This package provides a set of utilities that are used that each endpoint
uses to communicate with the Gallagher Command Centre API.
Every endpoint inherits from the APIEndpoint class and must define
a configuration that is assigned to the variable __config__.
The endpoint variable in the EndpointConfig should be assigned to a reference
to the endpoint in the DiscoveryResponse object. When initialised the
endpoint will be assigned to None but will self heal as part of
the bootstrapping process.
"""
from typing import Optional
from dataclasses import dataclass

import httpx

from ..dto.discover import DiscoverResponse
from ..dto.discover import (
DiscoveryResponse,
FeaturesDetail,
)


def check_api_key_format(api_key):
Expand Down Expand Up @@ -93,7 +106,10 @@ class APIEndpoint():
# to access the endpoint then the library will throw an exception
#
# This value is memoized and should perform
paths = DiscoverResponse()
paths = DiscoveryResponse(
version="0.0.0", # Indicates that it's not been discovered
features=FeaturesDetail()
)

# This must be overridden by each child class that inherits
# from this base class.
Expand All @@ -107,7 +123,13 @@ def _discover(cls):
on the first operation that is accessed, subsequent calls
will return the cached result.
"""
pass
# Auto-discovery of the API endpoints, this will
# be called as part of the bootstrapping process
from ..cc import (
APIFeatureDiscovery
)

cls.paths = APIFeatureDiscovery.list()

@classmethod
def list(cls, skip=0):
Expand Down
10 changes: 7 additions & 3 deletions gallagher/dto/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"""

from typing import Optional
from typing import (
Annotated,
Optional,
)

from .utils import (
AppBaseModel,
Expand Down Expand Up @@ -228,8 +231,9 @@ class DiscoveryResponse(
the API client can work with the server.
"""

version: str
features: FeaturesDetail
version: Annotated[str, "The version of the server"]
features: Annotated[FeaturesDetail,
"A list of features available on the server"]

@property
def get_sem_ver(self):
Expand Down

0 comments on commit d48390a

Please sign in to comment.