Question
· Jan 30, 2018

cache app with google map distance matrix api

I am trying to write an application that will take some information on the database make a call to the Google API distance matrix and get the information to use with rest of the application without using the ensemble part of the development is this possible and how can I do my communication with the  API from a cache class thanks in  advance

Discussion (7)2
Log in or sign up to continue

@Dmitry Maslennikov I have  done this for starters but got an error ^
<INVALID OREF>zTalkToTheWorld

here is my code

 

 

Method TalkToTheWorld()
{
 Set httprequest=##class(%Net.HttpRequest).%New()
 Set httprequest.Server="https://maps.googleapis.com/maps/api/distancematrix/json?origins=BRISTOL&destinations=LONDON&key=TZhydlRg6-dUm-EoIuees43456rtyu7 "
 Do httprequest.Get("/")
 Do httprequest.HttpResponse.OutputToDevice()
 Do httprequest.Get("/cache/")
 Do httprequest.HttpResponse.OutputToDevice()
}

This error happens because you got another error in the request, do not forget to check status.

But anyway. As you need to call https, you should first configure SSL Configuration manually through management portal. You have to fill only Name field, any name on your choice.

Or the same but programmatically.

 ClassMethod GetSSLConfiguration(Server As %String) As %String
{
  new $namespace
  znspace "%SYS"
  do {
    quit:##class(Security.SSLConfigs).Exists(Server)

    set tSC=##class(Security.SSLConfigs).Create(Server)
    $$$ThrowOnError(tSC)
  while 0
  
  quit Server
}

And full working example 

 Class User.Google Extends %RegisteredObject
{

/// Google API key
Parameter KEY = "Put Your Google Maps API Key here";

ClassMethod GetDistanceMatrix(Origins As %String, Destinations As %String, Output Status As %Status) As %DynamicObject
{
  set params("origins")=Origins
  set params("destinations")=Destinations
  set Status=..CallGoogleMapsAPI("/maps/api/distancematrix/json", .params, .data) 
  quit data
}

ClassMethod CallGoogleMapsAPI(Url As %String, ByRef Params, Output Data) As %Status
{
  set Data={}
  set ht=##class(%Net.HttpRequest).%New()
  set ht.Server="maps.googleapis.com"
  set ht.Https=1
  set ht.SSLConfiguration=..GetSSLConfiguration(ht.Server)
  
  set param=""
  for {
    set param=$order(Params(param),1,value)
    quit:param=""
    do ht.SetParam(param, value)
  }
  do ht.SetParam("key", ..#KEY)
  
  set sc=ht.Get(Url)
  if $$$ISERR(sc) quit sc
  
  set Data={}.%FromJSON(ht.HttpResponse.Data)

  quit $$$OK
}

ClassMethod GetSSLConfiguration(Server As %String) As %String
{
  new $namespace
  znspace "%SYS"
  do {
    quit:##class(Security.SSLConfigs).Exists(Server)

    set tSC=##class(Security.SSLConfigs).Create(Server)
    $$$ThrowOnError(tSC)
  while 0
  
  quit Server
}

}

And call in terminal

USER>set data=##class(Google).GetDistanceMatrix("BRISTOL", "LONDON", .sc)

USER>w data.%ToJSON()
{"destination_addresses":["London, UK"],"origin_addresses":["Bristol, UK"],"rows":[{"elements":[{"distance":{"text":"190 km","value":189925},"duration":{"text":"2 hours 23 mins","value":8582},"status":"OK"}]}],"status":"OK"}

My example should work on versions 2016.2 and later, tested on 2017.2

i would start using "postman" to try out google restful api for your particular needs. To see what exactly you need to sent (request) and what you get in return (response). With that in place you can start coding some kind of wrapper methods using %Net.HttpRequest for doing the same requests and preparing the appropriate results from the response for your further processing, as you've already figured out before by using postman.

HTH, Bernd

This method call is not part of any release but, as others have mentioned, you can use %Net.Http to submit a request and receive a response. I can help you write that request. How does this look?

 

USER>set matrix = ##class(<your classname goes here>).GetJSON("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626&key=<you api key goes here>",{"SSLConfiguration":"<your SSL Configuration goes here>"})

USER>write matrix.%ToJSON()

{"httpStatus":"200","message":"HTTP/1.1 200 OK","content":{"destination_addresses":["67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","585 Schenectady Ave, Brooklyn, NY 11203, USA","66-0-66-26 103rd St, Rego Park, NY 11374, USA","931-947 N Village Ave, Rockville Centre, NY 11570, USA","300-448 Beach 19th St, Far Rockaway, NY 11691, USA","585 Schenectady Ave, Brooklyn, NY 11203, USA","66-0-66-26 103rd St, Rego Park, NY 11374, USA","931-947 N Village Ave, Rockville Centre, NY 11570, USA","300-448 Beach 19th St, Far Rockaway, NY 11691, USA"],"origin_addresses":["566 Vermont St, Brooklyn, NY 11207, USA"],"rows":[{"elements":[{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"3.4 mi","value":5426},"duration":{"text":"19 mins","value":1136},"status":"OK"},{"distance":{"text":"8.5 mi","value":13740},"duration":{"text":"23 mins","value":1399},"status":"OK"},{"distance":{"text":"15.9 mi","value":25546},"duration":{"text":"29 mins","value":1755},"status":"OK"},{"distance":{"text":"13.2 mi","value":21296},"duration":{"text":"34 mins","value":2040},"status":"OK"},{"distance":{"text":"3.4 mi","value":5426},"duration":{"text":"19 mins","value":1136},"status":"OK"},{"distance":{"text":"8.5 mi","value":13740},"duration":{"text":"23 mins","value":1399},"status":"OK"},{"distance":{"text":"15.9 mi","value":25546},"duration":{"text":"29 mins","value":1755},"status":"OK"},{"distance":{"text":"13.2 mi","value":21296},"duration":{"text":"34 mins","value":2040},"status":"OK"}]}],"status":"OK"}}

 

And with a little work, this can also be invoked as an SQL function:

 

Row count: 1 Performance: 0.398 seconds  54 global references 2349 lines executed 0 disk read latency (ms)  Cached Query: %sqlcq.pSYS.cls4  Last update: 2018-01-30 07:09:30.073

Print

 
Expression_1
[{"httpStatus":"200","message":"HTTP/1.1 200 OK","content":{"destination_addresses":["67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","67-89 Pacific St, Brooklyn, NY 11201, USA","585 Schenectady Ave, Brooklyn, NY 11203, USA","66-0-66-26 103rd St, Rego Park, NY 11374, USA","931-947 N Village Ave, Rockville Centre, NY 11570, USA","300-448 Beach 19th St, Far Rockaway, NY 11691, USA","585 Schenectady Ave, Brooklyn, NY 11203, USA","66-0-66-26 103rd St, Rego Park, NY 11374, USA","931-947 N Village Ave, Rockville Centre, NY 11570, USA","300-448 Beach 19th St, Far Rockaway, NY 11691, USA"],"origin_addresses":["566 Vermont St, Brooklyn, NY 11207, USA"],"rows":[{"elements":[{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"6.7 mi","value":10754},"duration":{"text":"37 mins","value":2201},"status":"OK"},{"distance":{"text":"3.4 mi","value":5426},"duration":{"text":"19 mins","value":1136},"status":"OK"},{"distance":{"text":"8.5 mi","value":13740},"duration":{"text":"23 mins","value":1399},"status":"OK"},{"distance":{"text":"15.9 mi","value":25546},"duration":{"text":"29 mins","value":1755},"status":"OK"},{"distance":{"text":"13.2 mi","value":21296},"duration":{"text":"34 mins","value":2040},"status":"OK"},{"distance":{"text":"3.4 mi","value":5426},"duration":{"text":"19 mins","value":1136},"status":"OK"},{"distance":{"text":"8.5 mi","value":13740},"duration":{"text":"23 mins","value":1399},"status":"OK"},{"distance":{"text":"15.9 mi","value":25546},"duration":{"text":"29 mins","value":1755},"status":"OK"},{"distance":{"text":"13.2 mi","value":21296},"duration":{"text":"34 mins","value":2040},"status":"OK"}]}],"status":"OK"}}]

1 row(s) affected