Smart IT Engineering Ltd. REST Constraints <ul>Stateless “ ... each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server.” </ul>Advantages Visibility Reliability Scalability
Smart IT Engineering Ltd. REST Constraints <ul>Cache “ ... the data within a response to a request be implicitly or explicitly labeled as cacheable or non-cacheable. If a response is cacheable, then a client cache is given the right to reuse that response data for later, equivalent requests.” </ul>Advantages Efficient Scalability Performance
Smart IT Engineering Ltd. REST Constraints <ul>Layered System The layered system style or also popularly referred to as n-layer system allows an architecture to be composed of hierarchical layers by constraining component behavior such that each component cannot "see" beyond the immediate layer with which they are interacting. </ul><ul>Code-On-Demand Allows client functionality to be extended by downloading and executing code in the form of applets or scripts . This simplifies clients by reducing the number of features required to be pre-implemented. </ul>
Smart IT Engineering Ltd. REST Constraints <ul>Uniform Interface Central distinguishing feature of REST Involves four constraints to define 'uniform interface' for REST systems. <li>Identification of resources
Manipulation of resources through representations
HATEOAS ( H ypermedia A s T he E ngine O f A pplication S tate) </li></ul>
Smart IT Engineering Ltd. REST Constraints <ul>Uniform Interface - Resource <li>Any concept that might be the target of an web-author's hypertext reference must fit within the definition of a resource.
A resource is a conceptual mapping to a set of entities , not the entity that corresponds to the mapping at any particular point in time.
If compared of Object Oriented aproach, Object if referrrable is a resource.
Examples of resources would Books, A Book, An Author, Authors, Authors of a Book, A Publisher, Categories of a Book, A Category etc. </li></ul>
Smart IT Engineering Ltd. REST Constraints <ul>Uniform Interface – Representations & Messages <li>A representation is a sequence of bytes describing a resource in a particular format.
Other commonly used but less precise names for a representation include: document, file, and HTTP message entity, instance, or variant.
Message consists of control data, metadata, messages and in some cases hyperlinks to resources.
Examples: Images (image/jpg, image/png, etc.), Markups (text/html, application/xml etc.) and more. </li></ul>
Smart IT Engineering Ltd. REST Constraints <ul>Uniform Interface – HATEOAS <li>A hypermedia in each server response will contain links that correspond to all the actions that the client can currently perform.
Therefore, dependent on the current application state, every server response describes the new actions that are available.
The server can change the range of allowable responses in a dynamic way, and a client should adapt its behavior to these changes.
A client of a RESTful application need only know a single fixed URL to access it. </li></ul>
Smart IT Engineering Ltd. REST Constraints <ul>Uniform Interface – HATEOAS <li>All future actions should be discoverable dynamically from hypermedia links included in the representations of the resources that are returned from that URL.
The link relations should be standardized , so that the client knows what selecting that state transition means.
Standardized media types are also expected to be understood by any client that might use the API.
Application state transitions are driven by a combination of the known processing rules for each media type, client selection from the server-provided choices in representations received, and the user's manipulation of those representations. Thus interactions are driven by hypermedia . </li></ul>
Smart IT Engineering Ltd. REST What is the biggest known RESTful System on planet Earth?
Smart IT Engineering Ltd. REST World Wide Web a.k.a Internet <ul><li>HTTP Web Pages </li><ul><li>HTML pages are hypermedia </li><ul><li>CSS
A Resource may have many URIs but needs to have at least one .
A Resource may have one or more representations; i.e. it may not have any representations at all. </li></ul>
Smart IT Engineering Ltd. ROA - Resource <ul><li>Having a nice URI is not mendatory as in REST clients do not form URIs rather discover them. So it is indifferent to have basis.com.bd/softexpo/2011/ instead of basis.com.bd/abc/def as long as they name and address the same resource.
If Resource has multiple variants, i.e. combination of media format (atom xml, html etc.), encoding (ASCII, UTF-8 etc.) and language (en-US, bn etc.), besides supporting content negotiation, URI for each variant is beneficial for external linking. </li></ul>
Smart IT Engineering Ltd. ROA - Resource <ul>HTTP Hints <li>Content negotiation for representation in HTTP is done through request headers – Accept, Accept-Encoding, Accept-Language
Use Vary header in response in case a URI support multiple representations
Use Location header in response to specify the exact URI to the variant in case of nice URIs. </li></ul>
Smart IT Engineering Ltd. ROA - Features <ul>Key Features of ROA <li>Addressability
Consider a search resource, e.g. Google Search, A paginated atom feed of all books of a bookstore etc. </li></ul>
Smart IT Engineering Ltd. ROA - Statelessnes <ul><li>Same as that of REST
Introducing Application State and Resource State.
Application State resides on client side ensuring every request can be treated individually by the server without considering the past requests from the client
Resource State is data that makes up the resource. It resides server side and in case of write-able resource can be modified through its representation </li></ul>
Smart IT Engineering Ltd. ROA – Statelessnes Representations <ul><li>Lets consider a resource we call Book.
Book has name, ISBN only. (ignoring publisher, author(s) and categories for now).
It has 2 representations HTML and WWW URL Encoded.
Client can track how it reached the book in its client application state. Note different apps may reach to the same resource in different ways. E.g., one from Google Search another from a Facebook app.
The resource state, i.e. the current name and ISBN resides on the server side and is indifferent for any client.
Clients receive the representations of the resource and provides the server with the same to edit its information. </li></ul>
Smart IT Engineering Ltd. ROA – Statelessnes Link & Connectedness <ul><li>Lets enrich the Book dataset to contain author(s), publisher and categories.
So for every book resource there would be at least 5 related, i.e. Connected/Linked resource. They are the book's authors resource , the book's categories resource , the book's publisher resource , an author of the book (from first resource), a category of the book (from the 3 rd resource). </li></ul>
Use Vary to support variants. </li></ul><li>Use Location to redirect or point to the permanent URI </li></ul>
Smart IT Engineering Ltd. ROA – The Uniform Interface <ul>Status (commonly used) <li>200 OK for returning success of request.
201 Created for returning that resource is created, in conjunction with Location header pointing to the created resource.
202 Accepted for returning that request accepted but will process at a later time without any guarantee.
204 No Content for specifying no message entity </li></ul>
Smart IT Engineering Ltd. ROA – The Uniform Interface <ul>Status (commonly used) <li>302 Found for redirecting, prefer 303 instead.
303 See Other for redirecting using the Location header pointing to the actual resource.
304 Not Modified for conditional GET when condition is unmet, i.e. client can server from client cache.
301 Move Permanently If a resource URI has been changed, e.g. the template for books changed to /r/books from /books. </li></ul>
Smart IT Engineering Ltd. ROA – The Uniform Interface <ul>Status (commonly used) <li>400 Bad Request when request information is sufficient to process the data, e.g. required field of an HTML Form is missing.
401 Unauthorized and 403 Forbidden for authentication and authorization failures respectively.
404 Not Found for not being able to resolve the URI to a resuorce.
406 Not Acceptable If none variants requested can be served by the server
412 Preconditioned Failed if the conditions in request not met. Useful lock like feature in case of updates.
415 Unsupported Media Type when request entity is not recognized by server for processing. </li></ul>
Smart IT Engineering Ltd. ROA – Safety & Idempotence <ul><li>If designed according to the spec we get safety and idempotence for free.
Safety – refers to GET and HEAD not changing any state of the resources concerned, but might have side effects, e.g. hit counters.
Idempotence – Repeating any one of PUT, POST, DELETE on a resource any number of will yield the same result. </li></ul>
Smart IT Engineering Ltd. RESTful vs RESTlike/REST-RPC Web Service <ul><li>Failures with my initial RESTful WS experiments </li><ul><li>Overloaded POST
Not realizing HATEOAS, i.e. Linked and Connectedness
Not realizing the strength of content negotiation, i.e. variant support
Not realizing strength of conditional requests
Not realizing the power of HTTP Cache </li></ul></ul>
Smart IT Engineering Ltd. RESTful Web Services & ROA Questions?
Smart IT Engineering Ltd. A Design Walk-through <ul><li>What remains unchanged? </li><ul><li>Use of HTTP as message vessel
Choose supporting media types for resources </li></ul></ul>
Smart IT Engineering Ltd. Design – Content Repository <ul><li>Requirement
Contents can be separated logically in a boundary such that their definition and data can easily identified. Contents should extensively searchable, that is, by the logical partition definition type, free text etc. Contents and their fields can have multiple user configurable representations. All logical partitions should have featured contents. </li></ul>