Skip to content

Instantly share code, notes, and snippets.

@sigmavirus24
Created July 16, 2012 15:51
Show Gist options
  • Save sigmavirus24/3123460 to your computer and use it in GitHub Desktop.
Save sigmavirus24/3123460 to your computer and use it in GitHub Desktop.
Example for #python
from requests import session
class requires_auth(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
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