How to learn HERE API in a short time

image alt

-Where are you?

-I'm here

At the end of September, the largest hackathon in the world took place in Kazan. This event, according to the number of participants, was included in the Guinness Book of Records.







For these 48 hours, we were tasked with:





Develop a prototype mobile application that allows a passenger to order food delivery from restaurants located in cities along the train route.


Lyrics



Of course, throughout the event, we were engaged in solving heterogeneous subtasks on our topic. We did not spend all 48 hours learning the HERE API and writing the above three queries.







In this article, I want to share exactly the experience of using the HERE API obtained during these sleepless 48 hours, more precisely, as a junior developer of Python and distributed systems on the network to interact with other systems on the network. The article does not pretend to be the translation of all the HERE API documentation, it only describes practical application in solving our problems.







Introduction



In order to optimize the delivery of orders and their execution on time, the courier must arrive at the platform at the exact time the train arrives at the station. In this regard, each courier should know the minimum necessary time for delivery of the order. To solve this problem, it was necessary to calculate the minimum travel time between two points (from the restaurant to the railway station). The path was calculated in the village, in connection with this, it was decided to consider various options, namely, the use of public transport, a personal car and your feet for walking.







Analysis of the public APIs showed that the following REST requests are most suitable for solving the problem:









Using the API begins by registering in the developer section of the official website to generate and receive APP ID and APP CODE keys. Free API key allows you to perform up to 250 thousand requests per month. Believe me, this covers all the needs for a hackathon.







The statistics of the use of the HERE API by our application for 48 hours showed the following figures:









Practice



Each request contains the following fields:







deplocation = A #   arrlocation = B #   # ,      app_id = os.getenv('HERE_APP_ID') app_code = os.getenv('HERE_APP_CODE')
      
      





Finding travel time using public transport



 url = f"https://transit.api.here.com/v3/route.json" query = { 'dep': deplocation, 'arr': arrlocation, 'time': datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), # ,     'app_id': app_id, 'app_code': app_code, 'routing': 'tt' #    } response = requests.get(url, params=query) data = response.json() status = data["Res"] if "Message" in status: print(status["Message"]) exit(-1) if "Connections" in status: route_dut_time = iso8601toSec( status["Connections"]["Connection"][0]["duration"] )
      
      





I would like to note that in this request, the time is given using ISO 8601. The function of converting the received time duration to seconds iso8601toSec ​​was implemented.









Finding travel time using personal vehicles



 url = f"https://route.api.here.com/routing/7.2/calculateroute.json" query = { 'waypoint0': deplocation, 'waypoint1': arrlocation, 'mode': 'fastest;car;traffic:enabled', #       'app_id': app_id, 'app_code': app_code, 'departure': 'now' #   } response = requests.get(url, params=query) data = response.json() route_dur_time = data['response']['route'][0]['summary']['trafficTime']
      
      





There are no problems with this request over time, it returns in seconds.









Finding travel time using your own legs (walking)



 url = f"https://route.api.here.com/routing/7.2/calculateroute.json" query = { 'waypoint0': deplocation, 'waypoint1': arrlocation, 'mode': 'fastest;pedestrian', #   'app_id': app_id, 'app_code': app_code } response = requests.get(url, params=query) data = response.json() route_dur_time = data['response']['route'][0]['summary']['travelTime']
      
      





In this request, as in the previous example, there are no problems with time, it returns in seconds.







findings



Based on these requests, we got three times necessary for moving from point A to point B. After calculating the minimum time and type of movement from them, we determined how long it took to get out in order to reach point B by the indicated time.








All Articles