Skip to content

Commit

Permalink
refactor: extract out request params
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalbd committed Mar 7, 2024
1 parent f32cf08 commit ffc73ae
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 45 deletions.
73 changes: 54 additions & 19 deletions dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@
using System.IO;
using System.Net;
using RestSharp;
using System.Collections.Generic;
using System.Text.Json;

namespace mapmyIndia
{
static class Constants
using obj = Dictionary<string, object>;
public class Program
{
public const string MAPMYINDIA_API_KEY = Environment.GetEnvironmentVariable("MAPMYINDIA_API_KEY");
public const string MAPMYINDIA_API_URL = "https://apis.mapmyindia.com/advancedmaps/v1";

public const string TOLLGURU_API_KEY = Environment.GetEnvironmentVariable("TOLLGURU_API_KEY");
public const string TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2";
public const string POLYLINE_ENDPOINT = "complete-polyline-from-mapping-service";
private static string MAPMYINDIA_API_KEY = Environment.GetEnvironmentVariable("MAPMYINDIA_API_KEY");
private static string MAPMYINDIA_API_URL = "https://apis.mapmyindia.com/advancedmaps/v1";

private static string TOLLGURU_API_KEY = Environment.GetEnvironmentVariable("TOLLGURU_API_KEY");
private static string TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2";
private static string POLYLINE_ENDPOINT = "complete-polyline-from-mapping-service";

// New Delhi
public const string source_latitude = "28.68932119156764";
public const string source_longitude = "77.18609677688849";
private static string source_latitude = "28.68932119156764";
private static string source_longitude = "77.18609677688849";
// Mumbai
public const string destination_latitude = "19.092580173664984";
public const string destination_longitude = "72.89902799500808";
}
private static string destination_latitude = "19.092580173664984";
private static string destination_longitude = "72.89902799500808";

private static obj request_params = new obj {
{ "vehicle", new obj {
{ "type", "2AxlesAuto" },
}},
{ "departure_time", "2021-01-05T09:46:08Z" },
};

public class Program
{
public static string get_Response(string source_latitude, string source_longitude, string destination_latitude, string destination_longitude)
{
string api_key = Constants.MAPMYINDIA_API_KEY;
string url = Constants.MAPMYINDIA_API_URL + "/" + api_key + "/route_adv/driving/" + source_longitude + "," + source_latitude + ";" + destination_longitude + "," + destination_latitude + "?geometries=polyline&overview=full";
string api_key = MAPMYINDIA_API_KEY;
string url = MAPMYINDIA_API_URL + "/" + api_key + "/route_adv/driving/" + source_longitude + "," + source_latitude + ";" + destination_longitude + "," + destination_latitude + "?geometries=polyline&overview=full";
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
String polyline;
Expand All @@ -44,11 +53,15 @@ public static string get_Response(string source_latitude, string source_longitud
}
public static string Post_Tollguru(string polyline)
{
var client = new RestClient(Constants.TOLLGURU_API_URL + "/" + Constants.POLYLINE_ENDPOINT);
var client = new RestClient(TOLLGURU_API_URL + "/" + POLYLINE_ENDPOINT);
var request1 = new RestRequest(Method.POST);
request1.AddHeader("content-type", "application/json");
request1.AddHeader("x-api-key", "");
request1.AddParameter("application/json", "{\"source\":\"mapmyindia\" , \"polyline\":\"" + polyline + "\" }", ParameterType.RequestBody);
request1.AddHeader("x-api-key", TOLLGURU_API_KEY);
request_params.AddRange(new obj {
{ "source", "mapmyindia" },
{ "polyline", polyline },
});
request1.AddParameter("application/json", JsonSerializer.Serialize(request_params), ParameterType.RequestBody);
IRestResponse response1 = client.Execute(request1);
var content = response1.Content;
//Console.WriteLine(content);
Expand All @@ -59,8 +72,30 @@ public static string Post_Tollguru(string polyline)
}
public static void Main()
{
string polyline = get_Response(Constants.source_latitude, Constants.source_longitude, Constants.destination_latitude, Constants.destination_longitude);
string polyline = get_Response(source_latitude, source_longitude, destination_latitude, destination_longitude);
Console.WriteLine(Post_Tollguru(polyline));
}
}
}

// Extension method to add range to a dictionary
public static class DictionaryExtensions
{
public static void AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, IDictionary<TKey, TValue> range)
{
if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary));
}

if (range == null)
{
throw new ArgumentNullException(nameof(range));
}

foreach (var kvp in range)
{
dictionary[kvp.Key] = kvp.Value;
}
}
}
25 changes: 19 additions & 6 deletions go/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ const (
destination_longitude float32 = 72.89902799500808
)

// Explore https://tollguru.com/toll-api-docs to get the best of all the parameters that tollguru has to offer
var requestParams = map[string]interface{}{
"vehicle": map[string]interface{}{
"type": "2AxlesAuto",
},
// Visit https://en.wikipedia.org/wiki/Unix_time to know the time format
"departure_time": "2021-01-05T09:46:08Z",
}

func main() {

// Getting polyline from MapmyIndia
Expand Down Expand Up @@ -75,12 +84,16 @@ func main() {

url_tollguru := fmt.Sprintf("%s/%s", TOLLGURU_API_URL, POLYLINE_ENDPOINT)

requestBody, err := json.Marshal(map[string]string{
"source": "mapmyindia",
"polyline": polyline,
"vehicleType": "2AxlesAuto",
"departure_time": "2021-01-05T09:46:08Z",
})
params := map[string]interface{}{
"source": "mapmyindia",
"polyline": polyline,
}

for k, v := range requestParams {
params[k] = v
}

requestBody, err := json.Marshal(params)

request, err := http.NewRequest("POST", url_tollguru, bytes.NewBuffer(requestBody))
request.Header.Set("x-api-key", TOLLGURU_API_URL)
Expand Down
14 changes: 13 additions & 1 deletion javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ const POLYLINE_ENDPOINT = "complete-polyline-from-mapping-service";
const source = { latitude: 28.68932119156764, longitude: 77.18609677688849 }; // New Delhi
const destination = { latitude: 19.092580173664984, longitude: 72.89902799500808, }; // Mumbai

// Explore https://tollguru.com/toll-api-docs to get the best of all the parameters that tollguru has to offer
const requestParameters = {
"vehicle": {
"type": "2AxlesAuto",
},
// Visit https://en.wikipedia.org/wiki/Unix_time to know the time format
"departure_time": "2021-01-05T09:46:08Z",
}

const url = `${MAPMYINDIA_API_URL}/${MAPMYINDIA_API_KEY}/route_adv/driving/${source.longitude},${source.latitude};${destination.longitude},${destination.latitude}?geometries=polyline&overview=full`

Expand Down Expand Up @@ -49,7 +57,11 @@ const handleRoute = (e, r, body) => {
'content-type': 'application/json',
'x-api-key': TOLLGURU_API_KEY
},
body: JSON.stringify({ source: "mapmyindia", polyline: _polyline })
body: JSON.stringify({
source: "mapmyindia",
polyline: _polyline,
...requestParameters,
})
},
(e, r, body) => {
console.log(e);
Expand Down
14 changes: 12 additions & 2 deletions php/__test__/test_mapmyindia.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
$TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2";
$POLYLINE_ENDPOINT = "complete-polyline-from-mapping-service";

// Explore https://tollguru.com/toll-api-docs to get the best of all the parameters that tollguru has to offer
$request_parameters = array(
"vehicle" => array(
"type" => "2AxlesAuto",
),
// Visit https://en.wikipedia.org/wiki/Unix_time to know the time format
"departure_time" => "2021-01-05T09:46:08Z",
);

//Source and Destination Coordinates..
function getPolyline($source_longitude,$source_latitude,$destination_longitude,$destination_latitude) {
global $MAPMYINDIA_API_KEY, $MAPMYINDIA_API_URL;
Expand Down Expand Up @@ -58,8 +67,9 @@ function getPolyline($source_longitude,$source_latitude,$destination_longitude,$


$postdata = array(
"source" => "gmaps",
"polyline" => $polyline_mapmyindia
"source" => "mapmyindia",
"polyline" => $polyline_mapmyindia,
...$request_parameters,
);

//json encoding source and polyline to send as postfields..
Expand Down
12 changes: 11 additions & 1 deletion php/php_curl_mapmyindia.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
$destination_longitude='72.89902799500808';
$destination_latitude='19.092580173664984';

// Explore https://tollguru.com/toll-api-docs to get the best of all the parameters that tollguru has to offer
$request_parameters = array(
"vehicle" => array(
"type" => "2AxlesAuto",
),
// Visit https://en.wikipedia.org/wiki/Unix_time to know the time format
"departure_time" => "2021-01-05T09:46:08Z",
);

$url = $MAPMYINDIA_API_URL.'/'.$MAPMYINDIA_API_KEY.'/route_adv/driving/'.$source_longitude.','.$source_latitude.';'.$destination_longitude.','.$destination_latitude.'?geometries=polyline&overview=full';

//connection..
Expand Down Expand Up @@ -56,7 +65,8 @@

$postdata = array(
"source" => "mapmyindia",
"polyline" => $polyline_mapmyindia
"polyline" => $polyline_mapmyindia,
...$request_parameters,
);

//json encoding source and polyline to send as postfields..
Expand Down
20 changes: 13 additions & 7 deletions python/Testing/test_mapmyindia.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
TOLLGURU_API_URL = "https://apis.tollguru.com/toll/v2"
POLYLINE_ENDPOINT = "complete-polyline-from-mapping-service"

"""Extrating Polyline from MapmyIndia"""
# Explore https://tollguru.com/toll-api-docs to get best of all the parameter that tollguru has to offer
request_parameters = {
"vehicle": {
"type": "2AxlesAuto",
},
# Visit https://en.wikipedia.org/wiki/Unix_time to know the time format
"departure_time": "2021-01-05T09:46:08Z",
}


def get_polyline_from_mapmyindia(
source_longitude, source_latitude, destination_longitude, destination_latitude
):
"""Extrating Polyline from MapmyIndia"""

# Query MapmyIndia with Key and Source-Destination coordinates
url = "{a}/{b}/route_adv/driving/{c},{d};{e},{f}?geometries=polyline&overview=full".format(
a=MAPMYINDIA_API_URL,
Expand Down Expand Up @@ -44,20 +53,17 @@ def get_polyline_from_mapmyindia(
return polyline_from_mapmyindia


"""Calling Tollguru API"""


def get_rates_from_tollguru(polyline):
"""Calling Tollguru API"""

# Tollguru querry url
Tolls_URL = f"{TOLLGURU_API_URL}/{POLYLINE_ENDPOINT}"
# Tollguru resquest parameters
headers = {"Content-type": "application/json", "x-api-key": TOLLGURU_API_KEY}
params = {
# explore https://tollguru.com/developers/docs/ to get best off all the parameter that tollguru offers
**request_parameters,
"source": "mapmyindia",
"polyline": polyline, # this is polyline that we fetched from the mapping service
"vehicleType": "2AxlesAuto", #'''Visit https://tollguru.com/developers/docs/#vehicle-types to know more options'''
"departure_time": "2021-01-05T09:46:08Z", #'''Visit https://en.wikipedia.org/wiki/Unix_time to know the time format'''
}
# Requesting Tollguru with parameters
response_tollguru = requests.post(Tolls_URL, json=params, headers=headers).json()
Expand Down
20 changes: 13 additions & 7 deletions python/mapmyindia.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@
19.09258017366498,
)

"""Extrating Polyline from MapmyIndia"""
# Explore https://tollguru.com/toll-api-docs to get best of all the parameter that tollguru has to offer
request_parameters = {
"vehicle": {
"type": "2AxlesAuto",
},
# Visit https://en.wikipedia.org/wiki/Unix_time to know the time format
"departure_time": "2021-01-05T09:46:08Z",
}


def get_polyline_from_mapmyindia(
source_longitude, source_latitude, destination_longitude, destination_latitude
):
"""Extrating Polyline from MapmyIndia"""

# Query MapmyIndia with Key and Source-Destination coordinates
url = "{a}/{b}/route_adv/driving/{c},{d};{e},{f}?geometries=polyline&overview=full".format(
b=MAPMYINDIA_API_KEY,
Expand Down Expand Up @@ -55,20 +64,17 @@ def get_polyline_from_mapmyindia(
return polyline_from_mapmyindia


"""Calling Tollguru API"""


def get_rates_from_tollguru(polyline):
"""Calling Tollguru API"""

# Tollguru querry url
Tolls_URL = f"{TOLLGURU_API_URL}/{POLYLINE_ENDPOINT}"
# Tollguru resquest parameters
headers = {"Content-type": "application/json", "x-api-key": TOLLGURU_API_KEY}
params = {
# explore https://tollguru.com/developers/docs/ to get best off all the parameter that tollguru offers
**request_parameters,
"source": "mapmyindia",
"polyline": polyline, # this is polyline that we fetched from the mapping service
"vehicleType": "2AxlesAuto", #'''Visit https://tollguru.com/developers/docs/#vehicle-types to know more options'''
"departure_time": "2021-01-05T09:46:08Z", #'''Visit https://en.wikipedia.org/wiki/Unix_time to know the time format'''
}
# Requesting Tollguru with parameters
response_tollguru = requests.post(Tolls_URL, json=params, headers=headers).json()
Expand Down
17 changes: 15 additions & 2 deletions ruby/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
# Destination Details - Mumbai Lat-long coordinates
destination = { longitude: '72.89902', latitude: '19.09258' }

# Explore https://tollguru.com/toll-api-docs to get the best of all the parameters that tollguru has to offer
request_parameters = {
"vehicle": {
"type": "2AxlesAuto",
},
# Visit https://en.wikipedia.org/wiki/Unix_time to know the time format
"departure_time": "2021-01-05T09:46:08Z",
}

# GET Request to MapmyIndia for Polyline
KEY = ENV['MAPMYINDIA_KEY']
MAPMYINDIA_URL = "#{MAPMYINDIA_API_URL}/#{KEY}/route_adv/driving/#{source[:longitude]},#{source[:latitude]};#{destination[:longitude]},#{destination[:latitude]}?geometries=polyline&overview=full"
Expand All @@ -24,6 +33,10 @@

# Sending POST request to TollGuru
tollguru_url = "#{TOLLGURU_API_URL}/#{POLYLINE_ENDPOINT}"
headers = {'content-type' => 'application/json', 'x-api-key' => TOLLGURU_API_KEY}
body = {'source' => "mapmyindia", 'polyline' => mapmyindia_polyline, 'vehicleType' => "2AxlesAuto", 'departure_time' => "2021-01-05T09:46:08Z"}
headers = {'content-type': 'application/json', 'x-api-key': TOLLGURU_API_KEY}
body = {
'source': "bing",
'polyline': google_encoded_polyline,
**request_parameters,
}
tollguru_response = HTTParty.post(tollguru_url,:body => body.to_json, :headers => headers)

0 comments on commit ffc73ae

Please sign in to comment.