Skip to content

Commit 6089532

Browse files
authored
Enhance get_ncm_instance with auto-initialization
Updated error handling in get_ncm_instance to support auto-initialization from environment variables.
1 parent 88921c7 commit 6089532

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

ncm/ncm/ncm.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ def get_routers(self, **kwargs):
15801580
'id', 'id__in', 'ipv4_address', 'ipv4_address__in',
15811581
'mac', 'mac__in', 'name', 'name__in',
15821582
'reboot_required', 'reboot_required__in',
1583-
'serial_number','state', 'state__in',
1583+
'serial_number', 'serial_number__in', 'state', 'state__in',
15841584
'state_updated_at__lt', 'state_updated_at__gt',
15851585
'updated_at__lt', 'updated_at__gt', 'expand',
15861586
'order_by', 'limit', 'offset']
@@ -5116,7 +5116,7 @@ def _load_api_keys_from_env() -> Tuple[Optional[Dict[str, str]], Optional[str]]:
51165116
def get_ncm_instance() -> Union['NcmClientv2', 'NcmClientv3', 'NcmClientv2v3']:
51175117
"""
51185118
Get the singleton NCM instance.
5119-
Raises an error if no instance has been created yet.
5119+
Automatically initializes from environment variables if no instance exists.
51205120
51215121
Environment variables:
51225122
- X_CP_API_ID: CP API ID for v2 API
@@ -5135,7 +5135,12 @@ def get_ncm_instance() -> Union['NcmClientv2', 'NcmClientv3', 'NcmClientv2v3']:
51355135
"""
51365136
global _ncm_instance
51375137
if _ncm_instance is None:
5138-
raise RuntimeError("No NCM instance has been created yet. Call ncm.set_api_keys() first.")
5138+
# Try to auto-initialize from environment variables
5139+
env_v2_keys, env_v3_key = _load_api_keys_from_env()
5140+
if env_v2_keys or env_v3_key:
5141+
_ncm_instance = NcmClient(api_keys=env_v2_keys, api_key=env_v3_key, log_events=False)
5142+
else:
5143+
raise RuntimeError("No NCM instance has been created yet. Call ncm.set_api_keys() first or set environment variables (X_CP_API_ID, X_CP_API_KEY, X_ECM_API_ID, X_ECM_API_KEY, NCM_API_TOKEN).")
51395144
return _ncm_instance
51405145

51415146

0 commit comments

Comments
 (0)