"""Jimo Maps REST example. Run only on a trusted machine/backend. Set JIMO_MAPS_BASE_URL and JIMO_MAPS_KEY (a server key). python jimo_maps.py [--navigation] """ import argparse import json import os from urllib.error import HTTPError from urllib.parse import urlencode from urllib.request import Request, urlopen def get(path): base = os.environ['JIMO_MAPS_BASE_URL'].rstrip('/') request = Request(base + path, headers={'X-JimoMaps-Key': os.environ['JIMO_MAPS_KEY']}) try: with urlopen(request, timeout=30) as response: return json.load(response) except HTTPError as error: raise RuntimeError(f'Jimo Maps HTTP {error.code}; check permissions/quota') from None if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--navigation', action='store_true') args = parser.parse_args() access = get('/api/map/access') print(json.dumps(access, indent=2)) if args.navigation: if not access.get('navigation'): raise SystemExit('Navigation is not enabled for this key') query = urlencode({'start': '36.8219,-1.2921', 'end': '36.814,-1.265'}) data = get('/api/map/route?' + query) if data.get('code') != 'Ok' or not data.get('routes'): raise SystemExit('No driving route found') route = data['routes'][0] print(f"{route['distance'] / 1000:.1f} km; {route['duration'] / 60:.0f} min")