Enterprise Application Development

Fast Modeling, Responsive Design, One-Button Deploy

BOOK a Demo

Economic theory-based business modeling

Resources, Events, Agents

BOOK a Demo
OMNIA – Low-Code Business Application Development Platform

External Data Sources: AccuWeather API
Posted December 18, 2019

External Data Sources: AccuWeather API Posted December 18, 2019

Integration is one of the most powerful aspects of our platform. Combining several sources of data into one robust application that allows you to manipulate it at will is very (very!) useful.

 

Data sources are, by definition, sources of data (duh).  Out platform provides the option to use Internal and External Data Sources. An External Data Source can be an ERP or other kinds of enterprise-type systems, but it can also be a .CSV file or an API. OMNIA provides the necessary endpoints and a spot to place your integration code.

 

Today we’ll be elaborating a simple example to demonstrate how to perform such integrations. In this example we’ll be adding a simple ListView web component, that will use AccuWeather’s API as a Data Source to show the forecast for the next 5 days.

AccuWeather as a Data Source

In order to be able to replicate this example, you’ll need to get acquainted with AccuWeather’s API reference (see here).

 

After you’re all set on that side, let’s focus on OMNIA’s side of things:

 

Data Sources are platform entities with three special characteristics that make them the perfect tool to interact with other sources of data:

 

     – Behaviour runtime: where the entity’s behaviour will execute. Can be internal (OMNIA) or external (Accuweather, in this example)

     – Data Access runtime: where CRUD Operations (what is CRUD?) will be executed. Can be internal or external.

     – Executed on Connector: whether behaviours will be executed on the Connector (what is this?) or not

 

Remember: External Data Sources can be whatever you want, from your company’s ERP/CRM (even legacy systems), APIs or a simple .csv file.

Setup Demo

In order to setup this AccuWeather integration, you need go to AccuWeather Developers, create an account and a new app, take note of the API Key, then follow these steps (all necessary code is for this example is provided below this setup):

 

1 – Now go to your Tenant and add a new Data Source with the following configurations:

  • Behaviour runtime: Internal;
  • Data Access runtime: External;
  • Executed on Connector: no.

2 – Add three attributes to that Data Source to represent:

  • API Key
  • Endpoint
  • Location

3 – Now go to the Data Source’s “Entity Behaviour” tab and paste the provided code snippet (end of page)

4 – Create a new “Generic Entity” with the following configurations:

  • Add provided code to “ReadList”;
  • Add a Behaviour Namespace (for HTTP requests)

5 – Add the ListView Web Component to a dashboard of your choosing;

6 – Apply provided code for UI Behaviour on the dashboard’s Initialize();

7 – Build your model and visit your application

Code Snippets

Entity Behaviors (3):

if(this._Context.Operation.IsNew){

this.ApiEndpoint = "http://dataservice.accuweather.com"; this.LocationCode = "274087"; this._code = "Default"; this._name = "Accuweather API";

}
ReadList Code (4): List<IDictionary<string, object>> forecasts = new List<IDictionary<string, object>>(); var dataSource = GetDataSourceConfiguration(); var client = new System.Net.Http.HttpClient(); var baseEndpoint = new Uri(dataSource.ApiEndpoint); string relativeUrl = $"/forecasts/v1/daily/5day/{dataSource.LocationCode}?apikey={dataSource.ApiKey}&details=true&language={this._Context.Operation.Language}&metric=true"; var forecastsEndpoint = new Uri(baseEndpoint, relativeUrl); var requestResult = client.GetAsync(forecastsEndpoint).GetAwaiter().GetResult(); string responseBody = requestResult.Content.ReadAsStringAsync().Result; if (!requestResult.IsSuccessStatusCode) throw new Exception($"Could not retrieve weather forecast (Endpoint: {forecastsEndpoint}): " + responseBody); var response = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseBody); var responseData = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(response["DailyForecasts"].ToString()); foreach (var dayForecast in responseData) { forecasts.Add(dayForecast); } return (responseData.Count, forecasts); AccuweatherDto GetDataSourceConfiguration() { var apiClient = this._Context.CreateApplicationHttpClient(); var dataSourceResponse = apiClient.GetAsync($"{this._Context.Operation.DataSourceType}/Default/{this._Context.Operation.DataSource}").Result; return dataSourceResponse.Content.ReadAsAsync().Result; }
UI Behaviour for Initialize (6): const apiClient = this._context.createApiHttpClient(); return apiClient .doPost(`/api/v1/${this._context.tenant.code}/${this._context.tenant.environmentCode}/Application/Queries/WeatherForecastsQuery/Default`, {}) .then( weatherForecast => { if(Array.isArray(weatherForecast.data) && weatherForecast.data[0]){ let forecastData = []; for(const dayForecast of weatherForecast.data){ forecastData.push({ title: `${dayForecast.temperature.Minimum.Value}º${dayForecast.temperature.Minimum.Unit} | ${dayForecast.temperature.Maximum.Value}º${dayForecast.temperature.Maximum.Unit}`, description: `${dayForecast.date} ${dayForecast.day.LongPhrase}`, thumbnail: {address: `https://www.accuweather.com/images/weathericons/${dayForecast.day.Icon}.svg`}, link: {address: dayForecast.link, target: '_blank'}, }); } this.weather.value = forecastData; } });
✉ Signup for our Newsletter to get our weekly news