Skip to content

Commit

Permalink
refactor: add php and node scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalbd committed Mar 7, 2024
1 parent c38784b commit 9c9dcab
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Python/route_encoded_polyline.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import requests
import json
import os

TOLLGURU_API_KEY = "YOUR_TOLLGURU_API_KEY" # API key for Tollguru
TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2" # Base URL for TollGuru Toll API
TOLLGURU_API_KEY = os.environ.get("TOLLGURU_API_KEY")
TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2"
POLYLINE_ENDPOINT = "complete-polyline-from-mapping-service"

polyline = "gib}FjbyeO...a@?c@?c@@g@" # replace with your own polyline
path = "43.64183,-79.38246|18.63085,-100.12845" # replace with your own path


def route_encoded_polyline():
url = f"{TOLLGURU_API_URL}/{POLYLINE_ENDPOINT}"

payload = json.dumps(
{
"source": "here",
"polyline": "gib}FjbyeO...a@?c@?c@@g@", # this is an example polyline please repalce this with your own polyline.
"polyline": polyline, # this is an example polyline please repalce this with your own polyline.
"height": 10, # optional parameters
"weight": 30000, # optional parameters
"vehicleType": "2AxlesTruck", # if not set, by deafult it will set vehicleType as 2AxlesAuto
Expand All @@ -31,7 +36,7 @@ def route_path_lat_lng():
payload = json.dumps(
{
"source": "here",
"path": "43.64183,-79.38246|...|18.63085,-100.12845", # this is an path please repalce this with your own path.
"path": path, # this is an path please repalce this with your own path.
"height": 10, # optional parameters
"weight": 30000, # optional parameters
"vehicleType": "2AxlesTruck", # if not set, by deafult it will set vehicleType as 2AxlesAuto
Expand All @@ -43,3 +48,7 @@ def route_path_lat_lng():
response = requests.request("POST", url, headers=headers, data=payload)

return response


print(route_encoded_polyline().json())
print(route_path_lat_lng().json())
66 changes: 66 additions & 0 deletions javascript/route_encoded_polyline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const TOLLGURU_API_KEY = process.env.TOLLGURU_API_KEY;
const TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2";
const POLYLINE_ENDPOINT = "complete-polyline-from-mapping-service";

const polyline = "gib}FjbyeO...a@?c@?c@@g@"; // replace with your own polyline
const path = "43.64183,-79.38246|18.63085,-100.12845"; // replace with your own path

function routeEncodedPolyline() {
const url = `${TOLLGURU_API_URL}/${POLYLINE_ENDPOINT}`;

const payload = JSON.stringify({
source: "here",
polyline,
height: 10, // optional parameters
weight: 30000, // optional parameters
vehicleType: "2AxlesTruck", // if not set, by default it will set vehicleType as 2AxlesAuto
departure_time: 1609507347, // optional parameters
});

const headers = {
'x-api-key': TOLLGURU_API_KEY,
'Content-Type': 'application/json'
};

return fetch(url, {
method: 'POST',
headers: headers,
body: payload
});
}

function routePathLatLng() {
const url = `${TOLLGURU_API_URL}/${POLYLINE_ENDPOINT}`;

const payload = JSON.stringify({
source: "here",
path,
height: 10, // optional parameters
weight: 30000, // optional parameters
vehicleType: "2AxlesTruck", // if not set, by default it will set vehicleType as 2AxlesAuto
departure_time: 1609507347, // optional parameters
});

const headers = {
'x-api-key': TOLLGURU_API_KEY,
'Content-Type': 'application/json'
};

return fetch(url, {
method: 'POST',
headers: headers,
body: payload
});
}

// Example usage:
routeEncodedPolyline()
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

// Uncomment the following lines to test routePathLatLng()
routePathLatLng()
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
82 changes: 82 additions & 0 deletions php/route_encoded_polyline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

define('TOLLGURU_API_KEY', getenv('TOLLGURU_API_KEY'));
define('TOLLGURU_API_URL', 'https://apis.tollguru.com/toll/v2');
define('POLYLINE_ENDPOINT', 'complete-polyline-from-mapping-service');

$polyline = 'gib}FjbyeO...a@?c@?c@@g@'; // replace with your own polyline
$path = "43.64183,-79.38246|18.63085,-100.12845"; // replace with your own path

function routeEncodedPolyline() {
global $polyline;

$url = TOLLGURU_API_URL . '/' . POLYLINE_ENDPOINT;

$payload = json_encode([
'source' => 'here',
'polyline' => $polyline,
'height' => 10, // optional parameters
'weight' => 30000, // optional parameters
'vehicleType' => '2AxlesTruck', // if not set, by default it will set vehicleType as 2AxlesAuto
'departure_time' => 1609507347, // optional parameters
]);

$headers = [
'x-api-key: ' . TOLLGURU_API_KEY,
'Content-Type: application/json',
];

$options = [
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => $payload,
],
];

$context = stream_context_create($options);
return file_get_contents($url, false, $context);
}

function routePathLatLng() {
global $path;

$url = TOLLGURU_API_URL . '/' . POLYLINE_ENDPOINT;

$payload = json_encode([
'source' => 'here',
'path' => $path, // replace with your own path
'height' => 10, // optional parameters
'weight' => 30000, // optional parameters
'vehicleType' => '2AxlesTruck', // if not set, by default it will set vehicleType as 2AxlesAuto
'departure_time' => 1609507347, // optional parameters
]);

$headers = [
'x-api-key: ' . TOLLGURU_API_KEY,
'Content-Type: application/json',
];

$options = [
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => $payload,
],
];

$context = stream_context_create($options);
return file_get_contents($url, false, $context);
}

// Example usage:
$response1 = routeEncodedPolyline();
$data1 = json_decode($response1, true);
print_r($data1);

// Uncomment the following lines to test routePathLatLng()
$response2 = routePathLatLng();
$data2 = json_decode($response2, true);
print_r($data2);

?>

0 comments on commit 9c9dcab

Please sign in to comment.