Apex Framework / library for consuming REST services
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
To consume REST services there is a lot of boilerplate code needed:
- Build Apex Objects to model the responses
- Code to deserialize REST responses to those classes
- Handling of HTTP requests, responses and errors
- ...
Did anyone come up with patterns or best practices codified into an open source library or framework that one could just reuse in a similar project without reinventing the callout-wheel over an over again?
Like the zillions of trigger-frameworks that exist as repos on Github...
callout rest design-patterns libraries framework
add a comment |
To consume REST services there is a lot of boilerplate code needed:
- Build Apex Objects to model the responses
- Code to deserialize REST responses to those classes
- Handling of HTTP requests, responses and errors
- ...
Did anyone come up with patterns or best practices codified into an open source library or framework that one could just reuse in a similar project without reinventing the callout-wheel over an over again?
Like the zillions of trigger-frameworks that exist as repos on Github...
callout rest design-patterns libraries framework
add a comment |
To consume REST services there is a lot of boilerplate code needed:
- Build Apex Objects to model the responses
- Code to deserialize REST responses to those classes
- Handling of HTTP requests, responses and errors
- ...
Did anyone come up with patterns or best practices codified into an open source library or framework that one could just reuse in a similar project without reinventing the callout-wheel over an over again?
Like the zillions of trigger-frameworks that exist as repos on Github...
callout rest design-patterns libraries framework
To consume REST services there is a lot of boilerplate code needed:
- Build Apex Objects to model the responses
- Code to deserialize REST responses to those classes
- Handling of HTTP requests, responses and errors
- ...
Did anyone come up with patterns or best practices codified into an open source library or framework that one could just reuse in a similar project without reinventing the callout-wheel over an over again?
Like the zillions of trigger-frameworks that exist as repos on Github...
callout rest design-patterns libraries framework
callout rest design-patterns libraries framework
asked Apr 3 at 19:17
Robert SösemannRobert Sösemann
13.2k1178225
13.2k1178225
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The ffhttp library should do the trick: https://github.com/financialforcedev/ffhttp-core
It includes error handling, redirections, building header fields, oauth, mime attachments and more.
EDIT - Sample code
Say you want to consume the JSONPlaceholder /users rest resource.
You could create a Named Credential for the URL.
Then you can use ffhttp to make the request, handle the failure or successful response and deserialize into the intended format. FFHTTP only includes an OAuth client but it's pretty simple to create your own...
public with sharing class ffhttpSample {
/*
* DTO for remote object result
*/
private class RemoteUser {
Integer id;
String name;
String username;
String email;
}
/*
* Vanilla HTTP request
*/
public static RemoteUser getUsers() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:JSONPlaceholder/users');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
return (RemoteUser)JSON.deserialize(response.getBody(), List<RemoteUser>.class);
} else {
System.debug(response.getStatus());
return new RemoteUser{};
}
}
/*
* FFHTTP request
*/
public static RemoteUser getUsersFfhttp() {
SimpleClient client = new SimpleClient();
client.setCredentials(new ffhttp_Client.NamedCredentials('JSONPlaceholder'));
SimpleClientRequest request = new SimpleClientRequest(
client,
'/users',
ffhttp_Client.REQUEST_METHOD_GET,
new ffhttp_JsonDeserializer(List<RemoteUser>.class));
// Execute and parse the response.
// Alternatively you can use executeUnparsed() to return the response body as a String
return (RemoteUser)request.execute();
}
/*
* FFHTTP client implementations
*/
private class SimpleClient extends ffhttp_Client.AbstractClient {}
private class SimpleClientRequest extends ffhttp_Client.AbstractClientRequest {
SimpleClientRequest(IAbstractClient client, String endpoint, String requestMethod, ffhttp_IDeserialize deserializer) {
super(client, endpoint, requestMethod, null, deserializer);
}
}
}
Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share.
– Robert Sösemann
Apr 3 at 20:59
would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-)
– Robert Sösemann
2 days ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f256451%2fapex-framework-library-for-consuming-rest-services%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The ffhttp library should do the trick: https://github.com/financialforcedev/ffhttp-core
It includes error handling, redirections, building header fields, oauth, mime attachments and more.
EDIT - Sample code
Say you want to consume the JSONPlaceholder /users rest resource.
You could create a Named Credential for the URL.
Then you can use ffhttp to make the request, handle the failure or successful response and deserialize into the intended format. FFHTTP only includes an OAuth client but it's pretty simple to create your own...
public with sharing class ffhttpSample {
/*
* DTO for remote object result
*/
private class RemoteUser {
Integer id;
String name;
String username;
String email;
}
/*
* Vanilla HTTP request
*/
public static RemoteUser getUsers() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:JSONPlaceholder/users');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
return (RemoteUser)JSON.deserialize(response.getBody(), List<RemoteUser>.class);
} else {
System.debug(response.getStatus());
return new RemoteUser{};
}
}
/*
* FFHTTP request
*/
public static RemoteUser getUsersFfhttp() {
SimpleClient client = new SimpleClient();
client.setCredentials(new ffhttp_Client.NamedCredentials('JSONPlaceholder'));
SimpleClientRequest request = new SimpleClientRequest(
client,
'/users',
ffhttp_Client.REQUEST_METHOD_GET,
new ffhttp_JsonDeserializer(List<RemoteUser>.class));
// Execute and parse the response.
// Alternatively you can use executeUnparsed() to return the response body as a String
return (RemoteUser)request.execute();
}
/*
* FFHTTP client implementations
*/
private class SimpleClient extends ffhttp_Client.AbstractClient {}
private class SimpleClientRequest extends ffhttp_Client.AbstractClientRequest {
SimpleClientRequest(IAbstractClient client, String endpoint, String requestMethod, ffhttp_IDeserialize deserializer) {
super(client, endpoint, requestMethod, null, deserializer);
}
}
}
Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share.
– Robert Sösemann
Apr 3 at 20:59
would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-)
– Robert Sösemann
2 days ago
add a comment |
The ffhttp library should do the trick: https://github.com/financialforcedev/ffhttp-core
It includes error handling, redirections, building header fields, oauth, mime attachments and more.
EDIT - Sample code
Say you want to consume the JSONPlaceholder /users rest resource.
You could create a Named Credential for the URL.
Then you can use ffhttp to make the request, handle the failure or successful response and deserialize into the intended format. FFHTTP only includes an OAuth client but it's pretty simple to create your own...
public with sharing class ffhttpSample {
/*
* DTO for remote object result
*/
private class RemoteUser {
Integer id;
String name;
String username;
String email;
}
/*
* Vanilla HTTP request
*/
public static RemoteUser getUsers() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:JSONPlaceholder/users');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
return (RemoteUser)JSON.deserialize(response.getBody(), List<RemoteUser>.class);
} else {
System.debug(response.getStatus());
return new RemoteUser{};
}
}
/*
* FFHTTP request
*/
public static RemoteUser getUsersFfhttp() {
SimpleClient client = new SimpleClient();
client.setCredentials(new ffhttp_Client.NamedCredentials('JSONPlaceholder'));
SimpleClientRequest request = new SimpleClientRequest(
client,
'/users',
ffhttp_Client.REQUEST_METHOD_GET,
new ffhttp_JsonDeserializer(List<RemoteUser>.class));
// Execute and parse the response.
// Alternatively you can use executeUnparsed() to return the response body as a String
return (RemoteUser)request.execute();
}
/*
* FFHTTP client implementations
*/
private class SimpleClient extends ffhttp_Client.AbstractClient {}
private class SimpleClientRequest extends ffhttp_Client.AbstractClientRequest {
SimpleClientRequest(IAbstractClient client, String endpoint, String requestMethod, ffhttp_IDeserialize deserializer) {
super(client, endpoint, requestMethod, null, deserializer);
}
}
}
Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share.
– Robert Sösemann
Apr 3 at 20:59
would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-)
– Robert Sösemann
2 days ago
add a comment |
The ffhttp library should do the trick: https://github.com/financialforcedev/ffhttp-core
It includes error handling, redirections, building header fields, oauth, mime attachments and more.
EDIT - Sample code
Say you want to consume the JSONPlaceholder /users rest resource.
You could create a Named Credential for the URL.
Then you can use ffhttp to make the request, handle the failure or successful response and deserialize into the intended format. FFHTTP only includes an OAuth client but it's pretty simple to create your own...
public with sharing class ffhttpSample {
/*
* DTO for remote object result
*/
private class RemoteUser {
Integer id;
String name;
String username;
String email;
}
/*
* Vanilla HTTP request
*/
public static RemoteUser getUsers() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:JSONPlaceholder/users');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
return (RemoteUser)JSON.deserialize(response.getBody(), List<RemoteUser>.class);
} else {
System.debug(response.getStatus());
return new RemoteUser{};
}
}
/*
* FFHTTP request
*/
public static RemoteUser getUsersFfhttp() {
SimpleClient client = new SimpleClient();
client.setCredentials(new ffhttp_Client.NamedCredentials('JSONPlaceholder'));
SimpleClientRequest request = new SimpleClientRequest(
client,
'/users',
ffhttp_Client.REQUEST_METHOD_GET,
new ffhttp_JsonDeserializer(List<RemoteUser>.class));
// Execute and parse the response.
// Alternatively you can use executeUnparsed() to return the response body as a String
return (RemoteUser)request.execute();
}
/*
* FFHTTP client implementations
*/
private class SimpleClient extends ffhttp_Client.AbstractClient {}
private class SimpleClientRequest extends ffhttp_Client.AbstractClientRequest {
SimpleClientRequest(IAbstractClient client, String endpoint, String requestMethod, ffhttp_IDeserialize deserializer) {
super(client, endpoint, requestMethod, null, deserializer);
}
}
}
The ffhttp library should do the trick: https://github.com/financialforcedev/ffhttp-core
It includes error handling, redirections, building header fields, oauth, mime attachments and more.
EDIT - Sample code
Say you want to consume the JSONPlaceholder /users rest resource.
You could create a Named Credential for the URL.
Then you can use ffhttp to make the request, handle the failure or successful response and deserialize into the intended format. FFHTTP only includes an OAuth client but it's pretty simple to create your own...
public with sharing class ffhttpSample {
/*
* DTO for remote object result
*/
private class RemoteUser {
Integer id;
String name;
String username;
String email;
}
/*
* Vanilla HTTP request
*/
public static RemoteUser getUsers() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:JSONPlaceholder/users');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
return (RemoteUser)JSON.deserialize(response.getBody(), List<RemoteUser>.class);
} else {
System.debug(response.getStatus());
return new RemoteUser{};
}
}
/*
* FFHTTP request
*/
public static RemoteUser getUsersFfhttp() {
SimpleClient client = new SimpleClient();
client.setCredentials(new ffhttp_Client.NamedCredentials('JSONPlaceholder'));
SimpleClientRequest request = new SimpleClientRequest(
client,
'/users',
ffhttp_Client.REQUEST_METHOD_GET,
new ffhttp_JsonDeserializer(List<RemoteUser>.class));
// Execute and parse the response.
// Alternatively you can use executeUnparsed() to return the response body as a String
return (RemoteUser)request.execute();
}
/*
* FFHTTP client implementations
*/
private class SimpleClient extends ffhttp_Client.AbstractClient {}
private class SimpleClientRequest extends ffhttp_Client.AbstractClientRequest {
SimpleClientRequest(IAbstractClient client, String endpoint, String requestMethod, ffhttp_IDeserialize deserializer) {
super(client, endpoint, requestMethod, null, deserializer);
}
}
}
edited yesterday
answered Apr 3 at 20:32
frup42frup42
21915
21915
Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share.
– Robert Sösemann
Apr 3 at 20:59
would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-)
– Robert Sösemann
2 days ago
add a comment |
Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share.
– Robert Sösemann
Apr 3 at 20:59
would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-)
– Robert Sösemann
2 days ago
Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share.
– Robert Sösemann
Apr 3 at 20:59
Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share.
– Robert Sösemann
Apr 3 at 20:59
would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-)
– Robert Sösemann
2 days ago
would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-)
– Robert Sösemann
2 days ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f256451%2fapex-framework-library-for-consuming-rest-services%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown