This document discusses RESTful principles and how the Salesforce REST API implements and extends those principles. It begins by outlining Roy Fielding's definition of REST, including client-server architecture, statelessness, caching, uniform interface, layered system, and hypermedia as the engine of application state. It then demonstrates how the Salesforce REST API fulfills most of these principles through examples of retrieving and updating objects. However, it also presents two extensions - object trees and batch requests - that allow creating and manipulating multiple objects in one request, pushing the boundaries of the uniform interface principle. The document concludes by encouraging readers to carefully consider REST principles and only diverge from them when it meaningfully improves the experience.
All Aboard the Boxcar! Going Beyond the Basics of REST
1. All Aboard the Boxcar!
Going Beyond the Basics of REST
Pat Patterson
Developer Evangelist Architect
ppatterson@salesforce.com
@metadaddy
2. Safe harbor statement under the Private Securities Litigation Reform Act of 1995:
This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize
or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the
forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any
projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding
strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or
technology developments and customer contracts or use of our services.
The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for
our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate
of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with
completed and any possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability
to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our
limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential
factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal year
and in our quarterly report on Form 10-Q for the most recent fiscal quarter. These documents and others containing important disclosures are
available on the SEC Filings section of the Investor Information section of our Web site.
Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and
may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are
currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
Safe Harbor
5. Client-Server
• Separate UI from Data Storage
• Independent evolution of components
Stateless
• Each request is self-contained
• Visibility, reliability, scalability
Cache
• Responses labeled as cacheable, or not
• Efficiency, scalability, performance
From http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
Fielding’s Six Constraints
6. Uniform Interface
• Decouple implementations from services
• Trade-off: degraded efficiency
Layered System
• Each component cannot see beyond
the next layer
Code-On-Demand (optional)
• Extend client functionality via applets (!)
or scripts
Fielding’s Six Constraints
7. Identification of resources
• Resource: “any information that can be named”
Manipulation of resources through representations
• Current or intended state of resource
Self-descriptive messages
• Metadata: e.g. media type, last modified time
Hypermedia as the engine of application state
• Resource navigation via links contained in the representation
From http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
Uniform Interface
8. Resources identified by URIs
• http://api.example.com/widgets/w000123
Representation by Internet media types
• JSON
HTTP with standard methods
• GET, PUT, POST, DELETE, PATCH
Hypertext links between resources
• No ‘magic knowledge’ driving interaction
REST in Practice - 2015
10. Transactional semantics when creating hierarchies of objects?
• ‘Stateless’ REST constraint means that transaction context is tricky
Performance with high latency networks (mobile clients)
• Client framework has a message queue, wants to submit multiple
operations in a single network request
But… All Was Not Perfect in the REST API!
12. POST to /vXX.X/composite/tree/ObjectName
{
"records" :[{
"attributes" : {"type" : "Account", "referenceId" : "ref1"},
"name" : "SampleAccount",
"Contacts" : {
"records" : [{
"attributes" : {"type" : "Contact", "referenceId" : "ref2"},
"lastname" : "Smith",
"email" : smith@example.com
}, {
"attributes" : {"type" : "Contact", "referenceId" : "ref3"},
"lastname" : "Jones",
"email" : jones@example.com
}]
}
}]
}
Create whole hierarchies of related objects in a single request
Solution 1: Object Trees
16. ✔ Identification of resources
✔ Manipulation of resources through representations
✔ Self-descriptive messages
✔ Hypermedia as the engine of application state
What About Uniform Interface?
21. ✔ Identification of resources
✔ Manipulation of resources through representations
✔ Self-descriptive messages
✔ Hypermedia as the engine of application state
Straight JSON via HTTP is just one pattern, not the law!
What About Uniform Interface?
22. Go read (or reread!) Fielding’s dissertation…
at the very least, read chapter 5!
Follow the REST orthodoxy…
until it makes more sense to blaze a new trail…
and, even then, check that you’re not driving over a cliff!
Conclusion
Applets were big in 2000, Ajax wasn’t described until 2005
Resources can be collections of other resources
Fielding talks about HTML, JPEG – JSON not until 2001
Use workbench to show REST API – root, sobjects, query, get account, create account, create contact, delete contact
Show how validation fail on a contact leaves us with an account we don’t want
Create account, contacts in one hit. Show how validation fail on a contact will back out account creation.
Remember - resources can be collections of other resources
Feels a bit SOAPy, but it’s effectively an extension of HTTP – once client understands batch semantics, no special magic knowledge required.