The next topic we'll cover in this video is chaining requests together,
how we make parent-child requests using the batch controller where a child sub request can access
the information from a parent sub response. In order to utilize this we're going to demonstrate
how to identify which sub requests are parents. These parents sub requests are executed first
so that the subsequent child request can use the content of the parent sub response. Additionally
we'll demonstrate how to retrieve information from parent sub responses. The child sub requests are
going to use JSON path to query the parent sub responses for the information from them.
One of the most common use cases for parent-child sub requests is using web IDs from one sub request
to build out the resource for another sub request. As a note JSON path was developed
independent of OSIsoft and is utilized in a number of applications, though we do support
most if not all JSON path syntax. In this example we're going to create an element then query for
the newly created element from the parent sub request. I'll remove the get home page request
and we'll add a new sub request for query element. In this case we want our method to be get. Next in
order to specify that this request is a child to another we'll add the parent IDs parameter
to our sub request, the parent ID's parameter takes an array of all sub requests that need
to be completed before this sub request can be executed. In our case we only have one
other sub request which is the create elements up request. This is also the sub request that needs
to be completed first so we'll enter this sub request ID into the parent array ID. This is one
of those times that it's useful to have helpful ID names for the sub request rather than 1 2 3.
Next we need to identify what information we'd like to retrieve from the parent sub request. We
use the parameters field for the sub request to do this. The parameters field also takes an array of
values; in our case we need to only retrieve one piece of information. This is where our JSON path
syntax comes in. We're going to start this off with a dollar sign which represents the top-level
JSON object. In our case the top-level JSON object is going to be the entire response, then we use
dots to access a particular property of the object beneath this top-level object. So if we want to
access a particular sub request we'll need to use the syntax dollar sign dot sub request ID.
We can use additional dots to reference properties of the sub request and this is the method we're
going to use for retrieving the URL out of a parent sub request. As a note each JSON path
must select exactly one token or the value of that property. In this case we can use a
previous response to take a look at the content of the response so we know what path we'll need.
From our last video if you recall we created a new element NuBlue. Taking a look at the
content of the response we see the header with the URL for the newly created element.
This is exactly what we're going to need for querying that element.
Lastly we need to build out the URL that we'll need for
the child request in the resource parameter of the child request.
We'll use substitution with syntax similar to C# string building to substitute in the value of the
property selected in the parameters of the sub request. So we use zero enclosed by curly braces
which is going to be substituted with whatever value is returned by the JSON path specified in
the first index of the array of the parameters property. We can have additional substitutions
in the resource from the parameters array if we need but we're going to focus on one for now. So,
to recap in our case we plan on retrieving the URL from the create element call,
then we're going to reference it using the JSON path syntax and the parameters
and use that as our URL for the resource. Let's update our name of the element that
we're creating to NuYellow so that we're not trying to create an element
that already exists. I'll also need to add the elements endpoint to this URL.
Let's execute the request.
We can see in the sub request that our get element response is 200.
In the content portion of this response we can see all of the information about this
newly created element including the path. So now we've confirmed that we're able to
use the parents sub request and return to us the information from that parameters array.