Skip to content

Commit 1a3410d

Browse files
authored
Update ncm.py
1 parent 2114b9e commit 1a3410d

1 file changed

Lines changed: 62 additions & 25 deletions

File tree

ncm/ncm/ncm.py

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,9 +2226,22 @@ def set_ncm_api_keys_by_router(self, router_id=None, router_name=None, x_ecm_api
22262226
'{0}/configuration_managers/?router.id={1}&fields=id,configuration'.format(
22272227
self.base_url,
22282228
str(router_id))) # Get Configuration Managers ID
2229-
response = json.loads(response.content.decode(
2229+
2230+
# Check response status
2231+
if response.status_code != 200:
2232+
raise Exception(f"Failed to get configuration manager: HTTP {response.status_code} - {response.text}")
2233+
2234+
response_data = json.loads(response.content.decode(
22302235
"utf-8")) # Decode the response and make it a dictionary
2231-
config_man_id = response['data'][0][
2236+
2237+
# Check if response has data and is not empty
2238+
if 'data' not in response_data:
2239+
raise Exception(f"Unexpected API response format. Response: {response_data}")
2240+
2241+
if not response_data['data'] or len(response_data['data']) == 0:
2242+
raise Exception(f"No configuration manager found for router_id: {router_id}")
2243+
2244+
config_man_id = response_data['data'][0][
22322245
'id'] # get the Configuration Managers ID from response
22332246

22342247
x509 = "-----BEGIN CERTIFICATE-----\nMIIB0jCCATugAwIBAgIUIF7Bygk4C0l0ikNv00u98unXZ9kwDQYJKoZIhvcNAQEL\nBQAwFzEVMBMGA1UEAwwMTmV0Q2xvdWQgQVBJMB4XDTI1MDYwNDA5MjYzNloXDTM1\nMDYwMzA5MjYzNlowFzEVMBMGA1UEAwwMTmV0Q2xvdWQgQVBJMIGfMA0GCSqGSIb3\nDQEBAQUAA4GNADCBiQKBgQDHWAtI42kixQBU9yZdiTmakxlj1OGfXlYGYDTMr/Q7\neFRZHLxJwIwrfV4UjJSvXkeo9ui1JNXzfQzDwZXdJKEdFM0fBpu9TD/cyetz9lCs\nh5YL1aC0IcH/liZwGt/z2X4snqe3KADHjy8Dl/5ib16vTC/FuRm02Bf8wVJ0c/sr\nhwIDAQABoxswGTAJBgNVHREEAjAAMAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEL\nBQADgYEAB5UavmWqkT7MXnt2/RE2qdtoTw4PfWIo+I2O7FAwJmHISubp3LW1vCn0\nRIsnyscH+BZmQkZOk3AYhLikgSky64HRHK32HXrLr79ku4as0drJzxuVOOKJn1+6\nDiNWTpAhzT55WU3fZ9H6FRvfEls0ZtLia/yiZ60rH01RO0lo2bs=\n-----END CERTIFICATE-----\n"
@@ -5249,31 +5262,55 @@ def _delegate_to_instance(method_name: str, *args: Any, **kwargs: Any) -> Any:
52495262
return getattr(instance, method_name)(*args, **kwargs)
52505263

52515264

5252-
# Create module-level functions for key methods
5253-
def get_routers(*args, **kwargs):
5254-
"""Module-level access to get_routers method (v2 API)"""
5255-
return _delegate_to_instance('get_routers', *args, **kwargs)
5256-
5257-
5258-
def get_exchange_resources(*args, **kwargs):
5259-
"""Module-level access to get_exchange_resources method (v3 API)"""
5260-
return _delegate_to_instance('get_exchange_resources', *args, **kwargs)
5261-
5262-
5263-
# Add other commonly used methods as needed
5264-
def get_accounts(*args, **kwargs):
5265-
"""Module-level access to get_accounts method"""
5266-
return _delegate_to_instance('get_accounts', *args, **kwargs)
5267-
5268-
5269-
def get_groups(*args, **kwargs):
5270-
"""Module-level access to get_groups method"""
5271-
return _delegate_to_instance('get_groups', *args, **kwargs)
5265+
def _setup_all_module_methods():
5266+
"""
5267+
Automatically create module-level functions for all methods in NCM client classes.
5268+
This makes all methods available as module-level functions (e.g., ncm.get_routers()).
5269+
"""
5270+
# Collect all public methods from the client classes
5271+
all_methods = set()
5272+
5273+
# Get methods from NcmClientv2
5274+
for name in dir(NcmClientv2):
5275+
if not name.startswith('_') and callable(getattr(NcmClientv2, name)):
5276+
all_methods.add(name)
5277+
5278+
# Get methods from NcmClientv3
5279+
for name in dir(NcmClientv3):
5280+
if not name.startswith('_') and callable(getattr(NcmClientv3, name)):
5281+
all_methods.add(name)
5282+
5283+
# Get methods from NcmClientv2v3
5284+
for name in dir(NcmClientv2v3):
5285+
if not name.startswith('_') and callable(getattr(NcmClientv2v3, name)):
5286+
all_methods.add(name)
5287+
5288+
# Exclude methods that are already defined or are special methods
5289+
excluded = {
5290+
'set_api_keys', 'get_ncm_instance', 'set_ncm_instance', 'reset_ncm_instance',
5291+
'__class__', '__dict__', '__doc__', '__module__', '__weakref__'
5292+
}
5293+
5294+
# Create module-level functions for each method
5295+
for method_name in all_methods:
5296+
if method_name in excluded or method_name in globals():
5297+
continue
5298+
5299+
# Create a wrapper function that delegates to the instance
5300+
# Use default parameter to capture method_name in closure
5301+
def make_wrapper(name=method_name):
5302+
def wrapper(*args, **kwargs):
5303+
return _delegate_to_instance(name, *args, **kwargs)
5304+
wrapper.__name__ = name
5305+
wrapper.__doc__ = f"Module-level access to {name} method"
5306+
return wrapper
5307+
5308+
# Set the function in the module's globals
5309+
globals()[method_name] = make_wrapper()
52725310

52735311

5274-
def get_alerts(*args, **kwargs):
5275-
"""Module-level access to get_alerts method"""
5276-
return _delegate_to_instance('get_alerts', *args, **kwargs)
5312+
# Automatically set up all module-level methods
5313+
_setup_all_module_methods()
52775314

52785315

52795316
# Backward compatibility: Create a submodule-like structure

0 commit comments

Comments
 (0)