Apex Framework / library for consuming REST services





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







4















To consume REST services there is a lot of boilerplate code needed:




  1. Build Apex Objects to model the responses

  2. Code to deserialize REST responses to those classes

  3. Handling of HTTP requests, responses and errors

  4. ...


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










share|improve this question





























    4















    To consume REST services there is a lot of boilerplate code needed:




    1. Build Apex Objects to model the responses

    2. Code to deserialize REST responses to those classes

    3. Handling of HTTP requests, responses and errors

    4. ...


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










    share|improve this question

























      4












      4








      4


      2






      To consume REST services there is a lot of boilerplate code needed:




      1. Build Apex Objects to model the responses

      2. Code to deserialize REST responses to those classes

      3. Handling of HTTP requests, responses and errors

      4. ...


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










      share|improve this question














      To consume REST services there is a lot of boilerplate code needed:




      1. Build Apex Objects to model the responses

      2. Code to deserialize REST responses to those classes

      3. Handling of HTTP requests, responses and errors

      4. ...


      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 3 at 19:17









      Robert SösemannRobert Sösemann

      13.2k1178225




      13.2k1178225






















          1 Answer
          1






          active

          oldest

          votes


















          6














          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.



          JSONPlaceholder named credential



          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);
          }
          }
          }





          share|improve this answer


























          • 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














          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          6














          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.



          JSONPlaceholder named credential



          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);
          }
          }
          }





          share|improve this answer


























          • 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


















          6














          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.



          JSONPlaceholder named credential



          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);
          }
          }
          }





          share|improve this answer


























          • 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
















          6












          6








          6







          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.



          JSONPlaceholder named credential



          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);
          }
          }
          }





          share|improve this answer















          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.



          JSONPlaceholder named credential



          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);
          }
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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





















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          數位音樂下載

          When can things happen in Etherscan, such as the picture below?

          格利澤436b