@@ -74,49 +74,18 @@ def __repr__(self):
7474 return '<Bugzilla Token Cache :: %s>' % self .value
7575
7676
77- class _BugzillaXMLRPCProxy (ServerProxy , object ):
78- def __init__ (self , uri , tokenfile , * args , ** kwargs ):
79- ServerProxy .__init__ (self , uri , * args , ** kwargs )
80- self .token_cache = _BugzillaTokenCache (uri , tokenfile )
81- self .api_key = None
82-
83- def use_api_key (self , api_key ):
84- self .api_key = api_key
85-
86- def clear_token (self ):
87- self .token_cache .value = None
88-
89- def _ServerProxy__request (self , methodname , params ):
90- if len (params ) == 0 :
91- params = ({}, )
92-
93- log .debug ("XMLRPC call: %s(%s)" , methodname , params [0 ])
94-
95- if self .api_key is not None :
96- if 'Bugzilla_api_key' not in params [0 ]:
97- params [0 ]['Bugzilla_api_key' ] = self .api_key
98- elif self .token_cache .value is not None :
99- if 'Bugzilla_token' not in params [0 ]:
100- params [0 ]['Bugzilla_token' ] = self .token_cache .value
101-
102- # pylint: disable=no-member
103- ret = ServerProxy ._ServerProxy__request (self , methodname , params )
104- # pylint: enable=no-member
105-
106- if isinstance (ret , dict ) and 'token' in ret .keys ():
107- self .token_cache .value = ret .get ('token' )
108- return ret
109-
110-
11177class _BugzillaSession (object ):
11278 """
11379 Class to handle the backend agnostic 'requests' setup
11480 """
11581 def __init__ (self , url , user_agent ,
116- cookiejar = None , sslverify = True , sslcafile = None , cert = None ):
82+ cookiejar = None , sslverify = True , sslcafile = None , cert = None ,
83+ tokenfile = None , api_key = None ):
11784 self ._user_agent = user_agent
11885 self ._scheme = urlparse (url )[0 ]
11986 self ._cookiejar = cookiejar
87+ self ._token_cache = _BugzillaTokenCache (url , tokenfile )
88+ self ._api_key = api_key
12089
12190 if self ._scheme not in ["http" , "https" ]:
12291 raise Exception ("Invalid URL scheme: %s (%s)" % (
@@ -143,6 +112,10 @@ def get_user_agent(self):
143112 return self ._user_agent
144113 def get_scheme (self ):
145114 return self ._scheme
115+ def get_api_key (self ):
116+ return self ._api_key
117+ def get_token_cache (self ):
118+ return self ._token_cache
146119
147120 def set_basic_auth (self , user , password ):
148121 """
@@ -171,11 +144,11 @@ def post(self, url, data):
171144
172145
173146class _BugzillaXMLRPCTransport (Transport ):
174- def __init__ (self , * args , ** kwargs ):
147+ def __init__ (self , bugzillasession ):
175148 if hasattr (Transport , "__init__" ):
176149 Transport .__init__ (self , use_datetime = False )
177150
178- self .__bugzillasession = _BugzillaSession ( * args , ** kwargs )
151+ self .__bugzillasession = bugzillasession
179152 self .__seen_valid_xml = False
180153
181154 # Override Transport.user_agent
@@ -220,9 +193,6 @@ def __request_helper(self, url, request_body):
220193 # pylint: enable=attribute-defined-outside-init
221194 raise e
222195
223- def set_basic_auth (self , user , password ):
224- self .__bugzillasession .set_basic_auth (user , password )
225-
226196
227197 ######################
228198 # Tranport overrides #
@@ -259,3 +229,40 @@ def request(self, host, handler, request_body, verbose=0):
259229 request_body = request_body .replace (b'\r ' , b'
' )
260230
261231 return self .__request_helper (url , request_body )
232+
233+
234+ class _BugzillaXMLRPCProxy (ServerProxy , object ):
235+ """
236+ Override of xmlrpc ServerProxy, to insert bugzilla API auth
237+ into the XMLRPC request data
238+ """
239+ def __init__ (self , uri , bugzillasession , * args , ** kwargs ):
240+ self .__bugzillasession = bugzillasession
241+ transport = _BugzillaXMLRPCTransport (self .__bugzillasession )
242+ ServerProxy .__init__ (self , uri , transport , * args , ** kwargs )
243+
244+ def _ServerProxy__request (self , methodname , params ):
245+ """
246+ Overrides ServerProxy _request method
247+ """
248+ if len (params ) == 0 :
249+ params = ({}, )
250+
251+ log .debug ("XMLRPC call: %s(%s)" , methodname , params [0 ])
252+ api_key = self .__bugzillasession .get_api_key ()
253+ token_cache = self .__bugzillasession .get_token_cache ()
254+
255+ if api_key is not None :
256+ if 'Bugzilla_api_key' not in params [0 ]:
257+ params [0 ]['Bugzilla_api_key' ] = api_key
258+ elif token_cache .value is not None :
259+ if 'Bugzilla_token' not in params [0 ]:
260+ params [0 ]['Bugzilla_token' ] = token_cache .value
261+
262+ # pylint: disable=no-member
263+ ret = ServerProxy ._ServerProxy__request (self , methodname , params )
264+ # pylint: enable=no-member
265+
266+ if isinstance (ret , dict ) and 'token' in ret .keys ():
267+ token_cache .value = ret .get ('token' )
268+ return ret
0 commit comments