Get JSON Node

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 Parameters
  • URL: This is the web address to call.  In the settings example above, the request is being sent to Google's Places API.  The URL can embed parameters from the workflow using Square 9 Notation as necessary.
  • Starting Element: The node makes a request to the URL provided, and expects a JSON response to be returned.  Providing a Starting Element what part of the response to work with for data that should be pulled into the workflow from the external call.
  • User Name / Password: Credentials used if Basic Authentication is enabled on the target endpoint.

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;
            }
        }
}