Ok I have had a problem expressing my problems with the code I am working on without dumping a ton of code; so here is what it would be synchronously (instead of asking it from the view of it being async).
Also for classes when should a variable be accessed through a method argument and when should it be accessed through a instance variable?
Synchronously it would look like so... Note: the actual server urls and parsing are different but just complicate things. Also in the following example the get_token method takes the session as a parameter, should it instead get the session by using self.session instead?
import urllib
import time
class SyncExampleClass(object):
def __init__(self):
self.session = None
self.token = None
self.session_time = -1
def get_session(self):
s = urllib.urlopen("http://example.com/session/").read()
self.session_time = int(time.time())
return s
def get_token(self, session):
t = urllib.urlopen("http://example.com/token/?session=%s" % session).read()
return t
def construct_api_call(self, api_method):
# if the session is over an hour old (is that the correct sign?)
if time.time() - 3600 > self.session_time or self.session is None:
self.session = get_session()
self.token = get_token(self.session)
call = urllib.urlopen("http://example.com/api/?method=%s%session=%s&token=%s" % (api_method, self.session, self.token) ).read()
return call