8. Bonus - Using Dependencies
In the previous step, we created a function to get the city and state for a given zip code.
Let's say we now also want to reverse engineer the USPS service to get the zip code given a address, city and state. Using the same approach as before, we can create a new function to do this:
However we now run into a problem, the new function is also dependent on the session cookies that we got the first time we visited the USPS website in the USPS Search Zip
collection. What we would like to do is to reuse the request that got the cookies in the first function in the second function. To do this we can use the dependencies feature in API Parrot.
Adding Dependencies
To start with, we will need to break out the exchange that gets the cookies into a separate function. We can do this by creating a new function in the USPS Search Zip
collection. We will name this function Get USPS Cookies
. In this function we will only include the exchange that gets the cookies and define the cookies we are interested in as outputs.
Now that we have that function, we can add it as a dependency to the Get City And State From Zip
function. We do this by going to the Get City And State From Zip
tab then press the + Add Dependency
button in the bottom panel of the function tab and select the Get USPS Cookies
function from the USPS Search Zip
collection and then press Confirm
. Alternatively we can drag and drop the dependency function from the left sidebar to the current function tab.
And woila! We now have the cookies from the Get USPS Cookies
function as a dependency in the Get City And State From Zip
function, which basically means that any outputs from the Get USPS Cookies
function will be available in the Get City And State From Zip
function as inputs.
Disabling Overlapping Exchanges
Dependencies may be used to break up a collection into smaller parts, for this purpose it is useful to use the Disable Overlapping Requests
action which disables all the requests / exchanges in the current function that are also executed in the dependency function. To use this action, go to the dependency function properties pane and press the Disable Overlapping Requests
button in the bottom right corner.
Dependencies in the Exported Code
When exporting the code of a function that has a dependency, that dependency will be expected as an input argument to the function in the form of a object with the keys being the outputs of the dependency function. So far, the user is responsible for providing the dependency object when running the function, but in the future we plan to add a feature to automatically run the dependency function before running the dependent function.
For the Get City And State From Zip
function, the expected dependency object would have the following structure:
{
tltsidCookieOutput : string,
nscUppmtUsvfOfxCookieOutput : string
}