Consuming a Web Service with Mo.net

Author: Guy Shepherd

Last week I received a request from an existing client who wanted to connect their existing Mo.net-based front office illustration platform to a new underwriting system.  The challenge they faced was that the new underwriting platform only provided connectivity using a web service application programming interface (API), rather than one the more traditional, and dare I say antiquated approaches, such as file or email transfer, or through a shared database environment.

This is challenge that we’ve helped a number of our clients solve in a variety of different ways over the last couple of years, so I thought I’d take a few minutes to explain how to do this sort of thing using Mo.net.

I’m going to illustrate this example using one of the many publicly available APIs that are now available, which can provide information regarding the stock market, car prices, demographics, and even dog breeds.  I’ve selected one of numerous weather APIs, called OpenWeatherMap, but a similar approach can be adopted for any API.  To use OpenWeatherMap it’s simply a case of signing up to get a free API key, so before attempting to run this example, please make sure you get your own key.

Sample Code

To help demonstrate how to consume data from an API in a Mo.net model, I’m going to assume that the current temperature in London has an impact on the mortality of a life in our sample LinkedFunds project.  While this is a slightly ridiculous notion, it keeps things simple, and should allow clients to develop more realistic solutions using their own models.  As usual there are several ways to implement such a solution using Mo.net, but I’m going to use a Custom Component approach for this example.

Follow the steps below:

  1. Create a new instance of the LinkedFundsDynamic sample model in Mo.net Model Development Studio.
  2. Create a new Custom Component, which we’ll call WeatherAPI.
  3. Add a Reference (from the File menu) to Newtonsoft.Json.dll, which is located in C:\Program Files (x86)\Software Alliance\Mo.net 7.6\Mo.net Model Development Studio
  4. Add the following imports in the Imports dialog:
    – System.Net
    – Newtonsoft.Json
    – Newtonsoft.Json.Linq
  5. Enter the code below into the WeatherAPI component.
'   This component retrieves the "feels like" temperature in Degrees Celcius for London from the OpenWeather API

Public Class MainInfo
    Public Property feels_like As Double
End Class

Public Class WeatherResponse
    Public Property main As MainInfo
End Class

Function GetFeelsLikeTemp() As Double

        Dim strresponse As String = ""

        Dim url As String = "https://api.openweathermap.org/data/2.5/weather?lat=51.507379&lon=-0.106834&appid=<id>"
        Dim webRequest As WebRequest = WebRequest.Create(url)
        Dim request As HttpWebRequest = CType(webRequest, HttpWebRequest)
        request.Method = "GET"
        request.ContentType = "application/json"

        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

        Using reader As New StreamReader(response.GetResponseStream())
            strresponse = reader.ReadToEnd()
        End Using

        ' Deserialize the JSON response into the strongly typed classes defined above
        Dim weatherResponse As WeatherResponse = JsonConvert.DeserializeObject(Of WeatherResponse)(strresponse)

        ' Access strongly typed properties

        If weatherResponse IsNot Nothing AndAlso weatherResponse.main IsNot Nothing Then
            Return weatherResponse.main.feels_like - 273.15
        Else
            Return 0
        End If       
        
End Function

Make sure to enter your own OpenWeatherMap API key in line 15 replacing the <id> tag at the end of the request.

  1. You can now call the API from anywhere in the model.  For this example, I’m simply going to add the current “feels like” temperature in London (in Degrees Celsius) to the age, by amending the Age1 function in the Linked model as follows:

Return AgeEnt1 + t \ 12 + CInt(GetFeelsLikeTemp)

  1. If you run the model or any of the tasks, the age of the lives should now be adjusted by the current temperature in London.

There are a couple of flaws with this example.  For a start the temperature is retrieved for every timestep rather than being retrieved once and stored in a new parameter.  This will have an impact on run times, and swamp the API with requests, but is trivial enough to resolve.

I hope this illustrates how external API data sources can easily be used in a Mo.net model without a significant amount of effort.

Was this article helpful?
YesNo

Comments are closed.