get_scheduled_passes_from_network: Fix next_url logic

The observations endpoint is paginated and ordered by observation
start time, i.e. most recent / future observations come at first.

The `get_scheduled_passes_from_network` method fetches the first page,
parses it, and then fetches new pages as long as the last observation
is still before the user-provided pass scheduling end time.

"Established" stations have plenty of past observations, so this pass
scheduling end time will be reached before the endpoint runs out of
observations (and thus stops to provide a `links['next']['url']` header.

For "young" stations the endpoint will run out of observations though
and the old `get_scheduled_passes_from_network` implementation didn't
detect the missing header and thus failed as described in issue #15.

This commit introduces logic to detect a missing link header.

Fixes issue #15.
merge-requests/53/head
Fabian P. Schmidt 2019-11-02 23:17:57 +01:00
parent 3e69cfdcd7
commit 0ae102d19b
1 changed files with 10 additions and 8 deletions

View File

@ -67,6 +67,8 @@ def get_scheduled_passes_from_network(ground_station, tmin, tmax):
# Loop
start = True
next_url = '{}/api/observations/?ground_station={:d}'.format(
settings.NETWORK_BASE_URL, ground_station)
scheduledpasses = []
logging.info("Requesting scheduled passes for ground station %d" % ground_station)
@ -74,17 +76,17 @@ def get_scheduled_passes_from_network(ground_station, tmin, tmax):
# before the start time of the selected timerange for scheduling
# NOTE: This algorithm is based on the order in which the API returns the observations, i.e.
# most recent observations are returned at first!
while True:
if start:
r = client.get('{}/api/observations/?ground_station={:d}'.format(
settings.NETWORK_BASE_URL, ground_station))
start = False
while next_url:
r = client.get(next_url)
if 'next' in r.links:
next_url = r.links['next']['url']
else:
nextpage = r.links.get("next")
r = client.get(nextpage["url"])
logging.debug("No further pages with observations")
next_url = None
if not r.json():
# Ground station has no observations yet
logging.info("Ground station has no observations yet")
break
# r.json() is a list of dicts/observations