Learning Websub (Part 3) : Integrating OpenWeatherMap API

Ayesh Almeida
2 min readMar 10, 2023

--

Photo by Marvin Meyer on Unsplash

In my previous article we completed the basic weather-reporter-hub implementation. However, in that implementation weather reports were generated using predefined sample texts. In this article we will be connecting the weather-reporter-hub to a publicly available weather-API to obtain up-to-date weather reports..

The OpenWeatherMap is a web service that provides access to weather data for any location on Earth. It offers a variety of weather data, including current weather conditions, hourly forecasts, daily forecasts, and historical data. The API is available in both free and paid versions, with the free version providing access to basic weather data and the paid version offering more advanced features.

In this application we will be using Current Weather Data API to retrieve relevant weather reports.

The API can be invoked using simple HTTP GET request. Following is a sample request and a response using cURL command.

The API_KEY serves as an authentication token that is supplied by OpenWeatherMap. To learn how to obtain an API_KEY, please refer to the FAQ section.

The response from the OpenWeatherMap API is in JSON format. To simplify the integration lets create set of Ballerina records which can be mapped to the response.

Here, WeatherReport record serves as the main response payload type. Then lets use an http:Client to send requests and receive responses from OpenWeatherMap API.

Now, we can update the weather report distribution logic.

Complete code changes for weather report distribution logic can be found here.

In this article we have discussed how to connect our hub implementation to 3rd party APIs. In the subsequent article, we will explore ways to enhance our hub implementation by integrating the Apache Kafka message broker in an efficient manner.

--

--

Ayesh Almeida
Ayesh Almeida

Written by Ayesh Almeida

A Tech Enthusiast who wants to build simple solutions for complex problems.

No responses yet