A configurable API Callout Framework

Callout Config (Beta)

The Callout Config is a framework concept to optimize all apex callouts within an organization. Its goals are to minimize the overall code needed for callouts, eliminate code redundancy, and enhance both maintainability and scalability.

It's intended to complement Named Credentials and doesn't propose any alternatives to the features provided by Named Credentials. Named credentials can further be configured in to this framework to make things more configurable and secure. This allows to securely configure parameters to Endopint, header and body beyond what is configured in Named Credentails.

Documentation

CalloutService - this class is where we request is created, callout is made, resopse is captured. This is where all the parsing methods will be added which can be used by all callouts. This is not dependent on the other class and can be used directly without using the CalloutConfigService class

CalloutConfigService - this class creats CalloutService from the configurations created. this can handle Named Credentails cnfigured in to the configuration objects

OBJECTS: Note: Custom Metadata Types can also be used instead of Custom Objects, but using Custom Objects allows to configure secure data as well (As of today encripted fields are not supported in Custom Metadata Types)

1. Api__c - Coming soon...

2. Parameter__c - Coming soon...

Usage/Examples With Named Credentials

Bitly token External Credentail

image

Bitly token Named Credentail

image

Bitly token configured with the Named Credentail

image

Bitly token body configired as a parameter with Named Credentail merge fields

image

Bitly url shorten endpoint configured directly in to API object and its two header parameters configured as related Parameters

image

Sample Code

String long_url = 'https://www.sfdcbox.com/2022/11/looping-of-asynchronousapex-calls-from.html';
// To make call for getting access token
String token = CalloutConfigService.newInstance('Bitly Token').makeCall().getResponse().getBody();
// To make call for getting shortened url for a long url
ICalloutService service = CalloutConfigService.newInstance('Bitly Shorten')
                                                .setAuthorization('Bearer '+token)
                                                .setJsonBodyParameter('long_url', long_url)
                                                .makeCall();
if(service.getResponse().getStatusCode() == 200){
    System.debug(service.getJSONRespStringByKey('link'));
}

..........
OUTPUT
18:07:19.99 (966585749)|USER_DEBUG|[10]|DEBUG|https://bit.ly/4c3jL1X

Usage/Examples Without Named Credentials

image image

To directly fetch a value from response

String type = CalloutConfigService.newInstance()
                        .setApiName('Geocoding API')
                        .setEndpointParameter('q', 'illinois')
                        .getCalloutService()
                        .doPost()
                        .getStringFromFirstOfJsonListBody('type');

To validate response and read response values

ICalloutService service = CalloutConfigService.newInstance()
                        .setApiName('Geocoding API')
                        .setEndpointParameter('q', 'illinois')
                        .getCalloutService()
                        .doPost();
if(service.getResponse().getStatusCode() == 200){
    System.debug(service.getJSONRespFirstStringByKey('lat'));
    System.debug(service.getJSONRespFirstStringByKey('lon'));
}else{
    //Handle failure
}

Comments