https://github.com/agentgill/http-framework
https://github.com/agentgill/http-framework
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/agentgill/http-framework
- Owner: agentgill
- Created: 2021-03-18T07:34:00.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-18T07:34:20.000Z (about 4 years ago)
- Last Synced: 2025-01-18T13:38:23.980Z (4 months ago)
- Language: Apex
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# HTTPCalloutFramework
HTTPCalloutFramework is a light weight framework for apex HTTP callouts in salesforce.
## Overview
HTTPCalloutFramework can be used to perform apex callouts to external systems.
It has in-built apex classes that can be used to perform HTTPCallouts.
The required information for the callout can be stored in the custom metadata named**HTTPCalloutConfiguration**.
The framework also consists of mock classes that can be used to define mocks for single
and multiple HTTP Callouts in a single transaction. The code used in the framework is already covered
so that you don't need to worry during the deployments.### Prerequisites
There are no such pre-requisites for installing and using this framework.
If you want to download the code in your local system,
you can do it by the executing the below command or downloading the code directly as a zip file.```bash
git clone https://github.com/rahulmalhotra/HTTPCalloutFramework.git
```### Installing
HTTPCalloutFramework is very easy to use. You can install this application in your salesforce org by using the **deploy to salesforce** button
present in the [deployment](#deployment) section of this readme. Installing this will add the following to your org :-1. HTTPCalloutService - Apex Class
2. HTTPCalloutServiceMock - Apex Class
3. HTTPCalloutServiceMultiMock - Apex Class
4. HTTPCalloutFrameworkException - Apex Class
5. HTTPCalloutServiceTest - Apex Class
6. HTTPCalloutConfiguration - Custom Metadata
7. TestMetadata - HTTPCalloutConfiguration record used in test class (should not be deleted)
8. SFDCStopBlogs - HTTPCalloutConfiguration record (For demo purposes - can be deleted)
9. HTTPCalloutConfiguration Layout - Layout for HTTPCalloutConfiguration metadata
10. SFDCStopAPI - Remote Site Settings record for SFDC Stop API (For demo purposes - can be deleted)**HTTPCalloutFramework** is now ready for use.
## Deployment
You can deploy HTTPCalloutFramework directly to your org by clicking the button below
An alternative way can be to use [SFDX Deploy Tool](https://github.com/rahulmalhotra/SFDX-Deploy-Tool) for deployment.
For that you can download the zip directly and rename it to unpackaged
that can be used by placing it inside the metadata folder of the application structure## Usage
### Synchronous Callouts
Once installed you'll see the below custom metadata records created in your org:-

You'll have a SFDCStopBlogs metadata record along with a remote site setting record created for the same that you can use for testing the framework.
You can see that I have specified details of the request in the metadata itself.
You can copy and execute the below code in developer console to test the framework.
As you can see below, I have passed the custom metadata record developer name in the constructor.
Rest of the details are fetched automatically.```apex
HttpCalloutService service = new HTTPCalloutService('SFDCStopBlogs');
System.debug(service.getRequest());
System.debug(service.sendRequest().getBody());
```
You can check the logs once the code is executed and it should have the output as below

Custom metadata and remote site setting record of SFDC Stop API are for demo purposes only.
You can delete these records after installation and create your own records for HTTP callouts.
Make sure you **Do not delete the TestMetadata** record of HTTPCalloutConfiguration custom metadata as it's being used in the test class for code coverage.### Asynchronous Callouts
Now, let's jump on to the asynchronous apex callouts part. As you must be aware that the asynchronous callouts in apex are implemented using the [Continuation](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Continuation.htm) class. The continuation class has different syntax for Visualforce Pages and Lightning Components. So, we're going to see the syntax for implementation in both cases. The good thing is that we have a single [HTTPCalloutAsyncService](https://github.com/rahulmalhotra/HTTPCalloutFramework/blob/master/classes/HTTPCalloutAsyncService.cls) class that we can use for both Visualforce Pages and Lightning Components.
#### Visualforce Page
The syntax of vf page controller is given below:-
```apex
HTTPCalloutAsyncService service {get;set;} // Creating an instance of HTTPCalloutAsyncService in controller
List requestLabels; // This list will be used to store the request labels returned by the continuation process
// Define your action method (should have return type of Object)
public Object sendAsyncCalloutRequest() {
service = new HTTPCalloutAsyncService(, new List {, , });
Continuation con = service.sendRequest('getAsyncCalloutResponse'); // Pass the callback method name in the parameter
requestLabels = service.getRequestLabels(); // Storing the request labels returned by continuation
return con; // Returning the continuation
}// Define a callback method with the same name as passed in the sendRequest method of service class
public Object getAsyncCalloutResponse() {
// Getting a list of responses by passing the request labels in the parameter
List responses = service.getResponse(requestLabels);
// Process the responses (Set variables that are being used in VF Page)
// Returning null to re-render the vf page
return null;
}
```The visualforce page should have an command button calling our controller method as shown below:-
```html```
#### Lightning Component
The syntax of lightning component controller is given below:-
```apex
// This method will be called from lightning component (should have return type of Object)
@AuraEnabled(cacheable=true continuation=true)
public static Object fetchData() {
HTTPCalloutAsyncService service = new HTTPCalloutAsyncService(, new List {, , });
return service.sendRequest('sendResponse'); // Pass the response method name in the parameter
}// Define a callback method with the same name as passed in the sendRequest method of service class
@AuraEnabled(cacheable=true)
public static Object sendResponse(List labels, Object state) {
HTTPCalloutAsyncService service = new HTTPCalloutAsyncService(, new List {, , });
// Getting a list of responses by passing the request labels in the parameter
List responses = service.getResponse(labels);
// Process the responses (Create a wrapper to send the response)
// Returning the wrapper in JSON format back to lightning component
return JSON.serialize();
}
```We should have call our apex method in lightning component helper as shown below:-
```js
var fetchDataAction = component.get('c.fetchData');
fetchDataAction.setCallback(this, function(response) {
if(response.getState() === 'SUCCESS') {
var data = JSON.parse(response.getReturnValue());
}
}
$A.enqueueAction(fetchDataAction);
```I have created a working example for both VF and Lightning that you can deploy in your org by clicking the below button. **Make sure that you've installed the framework in your org first otherwise the deployment of examples will fail.**
The working example consist of a VF Page named **SFDCStopCallout** and a Lightning Application named **BlogsContinuationApp** that will make an asynchronous continuation call to SFDC Stop APIs and fetch the data using our framework.
*Visualforce Page Output :-*

*Lightning Component Output :-*

## Tools and Softwares used
You just need a salesforce org to run this application.
If you want to make any changes in the code, you can do that by using the developer console provided by Salesforce.
However I like to use VS Code IDE to keep a local copy of code on my system too. For regular deployments,
I use SFDX Deploy Tool. So below are the tools or softwares I use personally :-* [VS Code](https://code.visualstudio.com) - Open Source IDE for Salesforce
* [SFDX Deploy Tool](https://github.com/rahulmalhotra/SFDX-Deploy-Tool) - SFDX Deploy Tool for Windows## Todo
- [ ] Find a way to cover code adding client certificate to HTTP callout in test class.
It will increase the code coverage of HTTPCalloutService class to 100% (Current coverage:- 99%)## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on code of conduct and the process for submitting pull requests.
## Authors
* **Rahul Malhotra** - [@rahulcoder](https://twitter.com/rahulcoder)
## License
This project is licensed under the BSD 3-Clause License - see the [LICENSE.md](LICENSE.md) file for details.