All Projects → rahulmalhotra → HTTPCalloutFramework

rahulmalhotra / HTTPCalloutFramework

Licence: BSD-3-Clause license
HTTP Callout Framework - A light weight callout framework for apex HTTP callouts in Salesforce

Programming Languages

Apex
172 projects

Projects that are alternatives of or similar to HTTPCalloutFramework

ssjs-lib
An open-source library that takes the repetitive and complex tasks and simplifies them, enabling you to get the most out of Salesforce Marketing Cloud.
Stars: ✭ 28 (-34.88%)
Mutual labels:  salesforce, salesforce-developers, salesforce-api, salesforce-rest-api
Salesforce-Short-Hands
The main purpose of this repository is to put all the utilities in one place so that other developers can get help and they can also contribute to this repo.
Stars: ✭ 31 (-27.91%)
Mutual labels:  salesforce, salesforce-developers, salesforce-api
NebulaFramework
A development framework for Salesforce's Apex language & the Force.com platform
Stars: ✭ 28 (-34.88%)
Mutual labels:  salesforce, salesforce-developers, salesforce-apex
apex-mocks-stress-test
Testing out FFLib versus Crud / CrudMock
Stars: ✭ 47 (+9.3%)
Mutual labels:  salesforce, salesforce-developers, salesforce-apex
apex-dml-mocking
DML mocking, CRUD mocking, dependency injection framework for Salesforce.com (SFDC) using Apex
Stars: ✭ 38 (-11.63%)
Mutual labels:  salesforce, salesforce-developers, salesforce-apex
apex-fp
Functional programming for Salesforce Apex
Stars: ✭ 231 (+437.21%)
Mutual labels:  salesforce, salesforce-developers, salesforce-api
apexmock
force.com Mock data and fixtures for Apex Unit Tests
Stars: ✭ 24 (-44.19%)
Mutual labels:  salesforce, salesforce-developers, salesforce-apex
Apex Recipes
A library of concise, meaningful examples of Apex code for common use cases following best practices.
Stars: ✭ 307 (+613.95%)
Mutual labels:  integration, salesforce
Datafire
A framework for building integrations and APIs
Stars: ✭ 487 (+1032.56%)
Mutual labels:  integration, api-client
apex-rollup
Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.
Stars: ✭ 133 (+209.3%)
Mutual labels:  salesforce, salesforce-developers
R.apex
Functional utility library for Apex
Stars: ✭ 80 (+86.05%)
Mutual labels:  salesforce, salesforce-developers
texei-sfdx-plugin
Texeï's plugin for sfdx
Stars: ✭ 99 (+130.23%)
Mutual labels:  salesforce, salesforce-developers
spaghetti-cmd-loader
Salesforce Custom Metadata Type Loader, designed for Lightning Experience
Stars: ✭ 13 (-69.77%)
Mutual labels:  salesforce, salesforce-developers
Simple Salesforce
A very simple Salesforce.com REST API client for Python
Stars: ✭ 1,072 (+2393.02%)
Mutual labels:  api-client, salesforce
awesome-lwc
A list of interesting on platform Lightning Web Components resources and code examples
Stars: ✭ 124 (+188.37%)
Mutual labels:  salesforce, salesforce-developers
Script.apex
Evaluate Javascript expressions in Apex
Stars: ✭ 18 (-58.14%)
Mutual labels:  salesforce, salesforce-developers
canvas-starter-kit
A template for developing on the Salesforce Canvas platform in Javascript.
Stars: ✭ 23 (-46.51%)
Mutual labels:  salesforce, salesforce-developers
Apex-Code-Conventions
Apex conventions and best practices for Salesforce Developers
Stars: ✭ 28 (-34.88%)
Mutual labels:  salesforce, salesforce-developers
ssc-restapi-client
Communicate with Fortify Software Security Center through REST API in java, a swagger generated client
Stars: ✭ 13 (-69.77%)
Mutual labels:  integration, api-client
lwc-redux
Integrate Redux with Lightning Web Component
Stars: ✭ 35 (-18.6%)
Mutual labels:  salesforce, salesforce-developers

HTTPCalloutFramework Tweet

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.

git clone https://github.com/rahulmalhotra/HTTPCalloutFramework.git

Installing

HTTPCalloutFramework is very easy to use. You can install this application in your salesforce org as an unmanaged package or by using the deploy to salesforce button present in the 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 (Not included in unmanaged package. For demo purposes - can be deleted if installed through deploy to salesforce button)
  9. HTTPCalloutConfiguration Layout - Layout for HTTPCalloutConfiguration metadata
  10. SFDCStopAPI - Remote Site Settings record for SFDC Stop API (Not included in unmanaged package. For demo purposes - can be deleted if installed through deploy to salesforce button)

HTTPCalloutFramework is now ready for use.

Deployment

You can deploy HTTPCalloutFramework directly to your org by clicking the button below

Deploy to Salesforce

I have also created an unmanaged package so that you can install it in your org very easily if you don't wish to use the above button.

Click Here to install the package now in a production/developer environment.

or use the below URL:-

https://login.salesforce.com/packaging/installPackage.apexp?p0=04t7F0000054q4h

Click Here to install the package now in a sandbox environment.

or use the below URL:-

https://test.salesforce.com/packaging/installPackage.apexp?p0=04t7F0000054q4h

Usage

Synchronous Callouts

Once installed you'll see the below custom metadata records created in your org:-

HTTPCalloutConfigurationMetadata

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.

SFDCStopBlogsMetadataRecord SFDCStopAPIRemoteSiteSettingsRecord

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.

HttpCalloutService service = new HTTPCalloutService('SFDCStopBlogs');
System.debug(service.getRequest());
System.debug(service.sendRequest().getBody());

DeveloperConsole

You can check the logs once the code is executed and it should have the output as below

DeveloperConsoleLog

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 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 class that we can use for both Visualforce Pages and Lightning Components.

Visualforce Page

The syntax of vf page controller is given below:-

HTTPCalloutAsyncService service {get;set;} // Creating an instance of HTTPCalloutAsyncService in controller
List<String> 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(<Integer Timeout>, new List<String> {<CustomMetadata1>, <CustomMetadata2>, <CustomMetadata3>});       
    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<HTTPResponse> 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:-

<apex:commandButton action="{!sendAsyncCalloutRequest}" value="Send Request"  reRender="<id of the block to re render>"/>

Lightning Component

The syntax of lightning component controller is given below:-

// 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(<Integer Timeout>, new List<String> {<CustomMetadata1>, <CustomMetadata2>, <CustomMetadata3>});
    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<String> labels, Object state) {
    HTTPCalloutAsyncService service = new HTTPCalloutAsyncService(<Integer Timeout>, new List<String> {<CustomMetadata1>, <CustomMetadata2>, <CustomMetadata3>});
    // Getting a list of responses by passing the request labels in the parameter
    List<HTTPResponse> 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(<wrapper>);
}

We should have call our apex method in lightning component helper as shown below:-

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.

Deploy Asynchronous Examples to Salesforce

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 :-

SFDCStopCalloutVFPage

Lightning Component Output :-

BlogsContinuationApp

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 :-

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 for details on code of conduct and the process for submitting pull requests.

Authors

License

This project is licensed under the BSD 3-Clause License - see the LICENSE.md file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].