Connector Builder is a tool to allow you to create Service Connectors. You provide the Swagger definition for the API and it will create a service connector that can then be used in a flow to interact with the backend service.
Like the Axway Flow SDK this is a tool that you can install globally:
npm install -g connector-builder
If it's not publicly available yet then it can be installed from the internal registry:
npm --registry http://registry.ecd.axway.int:8081/artifactory/api/npm/registry-npm install -g connector-builder
Once installed you can run the builder using connector-builder
.
Let's say we're big fans of Weatherbit.io and want to use it in our flow. There's no node available but that's not a problem. We can create one.
We need the Swagger definition for the service: https://raw.githubusercontent.com/bladedancer/training/master/06_ConnectorBuilder/swagger/weatherbitio.json
** Note this was not the best choice of API as the swagger is actually not valid - it has query parameters set in the path and defined as path params. Connector Builder actually worked with it but that's unexpected and can't be relied on to remain that way so the swagger on disk has been updated (at least for the getCurrent method that this flow is using).
Run connector-builder i
to start the wizard.
w:\training\06_ConnectorBuilder>connector-builder i
? Enter the name of the connector you want to generate: weatherbitio
? Enter the URL or the path (absolute or relative) to the API definition: .\swagger\weatherbitio.json
? Select the Connector Type: Open API
? Select the Context where the Connector will be used: API Builder
? Enter the absolute path to the location where the connector will be generated (leave empty for current dir):
? Do you want to remove previous connector if it is already exist? Yes
Before building you can replace icon.svg with the logo of the service.
You also need to install the axway-requester module which may not be on the public npm registery.
npm install -S axway-requester
If its not on the public registry install it with:
npm --registry http://registry.ecd.axway.int:8081/artifactory/api/npm/registry-npm install -S axway-requester
Then build all:
npm run all
npm pack
This is the same as installing a downloaded service connector. Unzip the packed tgz to the serviceconnectors folder of your app.
Start the API Builder console and navigate to the Welcome flow: http://localhost:8080/console/project/flows/welcome-Welcome/edit
The Weatherbit.io node is now available:
As well as getting the contacts expenses we also want to get the weather in their area.
Note the flow layout could be better, there are plans to improve the layout engine.
There's a minor reorganization to move exclude before the service conenctors, just for readability. The addition here is a Compose node to format the city & state into a single string for the WeatherBit method and the addition of the WeatherBit service connector to get the weather.
Type: Compose
Method: Format string
The getCurrent API that we're going to call take the city as city,state so just format the string appropriately.
The template could be more robust but for now it's just:
{{=it.city}},{{=it.state}}
The output of the template evaluation is stored as $.citystate.
Type: Weatherbit.io - Weather API
Method: getCurrent
This is the service connector we've just created. The methods on it are the methods defined in the swagger document. For this example we're calling getCurrent with the contacts address details.
The city is $.citystate
, from the Format City State output. The country is straight from the contact address $.address.country. And you can sign up at weatherbit.io to get an api key.
The output is stored as $.weather.
Note: This is pretty brittle, if there is an error in the parameters it will trigger an exception in the response parsing and abort the flow (user gets a 500).
Type: Compose
Method: Format object
Update the Format Response node to include the weather in it's template:
{
"message": "Welcome",
"name": "{{?it.contacts[0].salutation}}{{=it.contacts[0].salutation}} {{?}}{{=it.contacts[0].firstname}} {{=it.contacts[0].lastname}}",
"address": {{=JSON.stringify(it.address)}},
"expenses": {{=it.expenseTotal}},
"weather": "{{=it.weather.data[0].weather.description}}"
}
As before, we're changing the response and so should update the endpoint definition to include this new field. Currently this has to be done on the file system.
Stop API Builder and open endpoints\welcome.json. Change the response to include weather:
"schema": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"name": {
"type": "string"
},
"address": {
"$ref": "schema:///model/ContactAddress"
},
"expenses": {
"type": "number"
},
"weather": {
"type": "string"
}
}
}
Open the Test API panel for the Welcome API and execute it with a cid of batman. The response should now include the weather description:
{
"message": "Welcome",
"name": "Mr. Bruce Wayne",
"address": {
"address1": "Wayne Manor",
"address2": "107 Mountain Drive",
"city": "New York",
"state": "NY",
"country": "US",
"postalcode": "0"
},
"expenses": 4210,
"weather": "Clear sky"
}