Communicating with RESTful Web Services

Questions about and code samples for automation process code snippets within Select.
Post Reply
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Communicating with RESTful Web Services

Post by BobRichards »

Automation Snippets can communicate with web services for the purpose of passing short messages or data packets. This can open a world of simple mechanisms of passing short messages to trigger external services to perform activities - all set into motion with the impressive logic capable in Automation Snippets. There are other possible activities but you remember to keep the implementation body short.

The provided example has undergone limited testing. It is up to the user to understand the operations used and use full production best practices including try/exception blocks and thorough error testing.

The example below performs two REST API calls. It is assumed that it is used inside an Action for when "Every time an order is saved" so that Context is the order. The first section gets a string with a GET call to external service. The second sends the order number and version number to the external service with the order property "Information".

Code: Select all

from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *

import clr
import System
# Load external assemblies.
clr.AddReference('System.Net.Http')
from System.Net.Http import HttpClient, StringContent

def SendMessage():
	client = HttpClient()
	client.DefaultRequestHeaders.Add('Accept', 'application/json')
	
	# Read a string from a RESTful server using GET.
	uri = Uri('http://localhost:63986/api/ID/1')
	response = client.GetStringAsync(uri).Result
	Context.RelatedOrders = response
	
	# Write the Order and Version Number to a RESTful server embedded in URL with POST.
	#  (i.e. will send order/version as "2018110009/25")
	uri = Uri('http://localhost:63986/api/OrderSaved?value=' + Context.Information)
	content = StringContent('')
	response = client.PostAsync(uri, content).Result

	return

SendMessage()
Bob Richards, Senior Software Developer, SoftPro
jturek
Posts: 14
Joined: Fri Jul 20, 2018 6:17 pm

Re: Communicating with RESTful Web Services

Post by jturek »

I have getting Null value while send the Context object in REST API method as parameter. Please find the below Automation snippet and REST API service method code.
The Automation snippet code successfully executes the REST API service method but not able to fetch the order details by using Context parameter. Please correct me if anything incorrect.

Automation Snippet :

Code: Select all

from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *


import clr
import System
# Load external assemblies.
clr.AddReference('System.Net.Http')
from System.Net.Http import HttpClient, StringContent

def SendMessage():
	client = HttpClient()
	client.DefaultRequestHeaders.Add('Accept', 'application/json')	
		
	# Write the Order details to a RESTful server embedded in URL with POST.
	uri = Uri('http://192.168.254.175:8080/api/Order/GetOrdeDetails?values=' + Context)
	content = StringContent('')	
	response = client.PostAsync(uri, content).Result

	return

SendMessage()
REST API Method:

Code: Select all

 	[HttpPost]
        public async Task<bool> GetOrdeDetails(dynamic values)
        {
        
        }
The requirement is once the Order Status changed in SoftPro need to send Order Number and Order Status to REST API method And also We have to send other details too based on the Order Status value. Please help us to select right approach for the same.
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Communicating with RESTful Web Services

Post by BobRichards »

"Context" is a complex object. You can't marshall this object across the wire directly. You need to break it up into simple object types - which is why I sent "Context.Information" (a string) to the REST API. Send the parts you need to the API or use a container such as XML or JSON and send it.
Bob Richards, Senior Software Developer, SoftPro
Post Reply