Communicating with RESTful Web Services
Posted: Wed Nov 07, 2018 11:28 pm
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".
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()