Asynchronously calling the REST Service with XML

  • Updated

See Also: irServer - Rule Execution Service

The following example demonstrates calling the irServer - Rule Execution Service REST interface with an XML based Entity State.

static void Main(string[] args)
{
  // The name of the ruleApp in the Catalog
string ruleApp = "MortgageCalculator";
// The address of the Rule Execution Service
string ruleExecutionServiceURI = "http://localhost/InRuleRuleEngineService/HttpService.svc";
// The Entity Name:
string entityName = "Mortgage";
// The name of the RuleSet to execute, or use 'ApplyRules' if found to be null.
string ruleSetName = "PaymentSummaryRules";
// The Entity State as XML text.
string entityStateXMLInput = @"<?xml version='1.0' encoding='utf-8'?>
<Mortgage xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <LoanInfo>
      <Principal>53000</Principal>
      <APR>7.625</APR>
      <TermInYears>15</TermInYears>
  </LoanInfo>
  <PaymentSummary/>
</Mortgage>";

// A console application's main() entry point cannot be declared as async, so here
// we are creating an Async task to execute the Async method, then we will wait on
// the result.
Task<string> httpTask = AsyncRestXmlCatalog(ruleExecutionServiceURI, ruleApp, entityName,
entityStateXMLInput, ruleSetName);


// Calling the Result property of a Task object will block this thread until
// the Task has had a chance to complete in it's worker thread.

string httpTaskResult = httpTask.Result;

Console.WriteLine(httpTaskResult);

// Now let's extract the <EntityState> element, which has been encoded.
// The Value property of the XElement will automatically decode the contents
// and expose it as a string, which lets us look at the inner XML document "EntityState".
// It's worth observing that while the input was encoded as UTF-8, this result is in
// UTF-16.
XDocument xmldoc = XDocument.Parse(httpTaskResult);
string xmlEntityStateOutput =
     (from n in xmldoc.Descendants()
      where n.Name.LocalName == "EntityState"
      select n.Value).FirstOrDefault();

// Wonderful!  Now lets take that concept further and extract just the
// <PaymentSummary> node from the document, which gives us our computed "answers".
XDocument xmlentitystatedoc = XDocument.Parse(xmlEntityStateOutput);
  string paymentSummaryXml = (from n in xmlentitystatedoc.Descendants()
                            where n.Name.LocalName == "PaymentSummary"
                            select n.ToString()).FirstOrDefault();
 }
private async static Task<string> AsyncRestXmlCatalog(string ruleExecutionServiceURI,
                                                      string ruleApp,
                                                      string entityName,
                                                      string entityStateXMLInput,
                                                      string ruleSetName)
{
// We must encode the Entity State XML into a valid XML string so that
// it can be wrapped with the outer XML request document.
string entityStateXMLEncoded = SecurityElement.Escape(entityStateXMLInput);
string rootNodeName;
if (string.IsNullOrEmpty(ruleSetName))
 { rootNodeName = "ApplyRulesRequest"; }
else
 { rootNodeName = "ExecuteRuleSetRequest"; }

// Now we generate the outer XML request document.
string requestXML = $@"<?xml version='1.0' encoding='utf-8'?>
<{rootNodeName} xmlns='http://www.inrule.com/XmlSchema/Schema'>
  <RuleApp>
      <RepositoryRuleAppRevisionSpec>
          <RuleApplicationName>{ruleApp}</RuleApplicationName>
      </RepositoryRuleAppRevisionSpec>
  </RuleApp>
  <EntityState>{entityStateXMLEncoded}</EntityState>
  <EntityName>{entityName}</EntityName>
  <RuleSetName>{ruleSetName}</RuleSetName>
</{rootNodeName}>";
//If no ruleSetName exists, execute Auto rules, otherwise execute explicit rule.
string postUri;
if (string.IsNullOrEmpty(ruleSetName))
 { postUri = ruleExecutionServiceURI + "/ApplyRules"; }
else
 { postUri = ruleExecutionServiceURI + "/ExecuteRuleSet"; }

// Our EntityState is encoded as an XML document, not JSON.
string mediaType = "application/xml";

// Async call to HttpClient with an HttpContent and wait for result.
HttpContent content = new StringContent(requestXML, Encoding.UTF8, mediaType);
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(postUri, content);

// Async convert the HttpResponseMessage's content to a string.
string responseXML = await response.Content.ReadAsStringAsync();

// We're done, return result.
return responseXML;

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.