Skip to content

Commit

Permalink
Hande companion API session timing out more gracefully.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marginal committed Jul 28, 2015
1 parent 47a00fb commit 5ff1c84
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions companion.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
holdoff = 120 # be nice
timeout = 10 # requests timeout

URL_LOGIN = 'https://companion.orerve.net/user/login'
URL_CONFIRM = 'https://companion.orerve.net/user/confirm'
URL_QUERY = 'https://companion.orerve.net/profile'


# Map values reported by the Companion interface to names displayed in-game and recognized by trade tools

Expand Down Expand Up @@ -137,7 +141,7 @@ def login(self, username=None, password=None):
else:
self.credentials = { 'email' : username, 'password' : password }
try:
r = self.session.post('https://companion.orerve.net/user/login', data = self.credentials, timeout=timeout)
r = self.session.post(URL_LOGIN, data = self.credentials, timeout=timeout)
except:
if __debug__: print_exc()
raise ServerError()
Expand All @@ -160,7 +164,7 @@ def login(self, username=None, password=None):
return r.status_code

def verify(self, code):
r = self.session.post('https://companion.orerve.net/user/confirm', data = {'code' : code}, timeout=timeout)
r = self.session.post(URL_CONFIRM, data = {'code' : code}, timeout=timeout)
r.raise_for_status()
# verification doesn't actually return a yes/no, so log in again to determine state
try:
Expand All @@ -176,17 +180,17 @@ def query(self):
elif self.state == Session.STATE_AUTH:
raise VerificationRequired()
try:
r = self.session.get('https://companion.orerve.net/profile', timeout=timeout)
r = self.session.get(URL_QUERY, timeout=timeout)
except:
if __debug__: print_exc()
raise ServerError()

if r.status_code != requests.codes.ok:
self.dump(r)
if r.status_code == requests.codes.forbidden:
if r.status_code == requests.codes.forbidden or (r.history and r.url == URL_LOGIN):
# Start again - maybe our session cookie expired?
self.login()
self.query()
return self.query()

r.raise_for_status()
try:
Expand Down

0 comments on commit 5ff1c84

Please sign in to comment.