1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-05-29 06:39:32 +03:00

Update to the 2.4 cAPI

This commit is contained in:
Jonathan Harris 2017-09-13 14:16:33 +01:00
parent b4a1012334
commit 302ce0b637
4 changed files with 37 additions and 16 deletions

View File

@ -122,7 +122,7 @@ try:
else: # <= 2.25 not yet migrated
session.login(config.get('username'), config.get('password'), monitor.is_beta)
querytime = int(time())
data = session.query()
data = session.station()
config.set('querytime', querytime)
# Validation
@ -198,10 +198,10 @@ try:
else:
sys.stderr.write("Station doesn't supply outfitting\n")
if (args.s or args.n) and not args.j and not data['lastStarport'].get('ships') and monitor.stationservices and 'Shipyard' in monitor.stationservices:
if (args.s or args.n) and not args.j and not data['lastStarport'].get('ships') and data['lastStarport']['services'].get('shipyard'):
# Retry for shipyard
sleep(SERVER_RETRY)
data2 = session.query()
data2 = session.station()
if (data2['commander'].get('docked') and # might have undocked while we were waiting for retry in which case station data is unreliable
data2['lastSystem']['name'] == monitor.system and
data2['lastStarport']['name'] == monitor.station):

View File

@ -294,7 +294,7 @@ class AppWindow:
if not config.get('cmdrs') and config.get('username') and config.get('password'):
try:
self.session.login(config.get('username'), config.get('password'), False)
data = self.session.query()
data = self.session.profile()
prefs.migrate(data['commander']['name'])
except:
if __debug__: print_exc()
@ -428,7 +428,7 @@ class AppWindow:
try:
querytime = int(time())
data = self.session.query()
data = self.session.station()
config.set('querytime', querytime)
# Validation
@ -514,12 +514,9 @@ class AppWindow:
self.eddn.export_outfitting(data, monitor.is_beta)
if data['lastStarport'].get('ships'):
self.eddn.export_shipyard(data, monitor.is_beta)
elif monitor.stationservices is not None and 'Shipyard' in monitor.stationservices:
elif data['lastStarport']['services'].get('shipyard'):
# API is flakey about shipyard info - silently retry if missing (<1s is usually sufficient - 5s for margin).
self.w.after(int(SERVER_RETRY * 1000), lambda:self.retry_for_shipyard(2))
elif monitor.stationservices is None and monitor.stationtype != 'Outpost':
# Pre E:D 2.4 we don't know if we should have shipyard info. Retry once.
self.w.after(int(SERVER_RETRY * 1000), lambda:self.retry_for_shipyard(1))
if not old_status:
self.status['text'] = ''
@ -557,7 +554,7 @@ class AppWindow:
def retry_for_shipyard(self, tries):
# Try again to get shipyard data and send to EDDN. Don't report errors if can't get or send the data.
try:
data = self.session.query()
data = self.session.station()
if __debug__:
print 'Retry for shipyard - ' + (data['commander'].get('docked') and (data['lastStarport'].get('ships') and 'Success' or 'Failure') or 'Undocked!')
if not data['commander'].get('docked'):
@ -741,7 +738,7 @@ class AppWindow:
self.status['text'] = _('Fetching data...')
self.w.update_idletasks()
try:
data = self.session.query()
data = self.session.profile()
except companion.VerificationRequired:
return prefs.AuthenticationDialog(self.w, partial(self.verify, self.shipyard_url))
except companion.ServerError as e:
@ -802,7 +799,7 @@ class AppWindow:
self.w.update_idletasks()
try:
data = self.session.query()
data = self.session.station()
self.status['text'] = ''
f = tkFileDialog.asksaveasfilename(parent = self.w,
defaultextension = platform=='darwin' and '.json' or '',

View File

@ -21,6 +21,8 @@ SERVER_BETA = 'https://pts-companion.orerve.net'
URL_LOGIN = '/user/login'
URL_CONFIRM = '/user/confirm'
URL_QUERY = '/profile'
URL_MARKET = '/market'
URL_SHIPYARD= '/shipyard'
# Map values reported by the Companion interface to names displayed in-game
@ -205,7 +207,7 @@ class Session:
self.session.cookies.save() # Save cookies now for use by command-line app
self.login()
def query(self):
def query(self, endpoint):
if self.state == Session.STATE_NONE:
raise Exception('General error') # Shouldn't happen - don't bother localizing
elif self.state == Session.STATE_INIT:
@ -213,7 +215,7 @@ class Session:
elif self.state == Session.STATE_AUTH:
raise VerificationRequired()
try:
r = self.session.get(self.server + URL_QUERY, timeout=timeout)
r = self.session.get(self.server + endpoint, timeout=timeout)
except:
if __debug__: print_exc()
raise ServerError()
@ -221,7 +223,7 @@ class Session:
if r.status_code == requests.codes.forbidden or r.url == self.server + URL_LOGIN:
# Start again - maybe our session cookie expired?
self.state = Session.STATE_INIT
return self.query()
return self.query(endpoint)
if r.status_code != requests.codes.ok:
self.dump(r)
@ -235,6 +237,28 @@ class Session:
return data
def profile(self):
return self.query(URL_QUERY)
def station(self):
data = self.query(URL_QUERY)
if data.get('docked'):
if data['lastStarport']['services'].get('commodities'):
marketdata = self.query(URL_MARKET)
if (data['lastStarport']['name'] != marketdata['name'] or
data['lastStarport']['id'] != marketdata['id']):
raise ServerLagging()
else:
data['lastStarport'].update(marketdata)
if data['lastStarport']['services'].get('outfitting') or data['lastStarport']['services'].get('shipyard'):
shipdata = self.query(URL_SHIPYARD)
if (data['lastStarport']['name'] != shipdata['name'] or
data['lastStarport']['id'] != shipdata['id']):
raise ServerLagging()
else:
data['lastStarport'].update(shipdata)
return data
def close(self):
self.state = Session.STATE_NONE
if self.session:

View File

@ -187,7 +187,7 @@ class StatsDialog():
self.parent.update_idletasks()
try:
data = self.session.query()
data = self.session.profile()
except companion.VerificationRequired:
return prefs.AuthenticationDialog(self.parent, partial(self.verify, self.showstats))
except companion.ServerError as e: