REST data sources with Oracle APEX: setting an external REST data source that needs an authentication token and persist the incoming data on a local table
There are a lot of tutorials and examples out there about how to set up a REST external data source on an Oracle database, most of them are focused on the core "how-to" connect the database to an external service, avoiding any unnecessary complexity in explaining the core concepts. But the real world is not that easy, and when it comes to implementing the REST data source you really need, it might not be as simple as the overused USGS earthquake API service.
There's a reason why the USGS earthquake API is used to teach how to use REST API calls with any platform, from Android native apps to Oracle, and it's because it is very handy and therefore very useful to learn the core concepts.
But what if your REST API needs an authentication token to be used?
After reading (and watching) some tutorials, I applied them to my needs: that is to set up a RapidAPI service as an external data source in an Oracle APEX application. In my case, it was the COVID-19 API which is a free service, but, anyway, you need to use a personal RapidAPI token to use it. I had previously subscribed to the service because I needed it for a mobile Flutter application. After that, I decided to integrate it into an OCI ADB instance as an external data source. Using APEX makes the whole thing quite straightforward, but, as always, the devil lies in the detail. What I mean to say, is that when you need an authentication key to call a REST API and the key has to be passed directly in the URL, the whole matter is relatively trivial: you'll end up with a URL request like this https://restservice.api.com/V1.0/getdata?key=[your token]&op=something&id=12345. Easy like riding a bike. But, what if you need to set the key into the request header instead?
So, in the next steps we'll:
- create a new APEX app on our ADB
- set up the COVID-19 API as an external REST data source
- call the REST service using an authorization key released by RapidAPI
- Bonus: pump the REST data into a table using a scheduled task that runs on a regular basis, to keep the local data table updated with the latest outcome from the REST service.
Creating the APEX application
Assuming that you have already set up an APEX instance on your free ADB, head to the management page of your ATP/ADW, open the APEX instance then launch APEX and sign in:
On the main page of APEX, click on "Create" then "New Application" and leave all options to their default values. Choose a name for your app (if you like you can change the theme and the usual rocket icon), then click on "Create application"
Analyze the API call with Postman
- our user key
- the country for which return the data (optional)
- KEY=country
- VALUE=Germany
- KEY=X-RapidAPI-Key
- VALUE=[your own token]
now click on the "Send" button and you'll get the response body with the information in JSON format:
Now it's time to get things working into APEX. In the application, go to the "Shared components" module and in the "Data sources" pane (down on the left), and select "REST data sources"
click on the "Create" button and start the wizard.
On the next page, we set up the authentication parameters. Set "Authentication Required" to ON, and, as authentication type, select HTTP Header from the list. This shows up two more options, and, guess what? they are the name of the header parameter and its value, in other words:
- Name=X-RapidAPI-Key
- Value=[your own personal key from RapidAPI]
same as seen before in Postman. Now click on "Discovery" and let the magic happens
the next page shows a sample result of the REST API call. Here you can see the data returned by the call in the "Data" tab, as well as the metadata that APEX automatically built from the JSON response, in the "Data Profile" tab.
We are still missing the "Germany" parameter, it'll come soon. Now click on "Create REST Data Source".
At this point, the REST data sources management page shows up and we can see the data source just created. Click on the "REST Source Name" to open and edit its properties.
Scroll down the page to the "Operation" sections, where we can see the only defined operation, which is a GET verb. On the right there's a button to test the REST call, let's try it.
As a result, we get all the COVID statistics returned by the call to the API endpoint and, as we can see, they are about all the countries covered by the service worldwide.
Now let's try to define the parameter to gather results only for Germany. Click on the "Go Back" button, choose the "Parameters" tab, and then click on "Add Parameter".
- choose "Query String variable" from the Type drop-down list.
- set name = country
- set value = Germany
- set direction=in
Loading and saving the REST data in a database table
- Local table owner = [select your schema]
- Synchronise to = New Table
- Table name = [choose a name for the table, e.g. REST_DATA_LOCAL]
- One button to show the DDL statement auto-generated to create the table where every column is mapping an attribute of the JSON object returned from the REST call. You can change the table definition and save the script.
- One button to run the DDL script and create the new table.
- Synchronization type: since we are retrieving statistic data for the latest available day, we can choose "Append" to keep older data or "Replace" if we want to keep only the latest ones.
- Synchronization schedule: how often do we want to update the data? Click on the "Schedule Builder" to pop up a modal window and set the schedule options:
- Frequency: between weekly, daily, hourly, minutely
- Interval: sets how often the recurrence repeats. For example, if we choose a daily frequency with an interval of 1, that means every day, with an interval of 2, that means every 2 days and so on.
- Execution Hour: the hour at which the synchronization runs, between 0 and 23 (no AM/PM required😀)
- Execution Minute: well, pretty clear
If you try, you'll see that changing the Frequency option changes the options available, e.g. choosing Weekly shows up the days of the week.
- Daily as the value for Frequency
- 1 as the value for Interval
- specify a time following the one reported into the JSON object, to make sure we have the latest information available.
Now a button saying "View in SQL Workshop" is visible on the right of the page.
This pops up the database object browser showing all the informations about the table we have just created to store the data returned from the REST service.
In the "Data" tab you can see the data has been retrieved from the REST service call and saved into the table.
Have fun!





























Commenti
Posta un commento