From 7dbbd954b2a2580e1a768af51a143039cc5e9255 Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Tue, 11 May 2010 01:19:17 -0400 Subject: [PATCH] Changing verifyCredentials to not auto-fire on class instantiation. No need to waste network resources; if anybody *wants* to verify credentials from this point onwards, you need to explicitly call instance.verifyCredentials(). This'll help with the upcoming change from Basic Auth... --- twython/core.py | 24 +++++++++++++++++++----- twython3k/core.py | 24 +++++++++++++++++++----- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/twython/core.py b/twython/core.py index dee5a6e..da5dcbc 100644 --- a/twython/core.py +++ b/twython/core.py @@ -93,11 +93,7 @@ class setup: self.opener = urllib2.build_opener(self.handler) if self.headers is not None: self.opener.addheaders = [('User-agent', self.headers)] - try: - simplejson.load(self.opener.open("http://api.twitter.com/%d/account/verify_credentials.json" % self.apiVersion)) - self.authenticated = True - except HTTPError, e: - raise AuthError("Authentication failed with your provided credentials. Try again? (%s failure)" % `e.code`) + self.authenticated = True # Play nice, people can force-check using verifyCredentials() else: # Build a non-auth opener so we can allow proxy-auth and/or header swapping if self.proxy is not None: @@ -124,7 +120,25 @@ class setup: def constructApiURL(self, base_url, params): return base_url + "?" + "&".join(["%s=%s" %(key, value) for (key, value) in params.iteritems()]) + + def verifyCredentials(self, version = None): + """ verifyCredentials(self, version = None): + Verifies the authenticity of the passed in credentials. Used to be a forced call, now made optional + (no need to waste network resources) + + Parameters: + None + """ + version = version or self.apiVersion + if self.authenticated is True: + try: + simplejson.load(self.opener.open("http://api.twitter.com/%d/account/verify_credentials.json" % version)) + except HTTPError, e: + raise AuthError("Authentication failed with your provided credentials. Try again? (%s failure)" % `e.code`) + else: + raise AuthError("verifyCredentials() requires you to actually, y'know, pass in credentials.") + def getRateLimitStatus(self, checkRequestingIP = True, version = None): """getRateLimitStatus() diff --git a/twython3k/core.py b/twython3k/core.py index 7be9f34..1f5cf79 100644 --- a/twython3k/core.py +++ b/twython3k/core.py @@ -93,11 +93,7 @@ class setup: self.opener = urllib.request.build_opener(self.handler) if self.headers is not None: self.opener.addheaders = [('User-agent', self.headers)] - try: - simplejson.load(self.opener.open("http://api.twitter.com/%d/account/verify_credentials.json" % self.apiVersion)) - self.authenticated = True - except HTTPError as e: - raise AuthError("Authentication failed with your provided credentials. Try again? (%s failure)" % repr(e.code)) + self.authenticated = True # Play nice, people can force-check using verifyCredentials() else: # Build a non-auth opener so we can allow proxy-auth and/or header swapping if self.proxy is not None: @@ -124,7 +120,25 @@ class setup: def constructApiURL(self, base_url, params): return base_url + "?" + "&".join(["%s=%s" %(key, value) for (key, value) in params.items()]) + + def verifyCredentials(self, version = None): + """ verifyCredentials(self, version = None): + Verifies the authenticity of the passed in credentials. Used to be a forced call, now made optional + (no need to waste network resources) + + Parameters: + None + """ + version = version or self.apiVersion + if self.authenticated is True: + try: + simplejson.load(self.opener.open("http://api.twitter.com/%d/account/verify_credentials.json" % version)) + except HTTPError as e: + raise AuthError("Authentication failed with your provided credentials. Try again? (%s failure)" % repr(e.code)) + else: + raise AuthError("verifyCredentials() requires you to actually, y'know, pass in credentials.") + def getRateLimitStatus(self, checkRequestingIP = True, version = None): """getRateLimitStatus()