FooTheBar
FooTheBar

Reputation: 857

HERE-API get for sequence optimization does not return

I'm trying to optimize the sequence of some waypoints with the here-api and the findsequence.json-API.

My code looks like this:

#! /usr/bin/python
# coding: utf8

from requests import Request, Session
import requests


class HereConector:
    app_id = "get_your_own_app_ip"
    app_code = "get_your_own_app_code"

    sequence_url = "https://wse.cit.api.here.com/2/findsequence.json"

    container_locations = [(u'Hamburg', [53.551086, 9.993682]), (u'Munich', [48.13715, 11.576124]), (u'Berlin', [52.520008, 13.404954]), (u'D', [51.514244, 7.468429])]

    def __init__(self):
        pass

    # small proof that the process can work
    def get_demo_route(self):
        url = "https://route.api.here.com/routing/7.2/calculateroute.json"
        # app_data = {"app_id": self.app_id, "app_code": self.app_code}
        app_data = {"waypoint0": "52.5,13.4", "waypoint1":"52.5,13.45", "mode": "fastest;car;traffic:disabled"}
        app_data['app_id'] = self.app_id
        app_data['app_code'] = self.app_code
        print (requests.get(url, app_data).json())


    def loc_to_waypoint(self, location):
        return str(location[1][0]) + ',' + str(location[1][1])

    def find_sequence(self):
        app_data = {"app_id": self.app_id, "app_code": self.app_code}
        app_data['start'] = self.loc_to_waypoint(self.container_locations[0])
        for i, cl in enumerate(self.container_locations):
            if i == 0:
                continue
            app_data['destination' + str(i)] = self.loc_to_waypoint(cl)
            if i == 10:
                break

        app_data['end'] = self.loc_to_waypoint(self.container_locations[-1])
        app_data['mode'] = 'truck;fastest'

        s = Session()
        req = Request('GET', url=self.sequence_url, params=app_data).prepare()

        # This URL can be copy/pasted into a browser and returns an optimized sequence
        print (req.url)

        # this call blocks
        r = s.send(req)

        # this call also blocks
        # r = requests.get(self.sequence_url, app_data)

        print (r.json())



if __name__ == "__main__":
    hc = HereConector()
    hc.get_demo_route()
    hc.find_sequence()

If I copy the generated URL into my browser, I get an optimized route immediately (processing time < 200ms) so the url and parameters should be right. However, the call to s.send (or requests.get(self.map_view_url, app_data)) blocks. (other calls e.g. to get maps work perfectly with requests).

This is also my first experiment with the requests-library so I don't know how to debug further.

Update: Code shows same behavior for python2.7 and python3

Upvotes: 0

Views: 187

Answers (1)

user3505695
user3505695

Reputation:

We tried executing your above code and see the below response using both s.send and requests.get calls. So the issue doesn't seem to be with the code. Could you please check your credentials and the respective number of requests you can make using it. It could be that you are exceeding your quota and hence the request is blocking. Try with different app id and code.

{'results': [{'waypoints': [{'id': 'start', 'lat': 53.551086, 'lng': 9.993682, 'sequence': 0, 'estimatedArrival': None, 'estimatedDeparture': None, 'fulfilledConstraints': []}, {'id': 'destination2', 'lat': 52.520008, 'lng': 13.404954, 'sequence': 1, 'estimatedArrival': None, 'estimatedDeparture': None, 'fulfilledConstraints': []}, {'id': 'destination1', 'lat': 48.13715, 'lng': 11.576124, 'sequence': 2, 'estimatedArrival': None, 'estimatedDeparture': None, 'fulfilledConstraints': []}, {'id': 'destination3', 'lat': 51.514244, 'lng': 7.468429, 'sequence': 3, 'estimatedArrival': None, 'estimatedDeparture': None, 'fulfilledConstraints': []}, {'id': 'end', 'lat': 51.514244, 'lng': 7.468429, 'sequence': 4, 'estimatedArrival': None, 'estimatedDeparture': None, 'fulfilledConstraints': []}], 'distance': '1484517', 'time': '68916', 'interconnections': [{'fromWaypoint': 'start', 'toWaypoint': 'destination2', 'distance': 289082.0, 'time': 13845.0, 'rest': 0.0, 'waiting': 0.0}, {'fromWaypoint': 'destination2', 'toWaypoint': 'destination1', 'distance': 591003.0, 'time': 27227.0, 'rest': 0.0, 'waiting': 0.0}, {'fromWaypoint': 'destination1', 'toWaypoint': 'destination3', 'distance': 604430.0, 'time': 27844.0, 'rest': 0.0, 'waiting': 0.0}, {'fromWaypoint': 'destination3', 'toWaypoint': 'end', 'distance': 2.0, 'time': 0.0, 'rest': 0.0, 'waiting': 0.0}], 'description': 'Targeted best time; without traffic', 'timeBreakdown': {'driving': 68916, 'service': 0, 'rest': 0, 'waiting': 0}}], 'errors': [], 'processingTimeDesc': '940ms', 'responseCode': '200', 'warnings': None, 'requestId': None}

Upvotes: 1

Related Questions