Calling external services
You can use the
Hansen.Core.Net.NetService object to call external services from your Operations and Regulations formulas.
The simplest way to use the NetService object is to write a formula using a service profile. The service profile specifies the details of the web request, such as the host address, the request method, and the response format.
See Service profiles.
You can use the NetService object without a service profile, but in that case you must specify the details of the web request in your code. This example shows how to construct a web request:
Public Class NetServiceIndexSearcherTests
Inherits ServerApplicationTestFixture
Private _webRequestConfigKey As Integer
Public Sub New()
NewConnectionPerTest = False
End Sub
Protected Overrides Sub OnClassLoad(source As Object, e As EventArgs)
Dim config As IWebRequestConfig = NewComponent(Of IWebRequestConfig)()
' Go to openweathermap.org to sign up for an API key (APPID)
config.HostAddress = "https://api.openweathermap.org/data/2.5/weather?q={{city}}&APPID=xxx"
config.WebMethod = WebRequestMethod.Get
config.Add().Assert()
Dim mapping As IWebRequestConfigMapping = NewComponent(Of IWebRequestConfigMapping)()
mapping.Source = "$.main.temp"
mapping.Destination = "Temp"
mapping.WebRequestConfigKey = config
mapping.Add()
_webRequestConfigKey = config.WebRequestConfigKey
End Sub
<Test>
Public Sub TestSearch()
Dim mappings As IDictionary(Of String, Object) = New Dictionary(Of String, Object)()
mappings.Add("city", "Sacramento")
Dim ds As IWebRequestDataSource = WebRequestDataSource.Create(mappings)
Dim context = NetService.Send(_webRequestConfigKey, ds)
End Sub
End Class
}