#NEWLINE# def _req_json_rpc( url , method , * args , **kwargs ) : #NEWLINE# #INDENT# 'Perform one JSON RPC operation.' #NEWLINE# data = json . dumps ( { #NEWLINE# #INDENT# 'method' : method , #NEWLINE# 'params' : args , #NEWLINE# #UNINDENT# } ) #NEWLINE# try : #NEWLINE# #INDENT# res = requests . post ( url , data = data , timeout = 5 , ** kwargs ) #NEWLINE# #UNINDENT# except requests . exceptions . Timeout : #NEWLINE# #INDENT# _LOGGER . exception ( 'Connection to the router timed out' ) #NEWLINE# return #NEWLINE# #UNINDENT# if ( res . status_code == 200 ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# result = res . json ( ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# _LOGGER . exception ( 'Failed to parse response from luci' ) #NEWLINE# return #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# return result [ 'result' ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# _LOGGER . exception ( 'No result in response from luci' ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( res . status_code == 401 ) : #NEWLINE# #INDENT# _LOGGER . exception ( 'Failed to authenticate, please check your username and password' ) #NEWLINE# return #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# _LOGGER . error ( 'Invalid response from luci: %s' , res )