Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jongpie/apexcallouts

A lightweight Apex library for making HTTP callouts. Works with remote site settings and named credentials.
https://github.com/jongpie/apexcallouts

apex rest-api salesforce

Last synced: 19 days ago
JSON representation

A lightweight Apex library for making HTTP callouts. Works with remote site settings and named credentials.

Awesome Lists containing this project

README

        

# Apex Callouts
A lightweight Apex library for making HTTP callouts


Deploy to Salesforce

## Using Callout Constructors
- **Callout(String endpoint)** - used when you have a full URL for the endpoint and you are using [remote site settings](https://help.salesforce.com/articleView?id=configuring_remoteproxy.htm&type=5)
- **Callout(String namedCredential, String endpointPath)** - used with [named credentials](https://help.salesforce.com/articleView?id=named_credentials_about.htm&type=5). To use this, the named credential's endpoint should be just the base URL of the API. The specific endpoint resource to call is then provided via the endpointPath parameter.

## Building Your Callout Request
Once you have instantiated an instance of Callout, you can setup additional options for the callout, like adding headers & parameters. Each builder method returns the current instance of Callout, allowing you to chain the builder methods.
* Callout setClientCertificateName(String clientCertificateName)
* Callout setCompressed()
* Callout setCompressed(Boolean compress)
* Callout setHeader(String key, String value)
* Callout setHeaders(Map headers)
* Callout setParameter(String key, String value)
* Callout setParameters(Map parameters)
* Callout setTimeout(Integer timeoutMs)

## Making Your Callout Request
Once you have instantiated an instance of Callout and setup the headers & parameters (if needed), you can call any of the HTTP verb methods - each method returns an instance of HttpResponse.
* HttpResponse del() - 'delete' is a reserved word in Apex, so the method name has been abbreviated
* HttpResponse get()
* HttpResponse head()
* HttpResponse patch() or patch(Object requestBody)
* HttpResponse post() or post(Object requestBody)
* HttpResponse put() or put(Object requestBody)
* HttpResponse trace()

## PATCH, POST & PUT methods
Patch, post & put methods accept an Object as a parameter, with 3 main types supported
* **Blob**: Callout automatically uses setBodyAsBlob(yourBlob)
* **Dom.Document**: Callout automatically uses setBodyDocument(yourDocument)
* **Serializable Object**: Any other object types will be serialized and sent as JSON, using setBody(JSON.serialize(yourObject))

For all 3 scenarios, the header 'Content-Type' is automatically set based on the request body if the header has not already been set

## Error Handling
When the callout is made, any status code >= 400 automatically throws an instance of Callout.HttpResponseException exception

## Example Usage
GET request with headers & parameters, using chained method calls & named credentials
```
HttpResponse myCalloutResponse = new Callout('myExampleNamedCredential', '/fakeResource')
.addHeader('myHeader', 'myHeaderValue')
.addParameter('myFirstParameter', 'someValue')
.addParameter('mySecondParameter', 'anotherValue')
.get();
```

GET request with headers & parameters, using chained method calls & a full URL (remote site settings)
```
HttpResponse myCalloutResponse = new Callout('https://api.example.com/fakeResource')
.addHeader('myHeader', 'myHeaderValue')
.addParameter('myFirstParameter', 'someValue')
.addParameter('mySecondParameter', 'anotherValue')
.get();
```