Skip to content

MJ12358/apex-http-utils

Repository files navigation

apex-http-utils

Utilities to make Http requests easier in Apex.

Deploy to Salesforce

Highlights

  • HttpBuilder

    Wraps System.Http, System.HttpRequest and System.HttpResponse.

    Use it to easily send http requests via a fluent api.

  • HttpException

    Used to consistently format http exceptions.

  • HttpFormBuilder

    Inspired by this post

    Used to build multipart/form-data requests easily.

  • HttpMethod

    An emum containing http methods.

  • HttpMock

    Implements System.HttpCalloutMock

    Allows for chaining requests during testing.

  • HttpStatus

    Used to get http status names/messages by status code or vice versa.

  • JwtBuilder

    Allows easy creation of JSON web tokens via a fluent api.

  • UriBuilder

    Used to build uri's easily.

  • ServerAction

    An Aura component to make server-side calls to @AuraEnabled actions.

Usage

HttpBuilder

HttpBuilder builder = new HttpBuilder();

builder
 .endpoint('https://example.com')
 .method(HttpMethod.POST)
 .body(new Map<String, Object>{'key' => 'value'})
 .compress()
 .header('Content-Type', 'application/json')
 .send()

System.HttpResponse response = builder.getResponse();

Or with a convience http method.

HttpBuilder builder = new HttpBuilder();

builder
 .endpoint('https://example.com')
 .post(new Map<String, Object>{'key' => 'value'})
 .compress()
 .header('Content-Type', 'application/json')
 .send()

String response = builder.getBody();

HttpException

throw new HttpException(request, response);

HttpFormBuilder

HttpFormBuilder builder = new HttpFormBuilder();

builder
 .put('key1', 'value')
 .put('key2', 101)
 .put('myfile', 'ABCDE=', 'myfile.txt')
 .build();

Blob result = builder.build();

JwtBuilder

JwtBuilder builder = new JwtBuilder(JwtBuilder.Algorighm.RS256);

builder
 .audience('host@domain.com')
 .expires(3600)
 .issuer('your@company.com')
 .key('A1B2C3')

String result = builder.build();

UriBuilder

The order of the paths matter.

UriBuilder builder = new UriBuilder('https://example.com');

builder
 .path('users')
 .path('MJ12358')
 .query('$select', 'displayName')
 .query('$expand', 'manager');

String result = builder.build();

System.assertEquals('https://example.com/users/MJ12358?$select=displayName&$expand=manager', result);

ServerAction

// YourComponent.cmp

<c:ServerAction aura:id="serverAction" isLoading="{!v.isLoading}" />

// YourComponentController.js

component.get('serverAction').callServer(
  component.get('c.myServerAction'),
  {
    param1: component.get('v.param1'),
    param2: component.get('v.param2'),
  },
  $A.getCallback(response => {
    // do something on success
  }),
  $A.getCallback(response => {
    // optionally do something on error
    // by default a toast is shown
  });
)

Tests

Current test results:

Class Percent Lines
BaseRequest 80% 48/60
HttpBuilder 80% 303/377
HttpException 100% 34/34
HttpFormBuilder 85% 83/97
HttpStatus 100% 184/184
JwtBuilder 81% 124/152
UriBuilder 81% 75/92

About

Utilities to make Http requests easier in Apex.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages