Skip to content

Instantly share code, notes, and snippets.

@bhearsum
Created May 12, 2015 13:20
Show Gist options
  • Save bhearsum/526d8ad8271e527f086d to your computer and use it in GitHub Desktop.
Save bhearsum/526d8ad8271e527f086d to your computer and use it in GitHub Desktop.
➜ tmp cat test.py
#!/usr/bin/env python
class BugzillaClient(object):
def configure(self, bzurl, username=None, password=None, apikey=None):
if apikey:
self.apikey = apikey
self.username = None
self.password = None
if username or password:
raise ValueError("Cannot use apikey along with user-based login")
elif username and password:
self.apikey = None
self.username = username
self.password = password
else:
print ValueError("One of apikey or username & password must be supplied")
print self.apikey
print self.username
print self.password
class BugzillaClient2(object):
def configure(self, bzurl, **kwargs):
if not set(kwargs.keys()).issubset(set(["password", "username", "apikey"])):
raise ValueError("Invalid arguments passed to BugzillaClient.configure")
if "apikey" in kwargs:
self.apikey = kwargs["apikey"]
self.username = None
self.password = None
if kwargs.get("username", None) or kwargs.get("password", None):
raise ValueError("Cannot use apikey along with user-based login")
elif "username" in kwargs and "password" in kwargs:
self.apikey = None
self.username = kwargs["username"]
self.password = kwargs["password"]
else:
print ValueError("One of apikey or username & password must be supplied")
print self.apikey
print self.username
print self.password
before = BugzillaClient()
after = BugzillaClient()
print "Before, apikey:"
before.configure("bz", apikey="foo")
print
print "Before, username/password:"
before.configure("bz", username="foo", password="bar")
print
print "Before, no auth:"
before.configure("bz")
print
print "After, apikey:"
after.configure("bz", apikey="foo")
print
print "After, username/password:"
after.configure("bz", username="foo", password="bar")
print
print "Aefore, no auth:"
after.configure("bz")
➜ tmp python test.py
Before, apikey:
foo
None
None
Before, username/password:
None
foo
bar
Before, no auth:
One of apikey or username & password must be supplied
None
foo
bar
After, apikey:
foo
None
None
After, username/password:
None
foo
bar
Aefore, no auth:
One of apikey or username & password must be supplied
None
foo
bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment