Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Current »

This node is designed as a generic data gatherer.  It's intention is to make an HTTP GET, optionally provided Basic Auth credentials.  It leverages Newtonsoft for JSON parsing, and RestSharp to brokering the HTTP access.  The Node's configuration collects the basic details required to make a connection.  For specific details on how to author your own GlobalCapture nodes, refer to the node documentation here.

GlobalCapture's flexible node design allows for interpolation of static variable names into their data equivalent at runtime.  In the URL specified above, the query string will include data available form the workflow.

Node Code
public class GetData : CaptureNode  //Processing Nodes in GlobalCapture workflows inherit from CaptureNode
{
        public override void Run()
        {
            Uri uri = null;
            String root="";
            try
            {
				//Initialize the properties specified at design time.  GlobalCapture will already have interpolated any variables.
                uri = new Uri(Settings.GetStringSetting("URL"));
                root = Settings.GetStringSetting("StartElement");
            }
            catch
            {
				//Log any errors to the Process history.
                LogHistory("Unable to parse provided URL: " + Settings.GetStringSetting("URL"));
                SetNextNodeByLinkName("Failure");
                return;
            }

            IRestResponse response = null;
            try
            {
				//Make the web request.  
                var client = new RestClient(uri.Scheme + "://" + uri.Authority);
                
                var user = Settings.GetStringSetting("User");
                var password = Settings.GetStringSetting("Password");
                if (!String.IsNullOrEmpty(user))
                {
                    client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator(user, password);
                }
                
                var request = new RestRequest(uri.PathAndQuery, DataFormat.Json);
                response = client.Get(request);
                if(response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception("Response received: " + response.StatusCode + ' ' + response.Content);
                }
            }
            catch(Exception ex)
            {
                LogHistory("Unable to communicate with data source. " + ex.Message);
                SetNextNodeByLinkName("Failure");
                return;
            }

            try
            {
				//Work with the response
                dynamic json = JsonConvert.DeserializeObject(response.Content);

                var rootObject = json;
                if(!string.IsNullOrEmpty(root))
                    rootObject = json[root];

                if (rootObject is JArray)
                    rootObject = rootObject[0];


                if (rootObject == null)
                {
                    LogHistory("No data returned, or no data for key element " + root);
                    SetNextNodeByLinkName("Failure");
                    return;
                }

				//Map elements in the response to matching (by name) Process Fields.
                bool propertySet = false;
                var propNames = Process.Properties.GetPropertyNames();
                foreach (String prop in propNames)
                {
                    String objVal = rootObject[prop];
                    if (objVal != null)
                    {
                        Process.Properties.SetSingleProperty(prop, objVal);
                        LogHistory(prop + " updating from external data source.");
                        propertySet = true;
                    }
                }

                if(!propertySet)
                    LogHistory("No data matched cooresponding property.");

                SetNextNodeByLinkName("Success");
            }
            catch(Exception ex)
            {
                LogHistory("Error parsing response object. " + ex.Message);
                SetNextNodeByLinkName("Failure");
                return;
            }
        }
}
  • No labels