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.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
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
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
You can simulate all this using workbench.Please find below the workbench link
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 )
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 dont satisfy then an apex class can be built and a custom API can be formed .
Refer 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.
Edit :As per request posted the code
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
//The above class will be called as REST API using GET HTTP call,
I am really confused that DTO's are wrapper classes kind of thing. I dont know how to combine the data from two objects (Account and Contacts) using DTO. What is DTO and how to start writing DTO's and how it can be used? Can anyone explain me in detail or provide me some examples would be very much helpful. please give me some ideas