Conditional statement can be configured to enable dynamic API proxy behavior at runtime. Conditional statement can be applied to RouteRules, conditional Flows, and processing steps.
A conditional statement consists of a conditional operator and a variable. For example, the following conditional statement dynamically enforces a policy called "Timer" based on the the value of an HTTP header in the request message. If an HTTP header named "responsetime" has a value of "true", then the Timer policy executes.
<Step> <Condition>request.header.responsetime="true"</Condition> <Name>Timer</Name> </Step>
(Note that all HTTP headers present in a request message are stored for use as variables. Each is stored in a variable with the prefix request.header, making it easy to use HTTP headers as a source of values for conditional statements.)
You can set up conditions to limit policy execution to a certain time of day, a locale, or for a specific type of request, such as XML, or when a particular value is present in an HTTP header. This is useful, for example, if you want to execute a transformation based on the type of device that is calling (that is, based on the value of the user-agent HTTP header). For an example, refer to the Developer Guide topic, Customize responses for mobile devices .
Conditions consist of an operator and a variable. The operator lets you construct the argument for the condition, such as equal to, greater than, is not, and so on. See Conditions Reference for complete list of operators. The variable stores data that can be used to evaluate the condition. For example, you can insert the system.timestamp variable to create a condition that evaluates based on the system time. Variables reference lists all available variables.
When Apigee encounters a condition attached to a policy it evaluates the variable in the condition. After it evaluates the argument and executes the result, the processing step executes. Steps are executed in the order in which they are present in the Flow configuration, from top to bottom. One conditional Flow executes for each ProxyEndpoint and TargetEndpoint request or response flow.
Here are some ways you can use conditions:
- Routing: You can create a conditional routing so that you can forward requests to a particular target endpoint. When more than one target endpoint is available, the route rule is evaluated for its condition. If true, the request is forwarded to its target endpoint. This is often used when you want to send a specific type of request to different endpoints.
- Conditional policy execution: Sometimes policies need to be enforced conditionally. For example, "if the request is of the type SOAP, then use XPolicy."
- Conditional flow: An entire policy flow can be designed for a particular set of request patterns and applied by specifying the patterns expressed as the conditions to the flow.
Condition attached to RouteRule
<RouteRule name="default"> <!==this routing executes if the header indicates that this is an XML call. If true, the call is routed to the endpoint XMLTargetEndpoint==> <Condition>request.header.content-type = "text/xml"</Condition> <TargetEndpoint>XmlTargetEndpoint</TargetEndpoint> </RouteRule>
Condition attached to a policy
<Step> <!==the policy MaintenancePolicy only executes if the response status code is exactly 503==> <Condition>response.status.code is 503</Condition> <Name>MaintenancePolicy</Name> </Step>
<!== this entire flow is executed only if the response verb is a GET==> <Flow name="GetRequests"> <Condition>response.verb="GET"</Condition> <Request> <Step> <!== this policy only executes if request path includes a term like statues==> <Condition>request.path ~ "/statuses/**"</Condition> <Name>StatusesRequestPolicy</Name> </Step> </Request> <Response> <Step> <!== this condition has multiple expressions. The policy executes if the response code status is exactly 503 or 400==> <Condition>(response.status.code = 503) or (response.status.code = 400)</Condition> <Name>MaintenancePolicy</Name> </Step> </Response> </Flow>
Operators let you create expressions that control what happens when a variable is evaluated. Simple operators like equals or is not let you create true/false expressions. For example, "if a variable is X then PolicyX executes, if the variable is Y then the next policy in the flow is executed." Using And (&&) and Or (||) you can create an expression that evaluates more than one variable. For example, "if a header contains variables X and Y, then the request is sent to TargetEndpointX." By stringing together multiple operators, you can create very complex conditions to precisely control how your policy flow is executed.
Here are some examples of operators used to create conditions:
- request.header.content-type = "text/xml"
- request.header.content-length < 4096 && request.verb = "PUT"
- response.status.code = 404 || response.status.code = 500
- request.uri MatchesPath "/*/statuses/**"
- request.queryparam.q0 NotEquals 10