Implement integrations
REST services
REST services are used to integrate an external API with the current project.
Note: The external API could be integrated by adding the specification as an API dependency or not.
Make external requests against an API with provided API specification
In case that there is the API specification of the external service provided, utility methods are auto generated that help to easier consume the API.
Below is an example of the implementation of integration against an email
API, which provides a POST method postEmail
for sending mails:
// 1. Accessing types from API dependency where
// - integrationSchema is a grouping for all api dependencies schemas
// - emailSchema is a grouping for schemas in integration namespace with acronym "email"
// - EmailMessage schema we want to get its type
import { services, integrationSchema } from 'solution-framework';
export default class extends services.email_SendMail {
public async execute(): Promise<void> {
const requestBody: integrationSchema.emailSchema.EmailMessage= {
to: [
{
adress: 'some address'
}
],
subject: `Order Confirmation of Order #12345`,
sender: 'some@sender.com',
message: 'Some message',
messageFormat: 'HTML'
};
// 1. Calling api dependency operation and getting response body
const responseBody = (await this.apis.email.postEmail(requestBody)).data;
// 2. Also we have full access to response returned as a result of an underlying axios call
const axiosResponse = await this.apis.email.postEmail(requestBody);
// Getting response body
const responseBody = axiosResponse.data;
// Getting response headers
const headers = axiosResponse.headers;
// Getting response status
const statusCode = axiosResponse.status;
//3. Another variation
const {headers, data, status} = await this.apis.email.postEmail(requestBody);
// further logic on handling response
// ...
};
}
Make external requests using request utility
When creating external calls to APIs you can retrieve the API binding option and if it contains a ca_cert property, you can use it to construct an SSLConfig object that can be used when making external API calls.
public async execute(): Promise<void> {
// get binding of petstore API
const apiBinding = await this.apiBindings.getPetstore();
// make the request using the custom ca_cert defined in the api binding
await this.util.request.get('www.something.com', {param1: 'val'}, {header1: 'val'}, { ca_cert: bind.ca_cert});
}
Attention: Whenever you are querying external services, you should make sure that all communication to
such services are encrypted and all TLS-encrypted protocols including HTTPS use version 1.2+. The connection to the
target service should be authenticated (certificate validation should be enabled)!
Make API facade operations calls
If your API dependency's API binding already contains ca_cert
value, then this ca_cert
value will be implicitly
used to make external API facade operation calls.