Synchronous HTTP Post to PeopleSoft Integration Broker using Python
By Chris Malek | Thu, Apr 4, 2013
This is a follow-up post for HTTP Post to PeopleSoft Integration Broker using Python. In that article, we showed how to post to an asynchronous service operation. In the PeopleTools architecture, when you post asynchronously, the PeopleSoft code that triggers is NOT ran instantaneously. It will run at some point in the future or could be canceled by an administrator and never run.
If your integration scenario is one that you need to ensure that PeopleSoft processes the message and your client software needs the response immediately, then you need to create a synchronous service operation. This type of service operation is slightly different on the PeopleSoft setup and the PeopleCode that gets triggered is also slightly different. The Python client code is almost identical in regards to actually making the HTTP calls and the message structure. The Python code in this article will get more functionality than we had in the asynchronous example.
In this article:
- We are using Python 3.3 standard libraries.
- We are posting to an Synchronous Service operation in PeopleSoft
- The PeopleCode that is executed will examine the XML sent and do something different depending on the content. The response will also be different based on the Python input.
- We are not doing too much error handling in the python script.
The PeopleTools Setup
First let’s setup the PeopleSoft side.
Step 1 - Setup a Node to represent your python program
First we need a node to represent your application. This can be done here: PeopleTools > Integration Broker > Integration Setup > Nodes
|Default User ID||PS (or some valid user in your system. )|
|Password||secret (Note this is NOT the password for any user. This is more like a “key” for the node)|
Step 2 - Create a New PeopleSoft Message Object
Now we need a PeopleSoft message object that will represent the XML that the python program will post.
PeopleTools > Integration Broker > Integration Setup > Messages
Step 3 - Create a new Service
In this example, we create a new service. You can easily re-use another service if you wish.
PeopleTools > Integration Broker > Integration Setup > Services
Service Attribute | Value —|– Name | CHG_TEST
Step 4 - Create the Handler PeopleCode
Now we need to create some PeopleCode that will run when a new message is posted to the integration broker for this Service Operation.
- Create an Application Package and Class with the following Package:Class Naming: CHG_I_TESTER:syncTester
- Paste in the following code into the Class:
import PS_PT:Integration:IRequestHandler; class syncTester implements PS_PT:Integration:IRequestHandler method onRequest(&MSG As Message) Returns Message; end-class; method onRequest /+ &MSG as Message +/ /+ Returns Message +/ /+ Extends/implements PS_PT:Integration:IRequestHandler.OnRequest +/ Local XmlDoc &xmlDocFromPython; Local XmlNode &requestRootNode; &xmlDocFromPython = &MSG.GetXmlDoc(); &requestRootNode = &xmlDocFromPython.DocumentElement; /* Setup response xml body */ Local Message &response; &response = CreateMessage(Operation.CHG_SYNC_TEST, %IntBroker_Response); Local XmlDoc &xmlout; Local XmlNode &childNode; &xmlout = CreateXmlDoc(""); Evaluate Lower(&requestRootNode.NodeName) When = "helloworld" &childNode = &xmlout.DocumentElement.AddElement("helloworld").AddText("Hello Python"); Break; When = "activeusercount" Local integer &ucount; SQLExec("SELECT COUNT(*) FROM PSOPRDEFN WHERE ACCTLOCK = 0 ", &ucount); &childNode = &xmlout.DocumentElement.AddElement("activeusercount").AddText(String(&ucount)); Break; When-Other &childNode = &xmlout.DocumentElement.AddElement("error").AddText("I do not understand. Please try again. You submitted: " | &requestRootNode.NodeName); Break; End-Evaluate; &response.SetXmlDoc(&xmlout); Return &response; end-method;
- This code is different than our asynchronous example. We have to
implement a different class and method. A synchronous request must
- You will see that the code examines the Root Node of the XML and
will run a different branch of code depending on the input.
- “helloworld” - If the root node is “helloworld” we will just echo back a “Hello Python” message.
- “activeusercount” - If the root node is “activeusercount”, then we will do a count of all unlocked users in the system and return that in a response
- If any other root node is passed in we return an error message that the input was not understood and we echo back the root node name in the message.
This will probably make a little more sense later in the article when we look at the request and response from the Python client.
Step 5 - Setup new Synchronous Service Operation
Now we need to setup the actual Service Operation. There are several steps here.
PeopleTools > Integration Broker > Integration Setup > Service Operation
|Service Operation Attribute||Value|
|Queue Name||IB_EXAMPLES (or create a new queue )|
- Click on the “Service Operation Security” link
- Input a permission list that you have on your user profile.
- Additionally, assign permission lists grants to a permission list that is on your “default user id” from your node definition.
Now we need to hook the CHG_I_TESTER:syncTester application class to execute when a service operation is posted. We do this on the Handler tab of the Service Operation.
|Service Operation Handler Attribute||Value|
|Handler Name||Test (This value will be ovewritten by peoplecode)|
Now we need to setup the routing to make this node able to send Service Operations.
|Service Operation Routing Attribute||Value|
|Receiver Node||HRMS (whatever your default local node is )|
Now our PeopleTools system should be ready to receive messages from some HTTP client.
- Download the posttopsoftsync.py python code
- Replace the following pieces of code for your environment
- Change the assignment to
urlwith your PeopleTools host.
- Change any
headers['xxx']assignments to reflect the nodes and other attributes for your installation.
- Change the assignment to
Running the Script
The Python script is written to handle a command line flag. The flag/command passed in will get submitted to the Integration Broker in an XML root node. The PeopleCode will look for two different root nodes and do two different things based on those commands passed in. If some other command is passed in, then the PeopleCode will just return a generic response telling you there was an error. Let’s look at it in detail.
Let’s first try the “helloworld” command.
python3 posttopsoftsync.py helloworld
The python script will submit the following XML to the Integration Broker.
This will cause the “helloworld” branch of the PeopleCode to run and PeopleSoft will return this XML.
If you call the Python script with the “activeusercount” command like this:
python3 postToPsoftSync.py activeusercount
It will submit the following XML to the Integration Broker:
That will cause the PeopleCode to do an active user profile count and return some XML formatted like this:
If you call the Python script with some junk that the PeopleCode cannot understand like this:
python3 posttopsoftsync.py blah
The XML submitted to the Integration broker will look like this:
The PeopleSoft XML response will look like this:
I do not understand. Please try again. You submitted: blah
So what did we learn? We learned that you can pretty much submit any XML to a synchronous Service Operation and have it respone to what was submitted. Of course, the functionality presented here is completely useless. You can extrapolate the example here to do whatever. The Python client could be sending in actual data in the XML. The PeopleCode could actually parse the data and update something in PeopleSoft and return a response. It is up to the two systems to agree upon an XML format to exchange.