Developing APIs on API Connect - 104 - Adding Definitions
Part of Four of the API Development Series. In this part we will show how to create simple definitions in API Connect and use the Mapping Policy to map between them.
Introduction
Start by creating a new API liked we did in part 1 only with this endpoint. http://apic-test-app.eu-gb.mybluemix.net/api/payment
The sample below can be used,
swagger: '2.0'
info:
version: 1.0.0
title: paymoney
x-ibm-name: paymoney
basePath: /paymoney
x-ibm-configuration:
properties:
target-url:
value: 'http://apic-test-app.eu-gb.mybluemix.net/api/payment'
description: URL of the proxy policy
encoded: false
cors:
enabled: true
gateway: datapower-gateway
type: rest
phase: realized
enforced: true
testable: true
assembly:
execute:
- proxy:
title: proxy
version: 1.0.0
verb: keep
target-url: $(target-url)
application-authentication:
certificate: false
paths:
/:
post:
responses:
'200':
description: success
schema:
type: string
consumes: []
produces: []
securityDefinitions:
clientID:
type: apiKey
in: header
name: X-IBM-Client-Id
security:
- clientID: []
schemes:
- https
This Service takes in a payload in the format.
{
"toId":"1001",
"fromId":"1002",
"amount":100
}
We will modify the API to accept the following payload
{
"FromAccountNo":"1001",
"TargetAccountNo":"1002",
"Amount":100
}
Steps
-
Go to the Design View
-
Go to Definitions
-
Click on Add
-
Enter the following parameters
Name : IntialRequest
Description : This is the payload schema for the data passed into the API - Press Add
-
Enter the following details
Property Name : FromAccountNo
Property Type : String
Property Example : 1001
Property Description : The account number to take the money from. -
Press Add
-
Enter the following details
Property Name : ToAccountNo
Property Type : String
Property Example : 1002
Property Description : The account number to put the money from. -
Press Add
-
Enter the following details
Property Name : Amount
Property Type : float
Property Example : 100
Property Description : The amount of money to move -
Press Save
-
Now we will create the definition for the Service Request.
Click on Add -
Enter the following parameters
Name : ServiceRequest
Description : This is the payload schema for the data passed into the Downstream Service -
Press Add
-
Enter the following details
Property Name : fromId
Property Type : String
Property Example : 1001
Property Description : The account number to take the money from. -
Press Add
-
Enter the following details
Property Name : toId
Property Type : String
Property Example : 1002
Property Description : The account number to put the money. -
Press Add
-
Enter the following details
Property Name : Amount
Property Type : Number
Property Example : 100
Property Description : The amount of money to move -
Press Save
-
Go to Path
-
Click on
/
-
Click on
Post
-
Click on Add in the Parameters Section
-
Enter the following details
Required : True
Name : Request
Located in ; body
Type : IntialRequest
Descrioption : JSON Payload container details on the acounts to move money from and two, and the quantity. -
Press Save
-
Goto Assembly
-
Drag a Parse Policy onto the start of the flow. The parse policy is required to turn the input message from a buffer into an Object.
-
Drag a Validate policy and place it after the Parse policy. We want to validate the payload prior to invoking the DownSteam service.
-
Click on the Validate policy and select the following
-
Drag a Map Policy after the validate and before the Invoke and configure it like below.
-
Set the Input to the following
-
Set the Output to the following
-
Link up the parameters, dragging the left circle to the corresponding right circle for each parameter.
-
Save and Publish the API,
- Invoke it as described in part 1