ApiController
is a wrapper class for handling all integrations using the HttpRequest
and HttpResponse
classes in Apex. This simple guide explains how to use the class in your Apex code.
Before using the ApiController
, you need to ensure that:
- You have created a named credential to authenticate the API callout.
You can use the quick installer here to deploy directly to your org.
After install assign the permission set View and manage Gantt Chart
to relevant users.
To use the ApiController
class, follow these steps:
- Create an instance of the class:
ApiController apiController = new ApiController();
- Initialize the controller with a named credential and a path:
apiController.init('named_credential', ApiController.HttpVerb.GET, '/path/to/resource');
Note that you need to specify the HttpVerb
enum value to determine the HTTP method for the API callout.
- Set additional parameters and add headers if necessary:
apiController.setTimeout(5000);
apiController.addHeader('Content-Type', 'application/json');
- Add query parameters and a request body if necessary:
Map<String, String> queryParams = new Map<String, String>{
'param1' => 'value1',
'param2' => 'value2'
};
apiController.addQueryParameter(queryParams);
String requestBody = '{"key": "value"}';
apiController.addBody(requestBody);
- Perform the API callout:
apiController.doCallout();
- Retrieve the response from the controller:
HttpResponse response = apiController.getResponse();