Prerequisites
In this tutorial, you will learn how to implement dynamically flow logic without writing any code using the API Assembly
Instructions:
- Note: You must complete If you did not complete previous tutorial, import the API definitions file from /conditional/weather_1.0.0.yaml.
We will examine the existing conditional policy in the Assembly.
- Open the
weather
API in the API designer. The default page takes you to the Design tab. - Click the Assemble tab. You will notice a single Invoke policy with a URL value of
$(target-url)
. In the previous tutorial, you set this variable to the actual endpoint. - In the API Assemble Palette (on the left-hand side), several policies can be drag and dropped into the editor. In this tutorial, you will apply conditional logic to modify the runtime endpoint.
- Drag the Switch policy into the editor before the Invoke policy and release it once you see an orange box. Note: When you drag the action into this case, an orange box will appear to indicate the action can be added. Move around the Invoke action until you get the orange box.
- Select the dropdown beside Case and select
get /current
. - Click the + Case button again and select
get /today
. Click the X button to close the action. Each operations requires a different endpoint to be invoked. You will configure an action to modify the target endpoint based on the operation. - Drag an Set Variable policy into the case for
get /current
. Click Action+ and enter the following values:
- Action: Set
- target-url
- Type: string
- Value: https://myweatherprovider.mybluemix.net/current?zipcode=$(request.parameters.zipcode)
- Drag another Set Variable policy into the case for
get /today
. Click Action+ and enter the following values:
- Action: Set
- target-url
- Type: string
- Value: https://myweatherprovider.mybluemix.net/today?zipcode=$(request.parameters.zipcode)
Note: The Invoke policy URL is similar except the base path is today
instead of current
.
- You will need to reset the
target-url
variable so that the updated values are picked up at runtime. Switch back to the Design tab and select Properties. Select thetarget-url
field and remove the URL value. - Save your changes and click the Republish button (circle with two arrows). If the button is unavailable then click the Stop button and the Play button again.
- Test the Assembly logic by calling the
current
operation (replacing the client id and secret with your own values)curl -k -X GET \
'https://localhost:9444/localtest/sandbox/weather/current?zipcode=90210'
-H 'x-ibm-client-id: 36e2da0e888ffcb6a9160200e40c5a15'
-H 'x-ibm-client-secret: 3f4b5e8e835c4d21030a8b6436923fe8'
{
"zip": "90210",
"temperature": 62,
"humidity": 90,
"city": "Beverly Hills",
"state": "California"
}
```
- Test the Assembly logic by calling the
today
operation (replacing the client id and secret with your own values)curl -k -X GET \
'https://localhost:9444/localtest/sandbox/weather/today?zipcode=90210'
-H 'x-ibm-client-id: 36e2da0e888ffcb6a9160200e40c5a15'
-H 'x-ibm-client-secret: 3f4b5e8e835c4d21030a8b6436923fe8'
{
"zip": "90210",
"hi": 72,
"lo": 56,
"nightHumidity": 91,
"city": "Beverly Hills",
"state": "California",
"dayHumidity": 67
}
```
Note: Each operation returns a different response
In this tutorial, you learned how to build a simple conditional statement based on the incoming API operation.
Next Tutorial: Write JavaScript to enrich API payloads