Creating a JSON Web Service in Valence

REST-based web service, with the other end being the “consumer” of said service. Consider this scenario:  You want to give a VIP customer the ability to have their system automatically contact your system to obtain real-time shipment status updates for specific orders being fulfilled by your company.  And as luck would have it, you’ve already done the legwork to create an order inquiry Valence app on your system, complete with an RPG program that provides the necessary data for these shipments in JSON format.  So with some minor tweaks to the code, you can give a portion of the JSON response to the customer as if they had clicked on a button in your Valence app. The first step is to adjust your RPG program to detect when the request is coming from a web service and adjust its response accordingly.  One way to do this is by looking at the session ID associated with the call.  Valence automatically creates a special session ID record for any web service, and the following code in your RPG program could be used to identify web service calls:

sid=vvIn_char('sid'); 
vvSecure_isLoggedIn(sid:*off:*off:*off:*omit:mode);
if mode='2';
  // this is a web service call!
  ...
endif;

(In the code above, “sid” is a 64-character alpha field and “omit” is a 1-character alpha field) When your program has identified a call as coming from a web service, your code should probably validate that the consumer of this particular web service is authorized to received information for, say, the order number being passed. If you have set up a unique Valence user for each unique consumer, you could do this by pulling in the user ID as fifth parameter on the vvSecure_isLoggedIn procedure call (replacing *omit), determine which customer the user ID corresponds to and then proceed accordingly. Once you have the program logic in place, the next step is to set it up as a web service in the Valence Portal Admin program, as described in the Valence Web Service Guide.  

You would basically create a special URL segment (such as “get_order_status”) that would cross-reference to your RPG program. This special segment would become part of your full URL for your customers to reach your web services, with each customer set up as a “consumer” cross-referenced to a specific Valence user ID and environment.   The final URL for a consumer, which you can see by clicking on the specific consumer record row, might look something like this: https://www.acme.com/service/get_order_status?key=B183249A6178BAF7AD1204E627797069&order=123456

The base of this URL (https://www.acme.com) and the web service identifier (/service) are set in the Portal Admin > Settings app, in the “Login Method” section.  The base portion comes from the “Secure URL to access Valence externally” setting if specified, otherwise it is pulled from the “URL to access Valence login page” setting; The service portion comes from the “Web service identifyer in URL” setting. As illustrated in this example, when granting outside access to your Power Server running IBM i, you should take the time to set up a secure SSL-based connection for your consumers.  Some step-by-step instructions for setting this up on your system can be found here. Also note that should you ever need to “turn off” or disable a customer’s access to a specific web service, you need only remove their consumer key from the list.  You could also disable all calls to a particular program by un-checking the “Enabled” checkbox.