Shared Flows

·

4 min read

Shared Flows

In shared flow, we can create a sequence of steps that we can reuse at multiple places.
Any changes to the shared flow will reflect for all the Flows which are referring to that.
The shared flow is called from the FlowCallout policy.
example: Some common functions are security, spike arrest, and message transformation, this kind of commonly occurring events can be kept in shared flows.

Let’s create shared flows in Apigee.

Develop tad-shared flows

Click +create

Click on create.

GO to develop tab to modify the shared flow. in flow, we will add policies in step.

JSON to XML policy code looks like below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONToXML continueOnError="false" enabled="true" name="JSON-to-XML-1">
    <DisplayName>JSON to XML-1</DisplayName>
    <Properties/>
    <Options>
        <NullValue>NULL</NullValue>
        <NamespaceBlockName>#namespaces</NamespaceBlockName>
        <DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName>
        <NamespaceSeparator>:</NamespaceSeparator>
        <TextNodeName>#text</TextNodeName>
        <AttributeBlockName>#attrs</AttributeBlockName>
        <AttributePrefix>@</AttributePrefix>
        <InvalidCharsReplacement>_</InvalidCharsReplacement>
        <ObjectRootElementName>Root</ObjectRootElementName>
        <ArrayRootElementName>Array</ArrayRootElementName>
        <ArrayItemElementName>Item</ArrayItemElementName>
    </Options>
<!-- here output from policy goes to response and source we are getting json
 message is also from response --> 
    <OutputVariable>response</OutputVariable>
    <Source>response</Source>
</JSONToXML>

save and deploy the shared flow. Once it is deployed it is available for proxies.
Any proxies that want to convert JSON to XML formate can use this.

From the API proxy, we can’t directly call/hit the shared flow. So we need to create one more policy called Flow callout.

Flow callout

This is used to call shared flows from the API proxy.
We can also call other shared flows from shared flow by using the Flow callout policy.
Shared flow is used to define common functionalities like security, and message validation.
Using shared flows we can avoid redundancy in code, by utilizing a flow callout policy we can call the shared flow wherever required.
Consider a scenario where shared flow is not deployed before the API proxy which is referring to that. Then while deploying the API proxy, API proxy throws a validation error saying, Shared flow does not exist.

Scenario 1:
We will get a reject request when we pass the
get -> APIproxy (call backend to get the response) --> JSON response
(When we are sending back the response to the client we will call the flow callout to convert JSON to XML format.
Flow callout -> shared flow(convert JSON to XML)

create a reverse proxy
with target endpoint: https://virtserver.swaggerhub.com/jihobe4823/Indian-Air-Flight-API/v1/flights

next - passthrough- eval- create and deploy - edit proxy - develop.
ADD get condition as well
proxyendpoint - GET condition

Condition flow got added

before passing to backend we need to remove path suffix. "/getflights"
target endpoint -> preflow ->request-> Assign message policy.

Assign message policy code should look like below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>

    <AssignVariable>
        <Name>target.copy.pathsuffix</Name>
        <Value>false</Value>
        <Ref/>
    </AssignVariable>

    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Save deploy and see the output before adding flow callout policy to convert to XML format.
we are getting JSON responses.

Convert this to XML.
In response, we need to modify
proxy endpoint -> get condition -> response - flow callout policy.

Select the shared flow from the list of shared flows.

add

Save and deploy to see the output.
see we are getting the XML response


scenario 2:
we will create one loop back service
that receives JSON message --> in response back it will send XML message(flow callout).

create a shared flow to convert JSON to XML. where is source is from the request.

In flow add JSON to XML policy.

Code JSON to XML policy should look like below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONToXML continueOnError="false" enabled="true" name="JSON-to-XML-1">
    <DisplayName>JSON to XML-1</DisplayName>
    <Properties/>
    <Options>
        <NullValue>NULL</NullValue>
        <NamespaceBlockName>#namespaces</NamespaceBlockName>
        <DefaultNamespaceNodeName>$default</DefaultNamespaceNodeName>
        <NamespaceSeparator>:</NamespaceSeparator>
        <TextNodeName>#text</TextNodeName>
        <AttributeBlockName>#attrs</AttributeBlockName>
        <AttributePrefix>@</AttributePrefix>
        <InvalidCharsReplacement>_</InvalidCharsReplacement>
        <ObjectRootElementName>Root</ObjectRootElementName>
        <ArrayRootElementName>Array</ArrayRootElementName>
        <ArrayItemElementName>Item</ArrayItemElementName>
    </Options>
    <!--reads the request and converts into xml then its will store back to request again-->
    <OutputVariable>request</OutputVariable>
    <!-- as per scenario we are gettign the json message from request -->
    <Source>request</Source>
</JSONToXML>

Save and deploy the shared flow

Create no target proxy (loop back proxy)

next - passthrough - eval - create and deploy - edit proxy - develop.
add POST conditional flow

when the request comes we need to modify the message JSON to XML.
proxy endpoint -> post conditional flow -> request -> flow callout policy

Select the shared flow from the list. Add

save and deploy
send a post request with body - raw - JSON