Integrating an API using Connect Service
Connect Service : Working as a middleman, Connect service can be used to interact with both internal as well as external api’s.
Providers - It includes base path (base url) and other secret information (eg: headers and params) in the db.
Model:
{
"code" : String,
"name" : String,
"config" : {
"url" : String,,
"headers" : Array,
}
}
Integrations - It includes path url, mapping key and a mapper in the db.
Model:
{
"req" : {
"url" : String,
"params" :Array,
"headers" : Array,
"httpAction" : String,
"response" : {
"keys" : Object,
"map" : {
"body" : Object
}
}
},
"res" : {
"headers" : Array,
"mapper" : Array
},
"code" : String
}
Data service - This service provides majorly three features: get, post and update data to an internal or external API using single provider and single integration or single provider and multiple integrations.
GET : '/:providerCode/:integrationCode/:id'
POST : '/:providerCode/:integrationCode'
UPDATE : '/upload/:providerCode/:integrationCode.csv'
Steps to Integrate an API:
Collect API related information such as URL, headers, role keys and other query params etc
Split the URL into two parts: Base path and path url.
Keep the base path and the secure information like headers, token or other keys in the providers following a specific model.
Keep the path url mapping key and request mapper in the integrations following a specific model.
Also specify the operation which needs to be performed in the integration itself.
Now select the path of data service with which you want to proceed.
Use the response as you wish to.
Example:
Let’s integrate this api which provides university data:
http://universities.hipolabs.com/search?country=United+States
Connect url: https://dev-api.yd-ff.com/connect/api/data/hipolabs/uni-details
Where hipolabs is provider code and uni-details is integration code
Provider:
{
"_id" : ObjectId("62532f0c148763b5359052a0"),
"code" : "hipolabs",
"name" : "Hipolabs-Official-Data",
"config" : {
"url" : "http://universities.hipolabs.com",
"headers" : [],
"params" : [
{
"key" : "country",
"value" : "United+States"
}
]
},
"createdAt" : ISODate("2022-04-10T13:49:43.241Z"),
"timeStamp" : ISODate("2022-04-10T13:49:43.242Z"),
"__v" : 0
}
Integration:
{
"_id" : ObjectId("62539e78148763b53598fae3"),
"req" : {
"url" : "/search",
"params" : [],
"headers" : [],
"httpAction" : "GET",
"response" : {
"keys" : {
"data" : "res",
"resExtractKey" : ""
},
"map" : {
"body" : {
"Country" : "{{${keys.data}.[country]}}",
"Stats" : {
"University Name" : "{{${keys.data}.[name]}}",
"Website" : "{{${keys.data}.[domains].[0] }}"
}
}
}
}
},
"res" : {
"headers" : [],
"mapper" : []
},
"code" : "uni-details",
"name" : "University Details",
"provider" : ObjectId("62532f0c148763b5359052a0"),
"createdAt" : ISODate("2021-12-20T11:28:50.977Z"),
"timeStamp" : ISODate("2021-12-20T12:40:03.615Z"),
"__v" : 0
}
Response:
Uploaders - One of the best functionality provided by connect service is uploaders, using providers and integration concept with update method of data service we can simply upload .csv and .xls/.xlsx files to any endpoint.
Multiple Integrations - This is another good feature in connect, mannier times we need to post data depending upon the result of previous request or we also need to use some keys from the result of previous requests. It is possible using multiple integrations concept. Array of different integrations is present in single integration which is called as integration of integrations and leave the pending work for connect.
"multipleIntegrations" : true
This flag needs to be placed in providers to config to ensure multiple integration.
Lookups API - Simply making api’s with excel. Just like building something in runtime. Upload .csv or .xls and pass the lookupkey in integration’s req.response and it will save row wise data in lookups table with a search text which can be later used to search data in specific region/type/name etc.
"lookupKey" : "code"