Use ORDS to run the CodeCard backend on your own free Oracle Cloud Database

ORDS (Oracle Rest Data Services) is a fast and easy way to host a REST API you can use to take full control of your Oracle CodeCard.

In this post I’ll walk through how to create the ORDS API on your own Always Free Oracle Cloud Database.

Prerequisites

    1. Create a compartment in your Oracle Cloud Account (Optional but recommended)
    2. Create an Always Free Autonomous Database on the Oracle Cloud
    3. Go to the Service Console for your new database, click on “Development” and save the URL in the “RESTful Services and SODA” section.
    4. CodeCard Sketch changes: Howto disable fingerprint check and increase REST URI size
    5. You Went to Oracle Open World 19 and got a Code Card, now what?
      Ignore the fingerprint settings since you just disabled that in the previous step but add the following.
      Replace the below {{URL}} with the URL you saved above.
      Make sure there’s only one / between your URL and cc.  For example:
      buttona1=https://asdr34edyjtyi4j-codecard.adb.us-ashburn-1.oraclecloudapps.com/ords/cc/functions/master

When all of that is done, go to the Development section of the Service Console for your ATP instance and click “SQL Developer Web”.  Sign in as admin with the admin password you created in step 2.

Database Setup

As Admin, create the CODE_CARD schema.

Execute the following code in the worksheet.  Use a good password and remember it for later.

This will create the new CODE_CARD schema and REST enable the schema so you can use it with ORDS.  Notice the p_url_mapping_pattern is set to ‘cc’, this will be part of the REST URI in the following sections.

oraclecloudapps.com/ords/cc/
As code_card, create the database objects

Switch to the code_card schema by changing the URL In your browser.  Replace ‘admin’ with ‘cc’.
...oraclecloudapps.com/ords/admin/_sdw/?nav=worksheet  to ...oraclecloudapps.com/ords/cc/_sdw/?nav=worksheet

Log into SQL Developer Web as code_card using your new password.

In a worksheet, use the following to create the tables.

Oracle REST Data Services API

You will need two rest endpoints.   One to POST the setting for each button press and one your CodeCard will use to GET those settings.

There are multiple ways you can create an ORDS API, this post will walk through how to create the API using PL/SQL.  I will explain the settings that are most important for the CodeCard interface.  For the other settings, you can find the full documentation here.

This step was completed above

When you created the new schema above, you also REST enabled the new schema with this PL/SQL.

This command was included in the above step so that you could log into SQL Developer WEB as code_card.

The following will explain the individual PL/SQL procedures used to create the REST API.

If you want to skip the explanation, you can jump straight to the complete code included below.

Define a module

A module is a collection of related REST services.  Think of a module as a package that contains REST endpoints.

Create a module called ‘functions’.

The base path is set to ‘/functions/’, this will be part of the REST URI.

oraclecloudapps.com/ords/cc/functions/
Define a Template

A Template is the endpoint for your REST API.

The pattern is set to ‘master’, this will be the final part of the REST URI.

oraclecloudapps.com/ords/cc/functions/master
GET request handler

The CodeCard sends a GET request to the REST API to retrieve the settings assigned to a specific button (A or B) and press function (long or short).

In order to handle this request we need to define a GET handler.

Using a source type of  ‘json/collection’ would allow you to use a simple SQL query and ORDS would handle all of the JSON formatting for us.  This would be easier.

However, we want to be able to return a functioning screen layout to the CodeCard even if there’s an error.  Using a source type of ‘plsql/block’ lets you create and return a custom APEX_JSON object even if there’s an error.

Parameters for the bind variables

The PL/SQL code uses 3 bind variables that are mapped to incoming request header values.

The bind variables are mapped to the header values by defining parameters. These parameters will all be assigned to the ‘functions’ module, ‘master’ template and  ‘GET’ handler.  They will be passed ‘IN’ through the ‘HEADER’ as a ‘STRING’ type.  The value of the incoming header defined in ‘p_name’ will be mapped to value of the PL/SQL bind variable defined in ‘p_bind_variable_name’.

  • X-CODECARD-ID is the id assigned to your CodeCard.
  • X-CODECARD-BUTTON-LABEL will either be the A or B button.
  • X-CODECARD-BUTTON-FUNCTION will be 1 for a short press or 2 for a long press.
Handle POST requests

The CodeCard sends a POST request to the REST API to store the settings assigned to a specific button and press function.

In order to handle this request we need to define a POST handler.

This PL/SQL block will either create a new record or update an existing record for the incoming CodeCard id, button label and button function.

Parameters for the bind variables

The parameters for the POST handler are the same as for the GET handler.

Register template (Optional)

The register template is used by the CodeCard designer to map your name to your CodeCard id, if you choose to enter it.  Another post will detail how to install the CodeCard designer in an always free Oracle compute instance.  If you plan on implementing the designer, include this handler template and handler.  Even if you do not plan on implementing the designer, it won’t hurt anything to include these objects.

If you are piecing all of the above sections together you will need to execute a ‘COMMIT’ to save the changes to your database.  Or, you can use the following.

Full ORDS PL/SQL code

In a worksheet use the following code to create the Oracle REST Data Services API.

Setup a button press

One final piece of information you’ll need is your CodeCard’s id.

  1. Turn your CodeCard on.
  2. Press and release the A and B buttons at the same time.  Your CodeCard will display a barcode with a 12 character id string under it.
  3. Save that ID string.

Testing

POST

If you are using a tool such as Postman to test your API you will need to create a POST request with the following pieces.

  • URI:  {{URL from the prerequisite section}}/cc/functions/master
    For example: https://asdr34edyjtyi4j-codecard.adb.us-ashburn-1.oraclecloudapps.com/ords/cc/functions/master
  •  Headers
    • X-CODECARD-ID: Us the ID string from above
    • X-CODECARD-BUTTON-LABEL:  either ‘a’ or ‘b’
    • X-CODECARD-BUTTON-FUNCTION: use ‘1’ for a short press or ‘2’ for a long press
    • Content-Type: application/json
  • Body: use a JSON object like this
     

If you’d prefer to use a CURL command (replace the {{ }} sections with your values):

With either method, you should receive a response with a status of 200 and a JSON object in the body like this

Test GET

Assuming you completed the prerequisite section, you should be able to short or long press the A or B button on your CodeCard and your new template settings will be displayed on your screen.

You can also test the GET handler by sending a GET request to the same URI used above.  Use the same values for the headers, you do not need a body.

  • X-CODECARD-ID: Use the ID string from above
  • X-CODECARD-BUTTON-LABEL:  either ‘a’ or ‘b’
  • X-CODECARD-BUTTON-FUNCTION: use ‘1’ for a short press or ‘2’ for a long press

If you’d rather use CURL (replace the {{ }} sections with your values):

You should receive a JSON object like the one you sent in the body of your POST.

If the GET request works but your CodeCard doesn’t, go back to the prerequisite section and make sure you followed steps 4 and 5 correctly.

Have Fun

Once you get everything working you could experiment with changing the ORDS GET handler to do something else when a button is pressed.

  • Create a new table and log which button is pressed and if it’s a short or long press.
  • Run a query on a table and return the results in the bodytext.  (There are some size limits.)
  • Execute a stored procedure that does whatever you want it to do.
  • Make a different module/template/GET API for each button/press.

Leave a Reply

Your email address will not be published. Required fields are marked *