@@ -55,6 +55,7 @@ def __init__(
55
55
session : ClientSession = None ,
56
56
ssl = False ,
57
57
verify_ssl = True ,
58
+ keep_keys = False ,
58
59
):
59
60
"""
60
61
Create a SagemCom client.
@@ -69,6 +70,7 @@ def __init__(
69
70
self .username = username
70
71
self .authentication_method = authentication_method
71
72
self ._password_hash = self .__generate_hash (password )
73
+ self .keep_keys = keep_keys
72
74
73
75
self .protocol = "https" if ssl else "http"
74
76
@@ -156,15 +158,16 @@ def __get_response(self, response, index=0):
156
158
157
159
return value
158
160
159
- def __get_response_value (self , response , index = 0 ):
161
+ def __get_response_value (self , response , index = 0 , keep_keys = None ):
160
162
"""Retrieve response value from value."""
161
163
try :
162
164
value = self .__get_response (response , index )["value" ]
163
165
except KeyError :
164
166
value = None
165
167
166
168
# Rewrite result to snake_case
167
- value = humps .decamelize (value )
169
+ if (keep_keys is not None and not keep_keys ) or (keep_keys is None and not self .keep_keys ):
170
+ value = humps .decamelize (value )
168
171
169
172
return value
170
173
@@ -293,7 +296,7 @@ async def logout(self):
293
296
self ._request_id = - 1
294
297
295
298
async def get_value_by_xpath (
296
- self , xpath : str , options : Optional [Dict ] = {}
299
+ self , xpath : str , options : Optional [Dict ] = {}, keep_keys = None
297
300
) -> Dict :
298
301
"""
299
302
Retrieve raw value from router using XPath.
@@ -309,11 +312,11 @@ async def get_value_by_xpath(
309
312
}
310
313
311
314
response = await self .__api_request_async ([actions ], False )
312
- data = self .__get_response_value (response )
315
+ data = self .__get_response_value (response , keep_keys = keep_keys )
313
316
314
317
return data
315
318
316
- async def get_values_by_xpaths (self , xpaths , options : Optional [Dict ] = {}) -> Dict :
319
+ async def get_values_by_xpaths (self , xpaths , options : Optional [Dict ] = {}, keep_keys = None ) -> Dict :
317
320
"""
318
321
Retrieve raw values from router using XPath.
319
322
@@ -331,7 +334,7 @@ async def get_values_by_xpaths(self, xpaths, options: Optional[Dict] = {}) -> Di
331
334
]
332
335
333
336
response = await self .__api_request_async (actions , False )
334
- values = [self .__get_response_value (response , i ) for i in range (len (xpaths ))]
337
+ values = [self .__get_response_value (response , i , keep_keys = keep_keys ) for i in range (len (xpaths ))]
335
338
data = dict (zip (xpaths .keys (), values ))
336
339
337
340
return data
@@ -349,7 +352,7 @@ async def set_value_by_xpath(
349
352
actions = {
350
353
"id" : 0 ,
351
354
"method" : "setValue" ,
352
- "xpath" : xpath ,
355
+ "xpath" : urllib . parse . quote ( xpath ) ,
353
356
"parameters" : {"value" : str (value )},
354
357
"options" : options ,
355
358
}
@@ -361,7 +364,7 @@ async def set_value_by_xpath(
361
364
async def get_device_info (self ) -> DeviceInfo :
362
365
"""Retrieve information about Sagemcom F@st device."""
363
366
try :
364
- data = await self .get_value_by_xpath ("Device/DeviceInfo" )
367
+ data = await self .get_value_by_xpath ("Device/DeviceInfo" , keep_keys = False )
365
368
return DeviceInfo (** data .get ("device_info" ))
366
369
except UnknownPathException :
367
370
data = await self .get_values_by_xpaths (
@@ -380,7 +383,7 @@ async def get_device_info(self) -> DeviceInfo:
380
383
381
384
async def get_hosts (self , only_active : Optional [bool ] = False ) -> List [Device ]:
382
385
"""Retrieve hosts connected to Sagemcom F@st device."""
383
- data = await self .get_value_by_xpath ("Device/Hosts/Hosts" )
386
+ data = await self .get_value_by_xpath ("Device/Hosts/Hosts" , keep_keys = False )
384
387
devices = [Device (** d ) for d in data ]
385
388
386
389
if only_active :
@@ -391,7 +394,7 @@ async def get_hosts(self, only_active: Optional[bool] = False) -> List[Device]:
391
394
392
395
async def get_port_mappings (self ) -> List [PortMapping ]:
393
396
"""Retrieve configured Port Mappings on Sagemcom F@st device."""
394
- data = await self .get_value_by_xpath ("Device/NAT/PortMappings" )
397
+ data = await self .get_value_by_xpath ("Device/NAT/PortMappings" , keep_keys = False )
395
398
port_mappings = [PortMapping (** p ) for p in data ]
396
399
397
400
return port_mappings
@@ -405,6 +408,6 @@ async def reboot(self):
405
408
}
406
409
407
410
response = await self .__api_request_async ([action ], False )
408
- data = self .__get_response_value (response )
411
+ data = self .__get_response_value (response , keep_keys = False )
409
412
410
413
return data
0 commit comments