Created
July 16, 2012 15:51
-
-
Save sigmavirus24/3123460 to your computer and use it in GitHub Desktop.
Example for #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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