mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 06:42:16 +00:00
Cleanup of API code, DRY principle.
This commit is contained in:
@@ -32,10 +32,6 @@ class Inbox():
|
|||||||
REDDIT_API_COMPOSE = "%s/api/compose/" % REDDIT
|
REDDIT_API_COMPOSE = "%s/api/compose/" % REDDIT
|
||||||
|
|
||||||
def __init__(self, username=None, password=None):
|
def __init__(self, username=None, password=None):
|
||||||
|
|
||||||
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
|
|
||||||
urllib2.install_opener(self.opener)
|
|
||||||
|
|
||||||
if username and password:
|
if username and password:
|
||||||
self.login(username, password)
|
self.login(username, password)
|
||||||
|
|
||||||
@@ -43,17 +39,15 @@ class Inbox():
|
|||||||
data = { 'user': username,
|
data = { 'user': username,
|
||||||
'passwd': password,
|
'passwd': password,
|
||||||
'api_type': 'json' }
|
'api_type': 'json' }
|
||||||
|
|
||||||
|
|
||||||
url = "%s/%s" % (self.REDDIT_API_LOGIN, username)
|
url = "%s/%s" % (self.REDDIT_API_LOGIN, username)
|
||||||
req = urllib2.Request( url, urllib.urlencode(data))
|
|
||||||
jsondoc = json.load(self.opener.open(req))
|
jsondoc = json.load(self._url_request(url, data))
|
||||||
|
|
||||||
self.login_cookie = jsondoc['json']['data']['cookie']
|
self.login_cookie = jsondoc['json']['data']['cookie']
|
||||||
self.modhash = jsondoc['json']['data']['modhash']
|
self.modhash = jsondoc['json']['data']['modhash']
|
||||||
|
|
||||||
if self.login_cookie:
|
if self.login_cookie:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -69,8 +63,12 @@ class Inbox():
|
|||||||
return self.__inbox_cache
|
return self.__inbox_cache
|
||||||
|
|
||||||
def _url_request(self, url, data):
|
def _url_request(self, url, data):
|
||||||
|
if not hasattr(self, '_opener'):
|
||||||
|
self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
|
||||||
|
urllib2.install_opener(self._opener)
|
||||||
|
|
||||||
req = urllib2.Request( url, urllib.urlencode(data))
|
req = urllib2.Request( url, urllib.urlencode(data))
|
||||||
return self.opener.open(req)
|
return self._opener.open(req)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self._inbox_data)
|
return len(self._inbox_data)
|
||||||
|
|||||||
Reference in New Issue
Block a user