From ffc73aed8f5a0ba627ff9fb4ada78db578860557 Mon Sep 17 00:00:00 2001 From: Vishal Balaji D Date: Thu, 7 Mar 2024 21:54:03 +0530 Subject: [PATCH] refactor: extract out request params --- dotnet/Program.cs | 73 +++++++++++++++++++++++-------- go/index.go | 25 ++++++++--- javascript/index.js | 14 +++++- php/__test__/test_mapmyindia.php | 14 +++++- php/php_curl_mapmyindia.php | 12 ++++- python/Testing/test_mapmyindia.py | 20 ++++++--- python/mapmyindia.py | 20 ++++++--- ruby/main.rb | 17 ++++++- 8 files changed, 150 insertions(+), 45 deletions(-) diff --git a/dotnet/Program.cs b/dotnet/Program.cs index 4c2452b..04e7434 100644 --- a/dotnet/Program.cs +++ b/dotnet/Program.cs @@ -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; + 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; @@ -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); @@ -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(this Dictionary dictionary, IDictionary 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; + } + } +} diff --git a/go/index.go b/go/index.go index 8d48dc1..ff3fa69 100644 --- a/go/index.go +++ b/go/index.go @@ -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 @@ -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) diff --git a/javascript/index.js b/javascript/index.js index 19688cd..319a7ad 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -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` @@ -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); diff --git a/php/__test__/test_mapmyindia.php b/php/__test__/test_mapmyindia.php index bed1310..33eed89 100644 --- a/php/__test__/test_mapmyindia.php +++ b/php/__test__/test_mapmyindia.php @@ -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; @@ -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.. diff --git a/php/php_curl_mapmyindia.php b/php/php_curl_mapmyindia.php index c0627ce..8272654 100644 --- a/php/php_curl_mapmyindia.php +++ b/php/php_curl_mapmyindia.php @@ -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.. @@ -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.. diff --git a/python/Testing/test_mapmyindia.py b/python/Testing/test_mapmyindia.py index a2139c3..7e6ece6 100644 --- a/python/Testing/test_mapmyindia.py +++ b/python/Testing/test_mapmyindia.py @@ -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, @@ -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() diff --git a/python/mapmyindia.py b/python/mapmyindia.py index bd88a23..dbfa171 100644 --- a/python/mapmyindia.py +++ b/python/mapmyindia.py @@ -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, @@ -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() diff --git a/ruby/main.rb b/ruby/main.rb index 5051604..c96d525 100644 --- a/ruby/main.rb +++ b/ruby/main.rb @@ -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" @@ -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)