@@ -58,13 +58,7 @@ def getPOSTData(self, cmd):
58
58
if not isinstance (cmd , str ):
59
59
for key in sorted (cmd .keys ()):
60
60
if (cmd [key ] is not None ):
61
- if isinstance (cmd [key ], list ):
62
- i = 0
63
- while i < len (cmd [key ]):
64
- tmp += ("{0}{1}={2}\n " ).format (key , i , re .sub ('[\r \n ]' , '' , str (cmd [key ][i ])))
65
- i += 1
66
- else :
67
- tmp += ("{0}={1}\n " ).format (key , re .sub ('[\r \n ]' , '' , str (cmd [key ])))
61
+ tmp += ("{0}={1}\n " ).format (key , re .sub ('[\r \n ]' , '' , str (cmd [key ])))
68
62
return ("{0}{1}={2}" ).format (data , quote ('s_command' ), quote (re .sub ('\n $' , '' , tmp )))
69
63
70
64
def getSession (self ):
@@ -213,7 +207,13 @@ def request(self, cmd):
213
207
"""
214
208
Perform API request using the given command
215
209
"""
216
- data = self .getPOSTData (cmd ).encode ('UTF-8' )
210
+ # flatten nested api command bulk parameters
211
+ newcmd = self .__flattenCommand (cmd )
212
+ # auto convert umlaut names to punycode
213
+ newcmd = self .__autoIDNConvert (newcmd )
214
+
215
+ # request command to API
216
+ data = self .getPOSTData (newcmd ).encode ('UTF-8' )
217
217
# TODO: 300s (to be sure to get an API response)
218
218
try :
219
219
req = Request (self .__socketURL , data , {
@@ -226,19 +226,19 @@ def request(self, cmd):
226
226
body = rtm .getTemplate ("httperror" ).getPlain ()
227
227
if (self .__debugMode ):
228
228
print ((self .__socketURL , data , "HTTP communication failed" , body , '\n ' , '\n ' ))
229
- return Response (body , cmd )
229
+ return Response (body , newcmd )
230
230
231
231
def requestNextResponsePage (self , rr ):
232
232
"""
233
233
Request the next page of list entries for the current list query
234
234
Useful for tables
235
235
"""
236
- mycmd = self . __toUpperCaseKeys ( rr .getCommand () )
236
+ mycmd = rr .getCommand ()
237
237
if ("LAST" in mycmd ):
238
238
raise Exception ("Parameter LAST in use. Please remove it to avoid issues in requestNextPage." )
239
239
first = 0
240
240
if ("FIRST" in mycmd ):
241
- first = mycmd ["FIRST" ]
241
+ first = int ( mycmd ["FIRST" ])
242
242
total = rr .getRecordsTotalCount ()
243
243
limit = rr .getRecordsLimitation ()
244
244
first += limit
@@ -293,11 +293,54 @@ def useLIVESystem(self):
293
293
self .__socketConfig .setSystemEntity ("54cd" )
294
294
return self
295
295
296
- def __toUpperCaseKeys (self , cmd ):
296
+ def __flattenCommand (self , cmd ):
297
297
"""
298
- Translate all command parameter names to uppercase
298
+ Flatten API command to handle it easier later on (nested array for bulk params)
299
299
"""
300
300
newcmd = {}
301
- for k in list (cmd .keys ()):
302
- newcmd [k .upper ()] = cmd [k ]
301
+ for key in list (cmd .keys ()):
302
+ newKey = key .upper ()
303
+ val = cmd [key ]
304
+ if val is None :
305
+ continue
306
+ if isinstance (val , list ):
307
+ i = 0
308
+ while i < len (val ):
309
+ newcmd [newKey + str (i )] = re .sub (r'[\r\n]' , '' , str (val [i ]))
310
+ i += 1
311
+ else :
312
+ newcmd [newKey ] = re .sub (r'[\r\n]' , '' , str (val ))
303
313
return newcmd
314
+
315
+ def __autoIDNConvert (self , cmd ):
316
+ """
317
+ Auto convert API command parameters to punycode, if necessary.
318
+ """
319
+ # don't convert for convertidn command to avoid endless loop
320
+ # and ignore commands in string format(even deprecated)
321
+ if isinstance (cmd , str ) or re .match (r'^CONVERTIDN$' , cmd ["COMMAND" ], re .IGNORECASE ):
322
+ return cmd
323
+
324
+ toconvert = []
325
+ keys = []
326
+ for key in cmd :
327
+ if re .match (r'^(DOMAIN|NAMESERVER|DNSZONE)([0-9]*)$' , key , re .IGNORECASE ):
328
+ keys .append (key )
329
+ if not keys .count :
330
+ return cmd
331
+ idxs = []
332
+ for key in keys :
333
+ if not re .match (r'^[a-z0-9.-]+$' , cmd [key ], re .IGNORECASE ):
334
+ toconvert .append (cmd [key ])
335
+ idxs .append (key )
336
+
337
+ r = self .request ({
338
+ "COMMAND" : "ConvertIDN" ,
339
+ "DOMAIN" : toconvert
340
+ })
341
+ if r .isSuccess ():
342
+ col = r .getColumn ("ACE" )
343
+ if col is not None :
344
+ for idx , pc in enumerate (col .getData ()):
345
+ cmd [idxs [idx ]] = pc
346
+ return cmd
0 commit comments