This document discusses media types in RESTful applications. It defines what a media type is, including that it has a name, family of compatible schemas, hypermedia semantics, and processing rules. It discusses how to design a media type, including defining extensible schemas and avoiding mandatory elements. It also explains how media types enable evolvability by limiting contract changes to adding or removing data and controls while clients can ignore unknown elements. This allows services, clients, and contracts to evolve independently through loose coupling.
73. Automatically check
prices and place order if
price is below threshold
User
Shop A
User Agent
(Crawler)
Shop B
74. UC 01: Automatically check prices and...
Primary Actor: User (Buyer)
Precondition: Crawler knows catalog entry URIs
Main Success Scenario:
1. User starts crawler
2. Crawler requests catalogs of known shops
3. Crawler compares prices of items to threshold
4. Crawler selects item if price below threshold
5. Crawler creates order for item
6. Crawler submits order to order-processor
7. Crawler archives order response
8. User reviews archived order responses
75. UC 01: Automatically check prices and...
Primary Actor: User (Buyer)
Precondition: Crawler knows catalog entry URIs
Main Success Scenario:
1. User starts crawler
2. Crawler requests catalogs of known shops
3. Crawler compares prices of items to threshold
4. Crawler selects item if price below threshold
5. Crawler creates order for item
6. Crawler submits order to order-processor
7. Crawler archives order response
8. User reviews archived order responses
76. Elements of Example
Media Type
Data:
Catalog (with items)
Order (with order-items)
Order Response
Hypermedia controls:
Order-Processor
(Catalog)
78. Order Example
<order>
<buyer>...</buyer>
<line-items>
<line-item id=”123”>
<price currency=”EUR”>27.99</price>
<quantity>4</quantity>
</line-item>
<line-item id=”456”>
<price currency=”EUR”>12.95</price>
<quantity>4</quantity>
</line-item>
</line-items>
</order>
79. application/procurement
•Three schemas <catalog>, <order> and
<order-response>
•No mandatory elements
•Extensible
•Specified as version 1
•Processing Rules
If an <order-processor> element is present an order can be POSTed to that resource.
Such an oder accepting resource SHOULD accept <order>-documents.
...
110. If you are a client developer...
•Use from the media type what suits your
intended application
111. If you are a client developer...
•Use from the media type what suits your
intended application
•Ignore unknown data and controls
112. If you are a client developer...
•Use from the media type what suits your
intended application
•Ignore unknown data and controls
•“What if” programming style
Upper four rather well understood by now. Mostly, people get the caching right, apply statelessness to invoke scalability.\n\nEvolvability is often ignored when REST is applied - although it is one of the key benefits of REST.\n
Evolvability however seems to be ignored most of the times REST is applied.\n\nWhy is it so important?\n\nChange is inevitable to support new requirements. But it is difficult and costly to change a service when we have to coordinate that change with client owners. Especially if there are many clients that are difficult to reach.\n
No coordination with clients necessary - Amazon never calls\n\nNo running application (e.g. an ongoing online purchase) has to be aborted\n\nHow can we reach that goal?\n\nLook at how communication between components works...\n
Communication requires contract.\n\nTraditionally contract is owned by server.\n\nWhen server evolves it changes the contract.\n\nCoordination with clients necessary to apply changed contract.\n\nThe more clients there are and the more difficult it is to reach them the more difficult and costly is such a contract change.\n\n\n
REST moves contract to global space.\n\nServer does not own contract anymore.\n\nServer cannot change the contract.\n\nServer change needs to take place within the bounds of the contract.\n\n\nThis is where media types come in: REST introduces media types to serve as the global contract of communication.\n\nIn order to understand how REST achieves the evolvability goal we need to look at the question:\n\nHow do media types provide a contract that is flexible enough to allow the server to change but still contract enough to allow communication.\n\nThe rest of the talk is looking into three questions around media types:\n\n
\n
\n
\n
Use HTML as an example\n
Use HTML as an example\n
Use HTML as an example\n
Use HTML as an example\n
Use HTML as an example\n
The media type must enable the execution of a canonical use case.\n\nTransfer data and control.\n\n
Explain use case.\n
Use written use cases to drive media type design.\n
Extract data and hypermedia controls from use case text scenario text.\n
Elements derived from written use case.\n\n
\n
\n
We define our media type application/procurement as follows.\n\nWe have now designed an example media type\n
Recall the problem - server does not own contract so change has to happen within that contract.\n\nlook at how it serves the problem of communication bewtween c und s\n\nsince we do not communicate, media type as the contract must tell everything. and enable the evolution of the server.\n\nSo, let&#x2019;s implement the shop server...\n
Silly catalog returning resource - but is that wrong?\n
\n
Better - but on what basis did we do that?\n
Media type provides a set of available data and control elements and combination rules.\n\nServer developers implement the service for a certain purpose. Pick from the media type what suits that purpose.\n\nAka common sense...\n
the question really is: why do we programm this this way? What is driving us since the media type does not really tell us.\n
Why did we implement the crawler in this way?\n\nMedia type provides idea of &#x2018;set&#x2019; of possible applications.\n\n\n
Media type provides a set of available data and control elements and combination rules.\n\nMedia type provides an understanding of the canonical behavior of the service. Apply that to your intended application.\n\nHow can I realize my intended application using the media type.\n\nThere might be other media types for which I also do this.\n\nIf you give me media type X I can solve my application if all my What-Ifs hold true.\n
Timing assumptions, for example are uniform.\n
Add in-stock quantity to items.\n\nFor example to build an HTML page for a shop from catalog data.\n
Not a problem from the point of view of the media type\n
\n
\n
If they turn out to be useful to others, add extensions to the media type to share them with others. This is how the media type evolves over time.\n
Suppose we have to return from some shop a catalog without an order processor.\n\n
\n
This will fail because the existence of the hypermedia control is blindly assumed. Yet it can&#x2019;t.\n\n\n
\n
Error handling\n
Don&#x2019;t assume that something will happen that you cannot control.\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
A contract that is flexible enough to allow the server to change yet contract enough to allow enable communication.\n
Client and server developers use media types to learn what to expect and what to possibly provide.\n\nClient developers need to match their intended application to the data and control elements provided.\n\nServer developers need to match their intended exposed capabilities to the data and controls provided.\n