Created
July 16, 2012 16:07
-
-
Save sigmavirus24/3123541 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 | |
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