How to use asynchronous Matrix Routing API v8 from HERE to get travel time response including traffic

1k views Asked by At

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:

  1. Request matrix (POST)
  2. Poll for status (GET)
  3. 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.

2

There are 2 answers

1
Persistent Plants On

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:

{
    "origins": [
        {
            "lat": 47.673993,
            "lng": -122.356108
        },
        {
            "lat": 47.656910,
            "lng": -122.362823
        },
        {
            "lat": 47.648015,
            "lng": -122.335674
        },
        {
            "lat": 47.653022,
            "lng": -122.312461
        },
        {
            "lat":47.675796,
            "lng": -122.311520
        }
    ],
    "destinations": [
        {
            "lat": 47.661438,
            "lng": -122.336427
        }
    ],
    "regionDefinition": {
        "type": "world"
    }
}
0
Nini On

It seems to be that internally, here.com performs a redirect, and the API key gets lost on the way.

The solution is to add allow_redirects=False to your request, following the answer given here (by someone working at here.com) to what seems to be pretty much the same issue as posted here.

This has solved it for me.