Skip to content

Instantly share code, notes, and snippets.

@sigmavirus24
Created July 16, 2012 16:07
Show Gist options
  • Save sigmavirus24/3123541 to your computer and use it in GitHub Desktop.
Save sigmavirus24/3123541 to your computer and use it in GitHub Desktop.
Example for #python
from requests import session
def requires_auth(method):
def wrapper(self, *args, **kwargs):
# check for auth params
if hasattr(self, 'auth_information'):
return method(self, *args, **kwargs)
else:
raise AuthRequiredException
return wrapper
class API(object):
def __init__(self, login='', passwd=''):
if login and password:
self.s = session(auth=(login, password))
else:
self.s = session()
@requires_auth
def user(self):
return self.s.get('https://api.github.com/user')
anon = API()
anon.user() # if written properly, the call would raise an error before trying to make the request
auth = API(username, passwd)
auth.user() # would return the completed request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment