In OutSystems, it's possible to integrate with a REST API in order to call an external service.
First, a quick recap: a REST API is a web service provided by an agnostic technology somewhere over the internet. A web service has methods, and methods can be called and can have inputs. A call to a method has a response, which varies depending on the method's type.
For this scenario, we want to get the current weather data into an OutSystems application. We need to find an external service provider such as openweather.org to do that.
Here, it's required to have an account and generate an API key as a security measure to use the open weather services.
We can now choose the service method that most fits our needs. In this case, the current weather service seems to do the trick.
So let's start!
In the Service Studio, go to the Logic tab, where you will find the Integrations folder.
Right-click the REST option and select consume REST API.
There are two options to choose from: Add a single method or add multiple methods. Since the latter needs a swagger file that we do not have or need at this point, let's use the first approach.
We need to set the URL to call the service: let's get it from the openweather.org website.
Notice that some optional inputs can be used, such as the units. We will add it manually to the call, using a similar syntax as for the other inputs.
Once you paste it, notice the words between curly brackets: those are input variables of this service method.
To test this service method call, we use the city name and the API key as inputs. Let’s provide values for the inputs: Lisbon as the city name and the previously generated API key from the website.
Once you click the test button, the response will appear below, and you can check the OK status returned by the service.
So, it seems like the call was executed successfully.
Next, you can check the elements that are part of the response and will be used by the platform to generate the required structures holding the response at runtime. Copy this code to the response body of the configuration window so that the platform can do its magic.
After you click that button, finish the process, and the service is ready to be used.
Notice all the structures created to allow the service to provide and hold its response.
How to use it? Call it like any other OutSystems action.
Let's go ahead and call this method in a previously created screen.
Once we drag and drop the REST API method and define its inputs, we display the attributes' values on the screen.
So in this data action, we will set the city name to Lisbon and paste the API key again. These are the values used at runtime.
After the call to the GetWeather service, just set the required outputs to later use on the screen. In this case, we will display the icon and the temperature, which are the outputs being set.
Then, we can use the icon URL described in the openweather.org website documentation on the screen.
Click the one-click publish button and open it in the browser.
Now we can see information about the current weather that is coming from an external service. We are all set.