What type of variables are commonly used as flags to indicate whether a specific condition exists?
Show
This preview shows page 1 - 4 out of 25 pages. Checkpoint Questions4.1 What is a control structure? Get answer to your question and much more 4.2 What is a decision structure? Get answer to your question and much more 4.3 What is a single alternative decision structure? Get answer to your question and much more 4.4 What is a Boolean expression? Get answer to your question and much more 4.5 What types of relationships between values can you test with relational operators? Get answer to your question and much more 4.7 Write a pseudocodeIf-Thenstatement that assigns 0.2 tocommissionifsalesis greater thanor equal to 10,000. Get answer to your question and much more Display “Commission has been applied” Get answer to your question and much more 4.8 How does a dual alternative decision structure work? Get answer to your question and much more 4.9 What statement do you use in pseudocode to write a dual alternative decision structure? Get answer to your question and much more 4.10 When you write anIf-Then-Elsestatement, under what circumstances do the statements thatappear betweenElseandEnd Ifexecute? Get answer to your question and much more 4.11 If the following pseudocode were an actual program, what would it display?Program:If “z” < “a” ThenDisplay “z is less than a.”ElseDisplay “z is not less than a.”End IfProgram Output:z is not less than a.4.12 If the following pseudocode were an actual program, what would it display?Program:Declare String s1 = “New York”Declare String s2 = “Boston”If s1 > s2 ThenDisplay s2Display s1ElseDisplay s1 Display s2End IfProgram Output:s1s24.13 How does a dual alternative decision structure work? Get answer to your question and much more 4.14 What statement do you use in pseudocode to write a dual alternative decision structure? Get answer to your question and much more 4.15 When you write anIf-Then-Elsestatement, under what circumstances do the statements thatappear between theElse Clauseand theEnd Ifclause execute? Upload your study docs or become a Course Hero member to access this document Upload your study docs or become a Course Hero member to access this document End of preview. Want to read all 25 pages? Upload your study docs or become a Course Hero member to access this document Tags Expression, relational operator, Switch statement You're viewing Apigee Edge documentation. Conditional statements are a common control structure in all programming languages. Like a programming language, API proxy configuration supports conditional statements for Flows, Policies, Steps, and RouteRules. By defining conditional statements, you define dynamic behavior for your API. This dynamic behavior lets you do things like converting XML into JSON only for mobile devices, or routing to a backend URL based on the content type or HTTP verb of the request message. This topic shows you how to use conditions to dynamically apply API management features at runtime, without writing any code. VideoTo learn more about flow variables and conditions, watch the following video:
Configure conditional statementsConditional behavior is implemented in API proxies by using a combination of conditions and variables. A conditional statement is created using a Condition element. The following is an empty condition: To create a conditional statement, you add a conditional operator and a variable to create the following structure: Supported conditional operators include = (equals), != (not equal), and > (greater than). For readability, you can also write the conditionals as text: equals, notequals, greaterthan. When working with URI paths you can use ~/ or MatchesPath. You can also match JavaRegex regular expressions with the ~~ operator. Conditions are used to define API proxy conditional flows to backend API resources, described in Create conditional flows to backend API resources. For a complete list of conditionals, see Conditions reference. VariablesConditions do their work by evaluating the values of variables. A variable is a property of an HTTP transaction executed by an API proxy, or a property of an API proxy configuration itself. Whenever an API proxy gets a request from an app, Apigee Edge populates a long list of variables that are associated with things like system time, the app's network information, HTTP headers on messages, the API proxy configuration, policy executions and so on. This creates a rich context that you can use to set up conditional statements. Variables always use a dotted notation. For example, HTTP headers on the request message are available as variables called request.header.{header_name}. So to evaluate the Content-type header, you could use the variable request.header.Content-type. For example request.header.Content-type = "application/json" indicates that the content type of the request should be JSON. Imagine that you need to create a conditional statement that will cause a policy to be enforced only when a request message is a GET. To create a condition that evaluates the HTTP verb of a request, you create the conditional statement below. The variable in this condition is request.verb. The value of the variable is GET. The operator is =. You could also use: Edge uses such a statement to evaluate conditions. The example above evaluates to true if the HTTP verb associated with the request is GET. If the HTTP verb associated with the request is POST, then the statement evaluates to false. To enable dynamic behavior, you can attach Conditions to Flows, Steps, and RouteRules. When you attach a condition to a Flow, you create a 'conditional Flow'. Conditional Flows execute only when the condition evaluates to true. You can attach as many Policies as you want to a conditional Flow. A conditional Flow enables you to craft highly specialized processing rules for request or response messages that meet certain criteria. For example, to create a Flow that executes only when the request verb is a GET: To create one Flow for GETs and another for POSTs: As shown in the example below, you can apply the condition to the Policy Step itself. The following Condition causes the VerifyApiKey Policy to be enforced only if a request message is a POST. Once you have defined such conditional Flows, you can attach Policies to them, enabling an API proxy to enforce one set of policies for GET requests, and another set of policies for POST requests. For comprehensive reference information, see the following resources:
Example 1The following example shows a single conditional flow named Convert-for-devices, configured in the ProxyEndpoint response flow. Add the Condition as an element to the entity to which the condition applies. In this example, the condition is a component of the flow. Therefore, the flow will execute whenever the statement evaluates to true. For each request received from an app, Edge stores the values of all HTTP headers present as variables. If the request contains an HTTP header called User-Agent, that header and its value are stored as a variable called request.header.User-Agent. Given the ProxyEndpoint configuration above, Edge checks the value of the request.header.User-Agent variable to see whether the condition evaluates to true. If the condition does evaluate to true, that is, the value of the variable request.header.User-Agent equals Mozilla, then the conditional Flow executes and the XMLtoJSON policy called ConvertToJSON is enforced. If not, the Flow is not executed, and the XML response is returned unmodified (in XML format) to the requesting app. Example 2Let's use a specific example in which you need to transform response message from XML to JSON—but only for mobile devices. First, create the policy that will convert the XML-formatted response from the Weather API into JSON: The policy configuration above tells the API proxy to take the response message, perform a conversion from XML to JSON with default settings, and then write the result to the new response message. (If you are converting a request message from XML to JSON, you simply set both of these values to request.) Since you want to convert responses from XML to JSON, you need to configure a conditional response Flow to perform the conversion. For example, to convert all responses from XML to JSON before they are returned to the client app, configure the following ProxyEndpoint response Flow. When you invoke the API using the standard request, the response is formatted in JSON. However, your goal is to only convert Weather reports into JSON when the requesting client is a mobile device. To enable such dynamic behavior, you must add a conditional statement to the Flow. Test the conditional flowIn this sample request, the HTTP User-Agent header is set to Mozilla, causing the conditional statement to evaluate to true and the conditional flow Convert-for-devices to execute. $ curl -H "User-Agent:Mozilla" http://{org_name}-test.apigee.net/weather/forecastrss?w=12797282or, to pretty print where Python is available: $ curl -H "User-Agent:Mozilla" http://{org_name}-test.apigee.net/weather/forecastrss?w=12797282 | python -mjson.toolSample Response: . . . "yweather_forecast": [ { "code": "11", "date": "12 Dec 2012", "day": "Wed", "high": "55", "low": "36", "text": "Showers" }, { "code": "32", "date": "13 Dec 2012", "day": "Thu", "high": "56", "low": "38", "text": "Sunny" } ] } . . .A request submitted without the User-Agent header, or with a different value than Mozilla, will result in an XML-formatted response. $ curl http://{org_name}-test.apigee.net/weather/forecastrss?w=12797282The unmodified XML response is returned. Sample Response: Pattern matchingThis section describes how to use pattern matching with conditions in an Apigee flow. Further ReadingOperatorsThis section describes how to use the following pattern matching operators in conditional statements:
MatchesLet's look at the "Matches" or "~" conditional operator first. These two operators are the same -- the English version, "Matches", is considered to be a more readable option. Summary: The "Matches" operator gives you two possiblities. Either match the string literally, or do a wildcard match with "*". As you might expect, the wildcard matches zero or more characters. Let's see how this works. The following XML shows a Step condition. It executes the SomePolicy policy when the condition evaluates to true. In this example, we test the variable proxy.pathsuffix, a built-in variable in Edge that stores the path suffix of the request. Note, however, you can test the value of any flow variable that contains a string. So, in this case, if the base path of the incoming request is /animals, and the request is /animals/cat, then the path suffix is the literal string "/cat". Question: What proxy path suffix will cause SomePolicy to execute? There's only one possibility. API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? Yes, because the proxy path suffix matches "/cat" exactly. It will not execute if the suffix is /bat or /dog or "/" or anything else. Now, consider this conditional statement where we use the wildcard character "*": API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? Yes, because the wildcard matches any character, and "/cat" is a match. API Call: GET http://artomatic-test.apigee.net/matchtest/bat Does the policy execute? Yes, because the wildcard matches any character, "/bat" is a match. API Call: GET http://artomatic-test.apigee.net/matchtest/owl Does the policy execute? Certainly not -- although the wildcard matches the "o", the letters "wl" are not matched. Now, lets move the wildcard to the end of the suffix: API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? Yes, because the wildcard matches zero or more of any characters. API Call: GET http://artomatic-test.apigee.net/matchtest/bat Does the policy execute? No, "/bat" is not a match. API call: GET http://artomatic-test.apigee.net/matchtest/cat123 Does the policy execute? Yes, the wildcard matches zero or more of any characters, therefore "123" produces a match. API call: GET http://artomatic-test.apigee.net/matchtest/cat/bird/mouse Does the policy execute? Yes, because the wildcard matches zero or more of any characters, so "/bird/mouse" produces a match. Note how an expression like this can get you into trouble because it matches everything after the literal characters! Question: Is the Matches operator case sensitive? Yes. Assume you have a condition like this: API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? No, the wildcard matches any letter (regardless of case), but the lowercase "a" does not match "A". API call: GET http://artomatic-test.apigee.net/matchtest/bAt Does the policy execute? Yes, the case matches. Question: How do I escape characters with the Matches operator? Use the percent "%" character to escape reserved characters. For example: API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? No, the Matches operator is looking for the literal string "c*at". API call: GET http://artomatic-test.apigee.net/matchtest/c*at Question:Does the policy execute? Yes, this path, although a bit unusual, matches. JavaRegexAs you can see, the "Matches" operator is great for simple situations. But you can use another operator, the "JavaRegex" or "~~" operator. These two are the same operator, except JavaRegex is considered to be more readable. It's called JavaRegex because it allows regular expression pattern matching, and Edge follows the same rules as the classes in the java.util.regex package in the Java language. The way the JavaRegex operator works is very different from the Matches operator, so it's important not to confuse the two! Summary: The "JavaRegex" operator lets you use regular expression syntax in conditional statements. The following code shows a Step condition. It executes the SomePolicy policy if the condition evalutes to true. In this example, we test the variable proxy.pathsuffix, a built-in variable in Edge that stores the path suffix of the request. If the base path of the incoming request is /animals, and the request is /animals/cat, then the path suffix is the literal string "/cat". Question: What proxy path suffix will cause SomePolicy to execute? Just like with the Matches operator, there's only one possibility in this case. API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? Yes, because the proxy path suffix matches "/cat" exactly. It will not execute if the suffix is /bat or /dog or anything else. Now, let's create a regular expression using "*" quantifier. This quantifier matches zero or more of the preceding character. API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? No! The "*" quantifier matches zero or more of the preceding character, which is a "c". API Call: GET http://artomatic-test.apigee.net/matchtest/ccccct Does the policy execute? Yes, because the wildcard matches zero or more of the preceding character. Next, we use the "?" quantifier, which matches the preceding character once, or not at all. API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? Yes. The "?" quantifier matches zero or one occurrence of the preceding character, which is an "a". API Call: GET http://artomatic-test.apigee.net/matchtest/ct Does the policy execute? Yes. The "?" quantifier matches one or none of the preceding character. In this case, there is no "a" character, so the condition evaluates to true. API Call: GET http://artomatic-test.apigee.net/matchtest/caat Does the policy execute? No. The "?" quantifier matches one of the preceding character, which is an "a". Next, we use the "[abc]" or "grouping" style of regex expression. It matches the characters "a" or "b" or "c". API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? Yes. We're using regular expressions here, and the "[cbr]" expression matches a "c", "b", OR "r". These calls are also matches: GET http://artomatic-test.apigee.net/matchtest/bat GET http://artomatic-test.apigee.net/matchtest/rat But this is not a match: GET http://artomatic-test.apigee.net/matchtest/mat Question: Is the JavaRegex operator case sensitive? Yes. Assume you have a condition like this: API call: GET http://artomatic-test.apigee.net/matchtest/cat Does the policy execute? Yes, the regex matches zero or one of the preceding character, which is "a". API call: GET http://artomatic-test.apigee.net/matchtest/cAt Question: Does the policy execute? No, because the capital "A" does not match lowercase "a". MatchesPathThe MatchesPath operator can also be specified like this "~/". It looks a little bit like the Matches (~) and the JavaRegex (~~) operators. But MatchesPath is entirely different. Just remember that this operator looks at a path as a series of parts. Therefore, if the path is: /animals/cats/wild, you can think of the path as consisting of the parts "/animals", "/cats", and "/wild". The MatchesPath operator lets you use two wildcard notations: a single asterisk (*) and a double asterisk (**). The single asterisk matches one path element. The double asterisk matches one or many path elements. Let's look at an example. In this example, we test the variable proxy.pathsuffix, a built-in variable in Edge that stores the path suffix of the request. Note, however, you can test the value of any flow variable that contains a string. Question: What proxy path suffix will cause SomePolicy to execute? API call: GET http://artomatic-test.apigee.net/matchtest/animals Question: Does the policy execute? No, because condition requires another path element after "/animals", as specified by "/*". API call: GET http://artomatic-test.apigee.net/matchtest/animals/ Does the policy execute? Yes, the path does have another path element (the part after "/animals/"), but it's just empty. API Call: GET http://artomatic-test.apigee.net/matchtest/animals/cats Does the policy execute? Yes, because the path clearly has an element ("/cats") that comes after "/animals" API Call: GET http://artomatic-test.apigee.net/matchtest/animals/cats/wild Question: Does the policy execute? No, because the single asterisk only matches one path element, and this API has more than one element after "/animals". Now let's use the double asterisk: Question: What proxy path suffix will cause SomePolicy to execute? API call: GET http://artomatic-test.apigee.net/matchtest/animals Does the policy execute? No, because condition requires at least one following path element specified by "/**". API call: GET http://artomatic-test.apigee.net/matchtest/animals/ Does the policy execute? Yes, the path does have another path element (the part after "/animals/"), but it's just empty. API Call: GET http://artomatic-test.apigee.net/matchtest/animals/cats Does the policy execute? Yes, because the path has at least one element that comes after "/animals" API Call: GET http://artomatic-test.apigee.net/matchtest/animals/cats/wild Does the policy execute? Yes, because the path has more than one element that comes after "/animals" Mixing asterisksYou can use combinations of the single (*) and double (**) asterisk to further refine your path matching. API call: All of these API calls will produce a match: GET http://artomatic-test.apigee.net/matchtest/animals/cats/wild/ and GET http://artomatic-test.apigee.net/matchtest/animals/dogs/wild/austrailian and GET http://artomatic-test.apigee.net/matchtest/animals/birds/wild/american/finches API resourcesRESTful services are collections of API resources. An API resource is a URI path fragment that identifies some entity that developers can access by calling your API. For example, if your service provides weather reports and weather forecasts, your backend service might define two API resources:
Further ReadingWhen you create an API proxy (as shown in the Build your first API proxy), at a minimum you're creating an alias base URL that maps to your backend service. For example:
At this point you can make API calls to your backend using either base URL. But when you use the API proxy URL, things start to get interesting. In addition to API analytics that Edge starts to collect when you use the API proxy, proxies also let you define conditional flows that map to the resources on your backend. In essence, "If a GET call comes in to the /reports resource, Edge should do something." The following image shows the behavior difference between two URLs that ultimately access the same backend. One is the un-proxied resource URL, the other is an Edge API proxy with a conditional flow to the same backend resource. We'll describe conditional flows in more detail below. How API proxies map to specific backend resourcesWith an API proxy URL mapped to the base URL of the backend service (when you create the proxy), you can add conditional flows to specific resources, such as the /reports and /forecasts resources mentioned earlier. Let's say you wanted to have Edge "do something" when calls come in to the /reports or /forecasts resources. At this point you're not telling Edge what to do, just that it should be listening for calls to those resources. You do this with conditions. In your Edge API proxy, you can create conditional flows for /reports and /forecasts. For conceptual purposes, the following API proxy XML shows what those conditions might look like. Those conditions say, "When a GET request comes in with /reports and /forecasts in the URL, Edge will do whatever you (the API developer) tell it to, through the policies you attach to those flows. Now here's an example of telling Edge what to do when a condition is met. In the following API proxy XML, when a GET request is sent to https://yourorg-test.apigee.net/mygreatweatherforecast/reports, Edge executes the "XML-to-JSON-1" policy in the response. In addition to those optional conditional flows, each API proxy also comes with two default flows: a Create conditional flows to backend resourcesDefining conditional flows to backend resources in an API proxy is completely optional. However, those conditional flows give you the ability to apply fine-grained management and monitoring. You will be able to:
For example, imagine that you need to apply different types of logic to your backend /developers to /apps resources. To do so, you add two conditional flows in your API proxy: /developers and /apps. In the Develop view of the API proxy editor Navigator pane, click the + icon next to default in the Proxy Endpoints. In the "New Conditional Flow" window, you'd enter the following key configurations:
The condition will be triggered (and policies will be executed) if a call is sent to the proxy with /developers at the end of the URI. Now add a conditional flow for /apps, and assume you want the condition to be triggered on both the URI and the POST verb in a request. The configuration involves setting the following:
The condition will be triggered (and policies will be executed) if a call is sent to the proxy with /apps at the end of the URI and a POST verb. In the Navigator pane, you'll see new flows for Apps and Developers. Select one of the flows to view the conditional flow configuration in the API proxy editor code view: As you can see, API resources are simply conditional Flows that evaluate the URI path of the inbound request. (The proxy.pathsuffix variable identifies the URI of the request that follows the BasePath configured in the ProxyEndpoint configuration.) Each API resource that you define is implemented by a conditional Flow in the API proxy. (See Configuring flows.) Once you deploy the API proxy to the test environment, the following request: http://{org_name}-test.apigee.net/{proxy_path}/appswill cause the condition to evaluate to true, and this flow, along with any associated policies, will execute. The following example condition uses a Java regular expression to recognize calls made to the /apps resource with or without a trailing forward slash (/apps or /apps/**): For more about this type of condition, see How to match regardless ... in the Apigee Community. Modeling hierarchical URIsIn some cases, you will have hierarchical API resources. For example, the Developer Services API provides a method for listing all apps that belong to a developer. The URI path is: /developers/{developer_email}/appsYou may have resources where a unique ID is generated for each entity in a collection, which is sometimes annotated as follows: /genus/:id/speciesThis path applies equally to the following two URIs: /genus/18904/species /genus/17908/speciesTo represent this structure in an API resource, you can use wildcards. For example: /developers/*/apps /developers/*example.com/apps /genus/*/specieswill resolve these hierarchical URIs as API resources appropriately. In some cases, especially for deeply hierarchical APIs, you may simply want to resolve everything below a certain URI fragment. To do so, use a double asterisk wildcard in your resource defintiion. For example, if you define the following API resource: /developers/**That API resource will resolve the following URI paths: /developers/{developer_email}/apps /developers/{developer_email}/keys /developers/{developer_email}/apps/{app_id}/keysHere's what the conditional flow condition would look like in the API proxy definition: More examplesCondition attached to RouteRuleCondition attached to a policyConditional FlowBài Viết Liên QuanQuảng CáoCó thể bạn quan tâmQuảng cáoXem NhiềuQuảng cáoChúng tôiTrợ giúpBản quyền © 2021 haylam.top Inc.
|