1616from abc import ABC
1717from abc import abstractmethod
1818from enum import Enum
19+ from functools import cache
20+ from typing import Optional
1921
2022
2123class EndpointType (Enum ):
@@ -26,18 +28,25 @@ class EndpointType(Enum):
2628
2729class HostnameSupplier (ABC ):
2830 @abstractmethod
29- def get_hostname (self , endpoint_type : EndpointType ) -> str :
30- """Return the full base URL including scheme (e.g., 'https://example.com')."""
31+ def get_hostname (self , endpoint_type : Optional [EndpointType ] = None ) -> str :
32+ """Return a base URL including scheme.
33+
34+ If endpoint_type is None, returns the base hostname (e.g., 'https://example.com').
35+ If endpoint_type is provided, returns the endpoint-specific URL.
36+ """
3137 ...
3238
3339
3440class StaticHostnameSupplier (HostnameSupplier ):
3541 def __init__ (self , base_url : str ) -> None :
42+ self ._base_url = base_url
3643 self ._api_gateway_url = base_url + "/api"
3744 self ._multipass_url = base_url + "/multipass/api"
3845 self ._stream_proxy_url = base_url + "/stream-proxy/api"
3946
40- def get_hostname (self , endpoint_type : EndpointType ) -> str :
47+ def get_hostname (self , endpoint_type : Optional [EndpointType ] = None ) -> str :
48+ if endpoint_type is None :
49+ return self ._base_url
4150 if endpoint_type == EndpointType .GENERIC :
4251 return self ._api_gateway_url
4352 elif endpoint_type == EndpointType .AUTH :
@@ -51,24 +60,23 @@ def get_hostname(self, endpoint_type: EndpointType) -> str:
5160class ServiceDiscoveryHostnameSupplier (HostnameSupplier ):
5261 def __init__ (self , services : dict [str , list [str ]]) -> None :
5362 self ._services = services
54- self ._url_cache : dict [EndpointType , str ] = {}
5563
56- def get_hostname (self , endpoint_type : EndpointType ) -> str :
57- if endpoint_type in self ._url_cache :
58- return self ._url_cache [endpoint_type ]
64+ @cache
65+ def get_hostname ( # pyright: ignore[reportIncompatibleMethodOverride]
66+ self , endpoint_type : Optional [EndpointType ] = None
67+ ) -> str :
68+ if endpoint_type is None :
69+ raise ValueError ("ServiceDiscoveryHostnameSupplier requires an endpoint_type." )
5970
6071 if endpoint_type == EndpointType .GENERIC :
61- url = self ._find_service_url ("api-gateway" )
72+ return self ._find_service_url ("api-gateway" )
6273 elif endpoint_type == EndpointType .AUTH :
63- url = self ._find_service_url ("multipass" )
74+ return self ._find_service_url ("multipass" )
6475 elif endpoint_type == EndpointType .HIGH_SCALE :
65- url = self ._find_service_url ("stream-proxy" )
76+ return self ._find_service_url ("stream-proxy" )
6677 else :
6778 raise ValueError (f"Unsupported endpoint type: { endpoint_type } " )
6879
69- self ._url_cache [endpoint_type ] = url
70- return url
71-
7280 def _find_service_url (self , service_name : str ) -> str :
7381 if service_name not in self ._services :
7482 raise ValueError (f"Unable to discover service '{ service_name } '." )
0 commit comments