I'm after the travel times between a set of nodes (in both directions) including traffic. In the old version of the API (7.2) I would use the following code in Python to request this:
payload = {
"apiKey": "API_KEY_HERE",
"start0": "-33.795602,151.185366",
"start1": "-33.865103,151.205627",
"destination0": "-33.795602,151.185366",
"destination1": "-33.865103,151.205627",
"mode": "fastest;car;traffic:enabled",
"departure": "2020-10-27T08:00:00+11",
"summaryattributes": "all"
}
base_url = "https://matrix.route.ls.hereapi.com/routing/7.2/calculatematrix.json?"
r = requests.get(base_url, params=payload)
print(r.json())
The new version has fewer examples, and to be honest I'm not so familiar with POSTs and Asynchronous responses.
Why change to the new version? Well, it seems that you could only feed a single set of origin nodes/locations and then a matrix will be computed (in the background) and once ready it can be pulled with a GET request. No specifying start0, start1, ..etc
New try with the version 8:
Steps:
- Request matrix (POST)
- Poll for status (GET)
- Download result when ready (GET)
# 1. Request matrix (POST)
base_url = "https://matrix.router.hereapi.com/v8/matrix?"
params = {"apiKey": "MY_API_KEY_HERE",
"async": "true",
"departureTime": "2020-10-27T08:00:00+11"}
payload = {"origins": [{"lat": -33.759688, "lng": 151.156369}, {"lat": -33.865189, "lng": 151.208162},
{"lat": -33.677066, "lng": 151.302117}],
"regionDefinition": {"type": "autoCircle", "margin": 10000},
"matrixAttributes": ["travelTimes", "distances"]}
headers = {'Content-Type': 'application/json'}
r = requests.post(base_url, params=params, json=payload, headers=headers)
response = r.json()
# pretty print
print(json.dumps(response, indent=4))
This gives an "accepted" status:
// Example response
{
"matrixId": "eba6780c-0379-40f1-abaf-5c62d07dabb4",
"status": "accepted",
"statusUrl": "https://aws-eu-west-1.matrix.router.hereapi.com/v8/matrix/eba6780c-0379-40f1-abaf-5c62d07dabb4/status"
}
Then I use the statusUrl and my apiKey to poll the status. This is where I'm stuck. How should I authenticate? I'm not sure how the authentication should work. The authentication in step 1 worked.
# 2. Poll for status (GET)
time.sleep(3)
statusUrl = response['statusUrl']
params = {'apiKey': 'MY_API_KEY_HERE'}
headers = {'Content-Type': 'application/json'}
r = requests.get(statusUrl, params=params, headers=headers)
response = r.json()
# pretty print
print(json.dumps(response, indent=4))
Where 'MY_API_KEY_HERE' is written I input my apiKey. The response:
{
"error": "Unauthorized",
"error_description": "No credentials found"
}
Evidently, there is an error using the authentication. How should the authentication be used? Would it be possible to show how a successful request for checking the status of a submitted matrix calculation looks like and how the request for downloading such matrix looks like in Python (the next step after polling the status using gzip headers)?
Any pointers would be welcome.
It looks like based on the documentation, when you provide departure time, they no longer take live traffic into account - I'm not sure about historical though. So if you need live traffic taken into account, you will have to remove departure time.
This will calculate travel times including traffic time: Method URL:
https://matrix.router.hereapi.com/v8/matrix?apiKey={{API_KEY}}&async=false
Method:
POST
Body: