Can somebody explain what is a REST API wrapper?

  • Can somebody explain what is a REST API wrapper? How should it look like? When would we actually need it? Can we call them as DTO's? Please give some usecases, examples or links.

  • A Wrapper Class (or the Wrapper Pattern) is where you declare a Class as a container for an sObject to extend the functionality only for display or processing purposes (i.e. you don't intend for that attribute to be persisted) - the classic example is a checkbox to select records. I would say that a DTO is a slightly dumbed down version of such a wrapper (used in conventional OOP to pass structured data between the layers)

    A REST Api wrapper is something slightly different. Salesforce exposes a REST Api and if you were to invoke that say from C#, you would have to perform a set of common steps such as login, query, etc. To make this available in C# by abstracting the innards of the actual REST calls to salesforce and exposing only the developer relevant detail, you could write a rest wrapper which performs these commonly used functions - creates requests, parses responses, etc

    could you please give some code samples as you said //creates requests, parses responses, etc// so that i can get some idea

    Is there a particular language you're looking for?

    Josh, i am looking for REST wrappers code samples in APEX lang to get an idea.

  • Force.com platform provides Standard REST API. These are Out Of Box API and no apex coding is necessary. Please find the below link to explore the standard REST API

    http://www.salesforce.com/us/developer/docs/api_rest/index.htm

    You can simulate all this using workbench. Please find below the workbench link

    workbench.developerforce.com

    Sometimes the business requirements are little complex and none of the API provided by salesforce fit and hence we can always make our own API by creating apex classes .(Restful APEX wrapper )

    http://wiki.developerforce.com/page/Creating_REST_APIs_using_Apex_REST

    Remember that since this is apex all the Governor limits of apex apply here.

    A common design approach is to explore all standard API and if standard API's don't satisfy then an apex class can be built and a custom API can be formed.

    Refer to the APEX guide for more info on how to write apex class to generate a REST API

    REST can expose data both in XML as well as in JSON format.

    Apex also gives ability to form your own class and these classes can be wrapped over native objects and these can be helpful to form the necessary data format needed by external system.

    http://cloudyworlds.blogspot.in/2012/09/how-to-generate-wrapped-data-from.html

    Edit: As per request posted the code

    {
    "Accounts": [
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v25.0/sobjects/Account/001W0000006XDG9IAO"
            },
            "CreatedDate": "2012-08-09T08:29:43.000+0000",
            "LastModifiedDate": "2012-08-09T15:24:42.000+0000",
            "IsDeleted": true,
            "Id": "001W0000006XDG9IAO"
        },
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v25.0/sobjects/Account/001W0000006XGEuIAO"
            },
            "CreatedDate": "2012-08-09T15:44:29.000+0000",
            "LastModifiedDate": "2012-08-09T15:50:31.000+0000",
            "IsDeleted": false,
            "Id": "001W0000006XGEuIAO"
        },
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v25.0/sobjects/Account/001W0000006QtKyIAK"
            },
            "Phone": "12131",
            "CreatedDate": "2012-08-02T14:46:48.000+0000",
            "LastModifiedDate": "2012-08-09T15:43:20.000+0000",
            "IsDeleted": true,
            "Id": "001W0000006QtKyIAK"
        },
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v25.0/sobjects/Account/001W0000006XGI4IAO"
            },
            "CreatedDate": "2012-08-09T15:50:07.000+0000",
            "LastModifiedDate": "2012-08-14T11:33:05.000+0000",
            "FirstName": "Test",
            "IsDeleted": false,
            "Id": "001W0000006XGI4IAO",
            "LastName": "Thomas"
        },
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v25.0/sobjects/Account/001W0000006XGIDIA4"
            },
            "CreatedDate": "2012-08-09T15:44:55.000+0000",
            "LastModifiedDate": "2012-08-14T10:15:39.000+0000",
            "FirstName": "shgdshg",
            "IsDeleted": false,
            "Id": "001W0000006XGIDIA4",
            "LastName": "Hello"
        },
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v25.0/sobjects/Account/001W0000006XGhwIAG"
            },
            "CreatedDate": "2012-08-09T16:08:14.000+0000",
            "LastModifiedDate": "2012-08-09T16:08:19.000+0000",
            "FirstName": "deleted",
            "IsDeleted": true,
            "Id": "001W0000006XGhwIAG",
            "LastName": "check"
        },
    
      "Contacts": [
            {
            "attributes": {
                "type": "Contact",
                "url": "/services/data/v25.0/sobjects/Contact/003W0000007mNoqIAE"
            },
            "AccountId": "001W0000006XGI4IAO",
            "HCP__c": "001W0000006XGIDIA4",
            "FirstName": "Test",
            "IsDeleted": false,
            "Id": "003W0000007mNoqIAE",
            "LastName": "Thomas"
        },
        {
            "attributes": {
                "type": "Contact",
                "url": "/services/data/v25.0/sobjects/Contact/003W0000007mNp1IAE"
            },
            "AccountId": "001W0000006XGIDIA4",
            "HCP__c": "001W0000006XGEuIAO",
            "FirstName": "shgdshg",
            "IsDeleted": false,
            "Id": "003W0000007mNp1IAE",
            "LastName": "Hello"
        },
        {
            "attributes": {
                "type": "Contact",
                "url": "/services/data/v25.0/sobjects/Contact/003W0000007mNpGIAU"
            },
            "Phone": "3213",
            "AccountId": "001W0000006XGEuIAO",
            "HCP__c": "001W0000006XGIDIA4",
            "IsDeleted": false,
            "Id": "003W0000007mNpGIAU",
            "LastName": "ueiru"
        },
        {
            "attributes": {
                "type": "Contact",
                "url": "/services/data/v25.0/sobjects/Contact/003W0000007wAkwIAE"
            },
            "Phone": "206-999-1111",
            "AccountId": "001W0000006XJ3fIAG",
            "Email": "[email protected]",
            "HCP__c": "001W0000006XGIDIA4",
            "FirstName": "Mark",
            "IsDeleted": false,
            "Id": "003W0000007wAkwIAE",
            "LastName": "Wells"
        }
      ]}
    

    For this we will have to use customized wrapping ,

    Following is the source code that will automatically handle the generation of JSON in the expected format

      global class SFA_AccountContactListedResponse{
     //Wrapper class to warp the various  List<sObject> as Super wrapper
      global class supersobjectWrapper{
      List<Account> Accounts=new List<Account>();//A list to hold the Accounts
      List<Contacts> Contacts=new List<Contact>();//A list to hold the Contacts  
     }
     //This method will be called as the http get request
      public static supersobjectWrapper makeResponseString(){  
     List<Account>  lstacc=new List<Account>();
     List<Contact> lstcontacts=new List<Contact>();
     lstacc=[Select Id,CreatedDate,LastModifiedDate,Isdeleted from Acccount ];
     lstcontacts=[Select id,CreatedDate,LastModifiedDate,Isdeleted from Contact];
     supersobjectWrapper superWrap=new supersobjectWrapper();//Instantiating thesuperclass
     superWrap.Contacts= lstcontacts ;
      superWrap.Accounts= lstacc;
      return superWrap;
     }
    }
    

    //The above class will be called as REST API using GET HTTP call,

      @RestResource(urlMapping='/GetAcc/*')
      global with sharing class SFA_ListedJsonGenerator{ 
      @HttpGet
     global static SFA_AccountGroupSyncManagerRevised.supersobjectWrapper    returnJsonpacket(){ 
       RestRequest req = RestContext.request;
       SFA_AccountGroupSyncManagerRevised.supersobjectWrapper result;  
       result=SFA_AccountContactListedResponse.supersobjectWrapper;
        return result;
       }
     }
    

    Mohit, the link http://cloudyworlds.blogspot.in/2012/09/how-to-generate-wrapped-data-from.html is not opening for me. could you post some sample code to get an idea of REST wrapper.

    Posted the code .Apologies for blog link not working.Looks some problem .Please find the exact replica of the work i did above

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used