Skip to content

Commit f3a1ba9

Browse files
committed
transport: Move all auth data into _BugzillaSession
The XMLRPC bits now interact with _BugzillaSession as the primary API object. XMLRPCTransport is now an implementation detail of XMLRPCProxy. The Bugzilla base class now instantiates Session directly. This will make it easier to handle XMLRPC vs REST Signed-off-by: Cole Robinson <crobinso@redhat.com>
1 parent c3bcc3e commit f3a1ba9

2 files changed

Lines changed: 58 additions & 51 deletions

File tree

bugzilla/base.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
from .apiversion import __version__
4040
from .bug import Bug, User
4141
from .transport import (BugzillaError,
42-
_BugzillaXMLRPCProxy,
43-
_BugzillaXMLRPCTransport)
42+
_BugzillaSession,
43+
_BugzillaXMLRPCProxy)
4444

4545

4646
log = getLogger(__name__)
@@ -299,7 +299,7 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
299299
self.url = ''
300300

301301
self._proxy = None
302-
self._transport = None
302+
self._session = None
303303
self._cookiejar = None
304304
self._sslverify = sslverify
305305
self._cache = _BugzillaAPICache()
@@ -546,19 +546,20 @@ def connect(self, url=None):
546546
If 'user' and 'password' are both set, we'll run login(). Otherwise
547547
you'll have to login() yourself before some methods will work.
548548
"""
549-
if self._transport:
549+
if self._session:
550550
self.disconnect()
551551

552552
if url is None and self.url:
553553
url = self.url
554554
url = self.fix_url(url)
555555

556-
self._transport = _BugzillaXMLRPCTransport(url, self.user_agent,
556+
self._session = _BugzillaSession(url, self.user_agent,
557557
cookiejar=self._cookiejar,
558558
sslverify=self._sslverify,
559-
cert=self.cert)
560-
self._proxy = _BugzillaXMLRPCProxy(url, self.tokenfile,
561-
self._transport)
559+
cert=self.cert,
560+
tokenfile=self.tokenfile,
561+
api_key=self.api_key)
562+
self._proxy = _BugzillaXMLRPCProxy(url, self._session)
562563

563564
self.url = url
564565
# we've changed URLs - reload config
@@ -570,7 +571,6 @@ def connect(self, url=None):
570571

571572
if self.api_key:
572573
log.debug("using API key")
573-
self._proxy.use_api_key(self.api_key)
574574

575575
version = self._proxy.Bugzilla.version()["version"]
576576
log.debug("Bugzilla version string: %s", version)
@@ -581,15 +581,15 @@ def disconnect(self):
581581
Disconnect from the given bugzilla instance.
582582
"""
583583
self._proxy = None
584-
self._transport = None
584+
self._session = None
585585
self._cache = _BugzillaAPICache()
586586

587587
def _login(self, user, password, restrict_login=None):
588588
"""
589589
Backend login method for Bugzilla3
590590
"""
591591
if self._basic_auth:
592-
self._transport.set_basic_auth(user, password)
592+
self._session.set_basic_auth(user, password)
593593

594594
payload = {'login': user, 'password': password}
595595
if restrict_login:

bugzilla/transport.py

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
11177
class _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

173146
class _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'&#xd;')
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

Comments
 (0)