跳至主要内容

Dynamic Parameters in Oracle APEX REST Data Source Synchronizations

Cloud Nueva Blog (Oracle, APEX & ORDS)

Introduction

In this post, I will describe how to dynamically pass parameters to Oracle APEX Rest Data Source Synchronizations. APEX REST Source Synchronizations allow you to declaratively Sync data from an REST API to a local table. Please read this post for more on why you may want to do that.

This works well if you want to synchronize small to medium-sized data sets. The challenge comes when you want to sync larger data sets. For example, you would not want to Synchronize 1 million records over a REST API hourly. We need a way to run our Sync every hour but only pick up records that have changed in the last hour. Oh, and by the way, we want to do this declaratively!

APEX REST Source Synchronization Steps to the Rescue.

Use Case

We have been tasked with Synchronizing Time Entries from the Harvest Time Entry SaaS Application to a local Oracle DB table for use in an APEX Application. We must also:

  • Ensure our local table is never more than 4 hours out of date.

  • Allow time entries to be changed for up to 7 days after their initial entry.

Finally, the Harvest instance already has over 100,000 time entries, so we cannot Sync all of them every four hours.

Although this example is specific to Harvest, this approach will work for most REST-based APIs.

REST Source

The first thing we need to do is to create a REST Source that we can use to fetch time entries from Harvest. Enter a static ID and the Harvest API Endpoint URL:

Create APEX REST Source 1

Click Next and confirm the Base URL and Service URL Path:

Create APEX REST Source 2

Next, configure Pagination for the Harvest API:

Create APEX REST Source 3

Then, set up a new APEX Web Credential. Note: For Harvest, when entering the 'Value', you need to enter 'Bearer ABC', where ABC is your Harvest API token.

Create APEX REST Source 4

Then, for this particular Harvest API, we need to click 'Advanced' and enter an 'HTTP Header' parameter indicating our company ID (Harvest-Account-ID).

Create APEX REST Source 5

Then, we can click Discover to verify that our REST Source setup works.

Create APEX REST Source 6

We should end up with a REST Source definition that looks something like this.

Create APEX REST Source 7

Add Parameters

For Harvest, we are going to Sync time entries between two dates. Harvest provides two 'URL Query String' parameters for this purpose: from and to. We must set these parameters up in the REST source we just created. The screenshot below shows all three parameters.

Identify a Unique Key

Before moving on to Synchronization, we must identify a Unique Key in the Data Source for our REST Source. Edit the REST Source and click 'Edit Data Profile':

APEX REST Source Data Profile Unique Key 1

Then, turn on the 'Primary Key' switch for the column that is a unique identifier for each record the REST API returns. In this case, it is the ID column.

Identifying a unique key allows us to perform a Merge operation when syncing records from our REST source.

APEX REST Source Data Profile Unique Key 2

That completes the REST Source definition.

Setup Synchronization

Now that we have a working APEX REST Data Source, we can configure it to Synchronize to a local table. Start this process by clicking on the 'No' link under the 'Synchronized' column for the REST Source.

Create APEX REST Source Syncing 1

Then, enter the table name where you want the data Synced. APEX is going to create this table for you. Alternately, you can create your own table and select it here. If you create your table, you must ensure your table column names match those in the 'Name' column of your REST Source Data Profile.

Create APEX REST Source Syncing 2

Create APEX REST Source Syncing 3

After clicking 'Create Table', your REST Source Synchronization is ready to run.

Create APEX REST Source Syncing 4

Note that I selected 'Merge' as the 'Synchronization Type'. Merge will use the Primary Key we identified and perform a SQL Merge (Insert / Update) on our table when performing the Sync. This makes sense for time entries because they could change, and we want to update our local table with the latest values.

Schedule the Sync

Let's schedule the REST Source Synchronization to run every four hours:

Scheduling the APEX REST Source Synchronization

Save and Run the Synchronization to ensure it works as expected and that records are synced to the local table.

REST Source Sync Log

Our REST Source and the Synchronization are now ready to go. The problem is, as it stands now, we are going to Sync all the time entries every four hours.

Passing Dynamic Parameters

Finally, we have come to the point of this blog post!

We must create an APEX REST Source Synchronization Step to pass parameters to the REST Source at runtime. To do so, navigate to the Synchronization page for the REST Source.

REST Source Synchronization Step 1

In this case, we want to fetch time entries between 7 days ago and today. To do this, we can use a PL/SQL expression to get the date from 7 days ago, return it to the from parameter, and get today's date for the to parameter. The screenshot below shows the REST Source Synchronization Step with the two parameters configured.

Options for 'Value Type' are:

  • Static Provide a static value.

  • PL/SQL Expression Run a PL/SQL Expression as we did above and pass the result to the REST API when the REST Source runs.

  • SQL Query (return a single value) Pick up a value from a table and pass it to the REST API when the REST Source runs.

  • PL/SQL Function Body Run PL/SQL code to figure out the parameter value (maybe even call another REST API) and pass it to the REST API when the REST Source runs.

If we run the Synchronization and then navigate to Monitor Activity > Web Service Activity Log, we can see that APEX calculated the from and to parameters at run time and passed them on to the Harvest REST API via the query string in the URL.

APEX Web Service Activity Log

Pretty Cool!

Conclusion

In this post, I showed you how to use APEX REST Source Synchronization Steps to pass values into your REST APIs at runtime. This powerful feature is invaluable when performing incremental synchronization of REST APIs to a local table in your Oracle Database.

© 2024 Cloud Nueva Blog (Oracle, APEX & ORDS)

8 The Green, Ste 300, Dover, Delaware, 19901

Unsubscribe
Write on Hashnode

评论

此博客中的热门博文

Scripting News: Tuesday, June 11, 2024

Scripting News: Tuesday, February 13, 2024