![DataPower API Gateway
only](../buildfiles/icon_apigtw_only.jpg)
Using the switch policy condition editor
The switch policy condition editor provides a user interface to help you build the
conditions for the case
clauses in a switch policy.
You build a condition script by using the Add Group and Add Condition options, together with operator selections, and pre-supplied function selections. The Output field shows the resultant condition script, and updates dynamically as you build your condition.
Adding conditions
When you open the policy condition editor, an initial condition is pre-supplied ready for you to configure.
Use the drop-down list to select the function that you want to begin your condition.
JSONata support in API Connect
API Connect supports v2.0 of the JSONata specification, with the following limitations:
- If you use a JSONata expression to extract or redact fields, soft linked copies of the field are also affected by the Extract or Redaction policy, even if the fields exist in different locations.
- API Connect does not support higher order JSONata functions (functions that process other functions).
- If a JSONata function supported by API Connect calls a higher order function as an argument, that argument is not supported (due to the lack of support for higher order functions).
- The following JSONata v2.0 functions are not supported:
$eval()
$formatNumber()
$formatInteger()
$parseInteger()
$sift()
$each()
$error()
$assert()
$fromMillis()
$now()
is supported with the following restrictions:- The following component specifiers are not supported:
W
= Week in yearw
= Week in monthX
= ISO week-numbering yearx
= ISO week-numbering month
- Representing numbers as words are not supported, so the following presentation modifiers are not supported:
W
= Uppercase word (for example: [YW] => TWO THOUSAND AND TWENTY-FOUR)w
= Lowercase word (for example: [Yw] => two thousand and twenty-four)Ww
= Title case word (for example: [YWw] => Two Thousand and Twenty-Four)
- Error messages are generic.
- The following component specifiers are not supported:
Table 1 lists the functional extensions that you can use with standard JSONata notation. Each extension corresponds to a part of the API context.
Extension | Variable | Description |
---|---|---|
$apiCtx() |
Generic access to an API context | The $apiCtx() extension gives generic access to an API context.
|
$header(name) |
message.headers.name |
Message header |
$httpVerb() |
request.verb |
HTTP method of the request |
$operationID() |
api.operation.id |
ID of the operation |
$operationPath() |
api.operation.path |
Path of the operation |
$queryParameter('name') |
|
Searches for the index of query in
request.parameters.name.locations and returns
request.parameters.name.values[index] , where
[index] is the value for query in locations. Parameter values are
not URL decoded. |
$statusCode() |
message.status.code |
Status code |
$storageType([arg]) |
variable.body
You can specify any variable in the API context. When no variable is specified, the default
variable |
Storage type of the message. The supported values are binary ,
empty , graphql , json , stream , or
xml . |
$urlParameter('name') |
|
Searches for the index of path and query in
request.parameters.name.locations and returns a single array
that contains both path and query values from
request.parameters.name.values . When the URL contains both path
and query parameter values, the array includes the path values first followed by the query values.
The values of each parameter type are added in the order that they are received. Parameter values
are URL decoded.For example, the following URL contains both path and query parameter
values.
The
$urlParameter('breed') URL returns the following array of
values.
In this example, the URL includes
an API path that is configured as |
$xpath(path, xpathExpression) |
You can specify any writable variable in the API context. The
xpathExpression must be a literal string. |
Allows use of XPath expressions. The following example specifies all price
elements in the source.
|
Table 2 lists the functional extensions that you can use with GraphQL APIs.
Extension | Variable | Description |
---|---|---|
$gqlActiveOperation([graphql_message]) |
message.body |
Gets the active operation found in the specified GraphQL message. The
operationName must be the same as the name of the active operation. |
$gqlAlias(graphql_field_node) |
message.body |
Gets the alias of a GraphQL field node. |
$gqlFragments([graphql_message]) |
message.body |
Gets the fragments found in the specified GraphQL message. |
$gqlName([graphql_node]) |
message.body |
Gets the node name. For operations, the node name is the
operationName . For fields, fragment definitions, arguments, and other elements, the
node name is the name of the element. By default, the operationName of
message.body is retrieved. |
$gqlOperations([graphql_message]) |
message.body |
Gets the operations found in the specified GraphQL message. |
$gqlType([graphql_node]) |
message.body |
The operation type of the active operation is retrieved for Query, Mutation, and Subscription query types. |
- You can use the
&
(concatenation) navigation operator.
You can use the following JSONata numeric operators:
+
(addition)-
(subtraction)-
*
(multiplication) - / (division)
- % (modulo)
You can use the following JSONata comparison operators for number values or strings:
=
!=
<
>
<=
>=
- Parentheses to convert a sequence into an array, specify operator precedence, or compute complex expressions on a context value.
- Array ranges and predicate expressions.
- Single asterisk (
*
) and double asterisk (**
) wildcard characters.
query
The entire GraphQL query including operations and fragments.
operationName
For an anonymous operation,
operationName
can be empty.~fragmentSpreadName
on~typeCondition
- ~~
fragmentDefinitionName
After you select the function, additional fields are displayed, depending on the selected
function, so that you can complete the condition. For example, if you select the function
$httpVerb()
, a comparison operator selection list is displayed (=
or !=
), together with a list of HTTP verbs to select on the right hand side of the
expression (GET
, PUT
, POST
,
DELETE
, HEAD
, OPTIONS
, or
PATCH
).
If you select NOT, your condition is negated with a $not
function.
To add further conditions, click Add Condition; you then select whether to insert an and
or an or
operator between this condition and the preceding one.
To group two or more conditions inside parentheses, click Add Group, then add your conditions to the group; you then select whether to insert an and
or an or
operator between this group and the preceding condition or group.
To write your own condition from scratch, select Custom, then enter your script in the field provided.
Example
($statusCode() != 200 and ($httpVerb() = 'GET' or $httpVerb() = 'PUT'))
complete these steps in the condition editor:Simple condition statements
The following examples show conditions that use a single function.
$httpVerb()
extension to specify the HTTP method of the
request.$httpVerb()="GET"
$operationPath()
extension to specify the path of the
operation.$operationPath()="/base/path-2"
$operationID()
extension to specify the operation
ID.$operationID()="test-gatewayscript-GET"
$statusCode()
extension to specify the message status
code.$statusCode()=200
$header(name)
extension to specify the content type of the
message header.$header("Content-Type")="application/json"
Combining conditions with logical operators
You can use and
and or
operators to combine multiple functions
in a single condition.
test-gatewayscript-GET
.$httpVerb()="GET" and $operationPath()="test-gatewayscript-GET"
POST
or PUT
request.$httpVerb()="POST" or $httpVerb()="PUT"
test-gatewayscript_POST
and
a message status code equal to 200
, or an API operation ID equal to
test-gatewayscript-GET
and a message status code equal to
500
.($operationID()="test-gatewayscript-POST" and $statusCode()=200) or ($operationID()="test-gatewayscript-GET" and $statusCode()=500)
test-gatewayscript-POST
and
a message status code equal to 200
with an API operation path equal to
/base/path-2
.($operationID()="test-gatewayscript-POST" and $statusCode()=200) and $operationPath()="/base/path-2"
text/plain
and a
message header length equal to 300
, or a message status code equal to
200
.($header("Content-Type")="text/plain" and $header("Content-Length")=300) or $statusCode()= 200