Author: Guy Shepherd
Back in January, I outlined an approach for sourcing model assumptions at run time using a web data source. This approach used some custom code and a third party DLL to connect to the data source endpoint and digest the response. Following the release of Mo.net 7.7 this has just become much more straightforward.
Mo.net Model Development Studio now includes a native web data source connector for accessing web-based assumptions and input data. In this article I will demonstrate how to achieve the same outcome as the original example, but without the need to create custom component code or use an external DLL.
Once again, 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. So, if you don’t already have a key for the OpenWeatherMap API, please check back to my original blog from January before proceeding.
Example
To demonstrate how to easily consume data from an API in Mo.net 7.7, follow the steps below:
1. Create a new instance of the LinkedFundsDynamic sample model in Mo.net Model Development Studio.
2. Create a New Web Data Source in the Tables view and give it a name, such as Weather.
3. Now click on the Source ellipsis and enter the address of the API with your personal key as the last parameter (appid):e.g.
https://api.openweathermap.org/data/2.5/weather?lat=51.507379&lon=-0.106834&appid={app_key}
4. Now create a new Table, calling it WeatherTable.
5. Now select the Source ellipsis and specify the source as an Existing Data Source, and select Weather as the Data Source.
6. Click OK to save the Table Source.
7. Now click on Reference, which will display the Attribute Selection dialog.
8. Select main in the 1st level attribute and click OK to save the selection. This will also provide a preview of the data from the API.
9. Set the following properties for the table: Numeric Only = true, Row LookUp = false, Col LookUp = false
10. To use the API data in the model the easiest thing to do is to add a new Parameter called WeatherData to the model of Data Type = TableDbl and Value = WeatherTable
11. We can then add a new Function called FeelsLikeDegC to retrieve the “feels like” temperature from the WeatherData table. The code is as follows:
Return WeatherData.Value(1, "feels_like") - 273.15
12. We can now add the feels like temperature to the Age1 calculation as before by changing the function code as follows:
Return AgeEnt1 + t \ 12 + CInt(FeelsLikeDegC())
13. 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.
Just for awareness, the temperature is only retrieved at the start of the model or task run; it’s not retrieved on a record-by-record basis, so all records in a given task will be adjusted by the same amount. It would be very simple to check the temperature on a record-by-record basis, but I will leave that as an exercise for the user!
I hope this illustrates how external API data sources can more easily be used in a model using one of the new features of Mo.net v7.7.
Comments are closed.