Restful Application Protocol Babita Saini Outline Introduction - - PowerPoint PPT Presentation

restful application protocol
SMART_READER_LITE
LIVE PREVIEW

Restful Application Protocol Babita Saini Outline Introduction - - PowerPoint PPT Presentation

Restful Application Protocol Babita Saini Outline Introduction What Is a Domain Application Protocol? Design Steps A RESTful Procurement Application References Domain Application Protocol A domain application protocol is


slide-1
SLIDE 1

Restful Application Protocol

Babita Saini

slide-2
SLIDE 2

Outline

  • Introduction
  • What Is a Domain Application Protocol?
  • Design Steps
  • A RESTful Procurement Application
  • References
slide-3
SLIDE 3

Domain Application Protocol

  • A domain application protocol is the set of rules

and conventions that guides and constrains the interactions between participants in a distributed application.

  • By adhering to a protocol, participants achieve a

useful domain or business outcome.

slide-4
SLIDE 4

Procurement

  • The process implies the effective existence of a domain application

protocol, a set of rules and conventions through which participants in a distributed system coordinate their interactions to achieve a useful, domain-specific application goal.

CUSTOMER SUPPLIER Request Quote Order Goods Confirm Order Cancel Pay

slide-5
SLIDE 5

Application, application protocol, application state

  • An application is defined as the computerized

behavior directed towards achieving a particular client or end user goal. A distributed application is one in which multiple participants employ computing behavior to realize an application goal.

  • Application state is a snapshot of the state of a

distributed application at a particular point in time.

slide-6
SLIDE 6
slide-7
SLIDE 7

Application State in a RESTful Application

  • RESTful web application delegates the responsibility

for remembering the overall state of an application to the client or clients participating in that application.

  • Client is presented with additional resources which

Server encodes using links and forms(Hypermedia)

Client conversation is expensive, particularly at web scale.

slide-8
SLIDE 8

Design Steps

slide-9
SLIDE 9

Three steps to design and implement Restful Web Services

  • 1. Model applications as application protocol

state machines.

  • 2. Implement them based on resources, resource

life cycles and the server-governed rules that associate resources.

  • 3. Document and execute them using media

types, link relations, and HTTP idioms.

slide-10
SLIDE 10

Step 1: It is concerned with the design of an abstract domain application protocol.

slide-11
SLIDE 11

Procurement ¡applica/on ¡state ¡transi/ons ¡

Quote Requested Goods Ordered Order Confirmed Cancelled Paid By explicitly modeling a protocol as a state machine, we gain a better understanding of the “value stream” of application state transitions through which value is released both to the customer and to the organisation(s) owning a process.

slide-12
SLIDE 12

Step 2:

slide-13
SLIDE 13

Resource, Resource state

  • Resource : Defined as being a membership

function, which groups a set of equivalent resource representations and identifiers. Membership of this set can vary over time.

  • For most resources, a resource’s state is simply a

function of its data. For some resources,a resource’s state is also partly a function of the state of other resources with which the resource is associated through some server-governed rules.

slide-14
SLIDE 14

Operation oriented vs Resource Oriented

CreateQuote

SubmitQuote

SearchOrders RemoveItem ReserveItem CancelOrder AddItem

Quote

  • GET
  • PUT
  • POST
  • DELETE

Order

  • GET
  • PUT
  • POST
  • DELETE

Item

  • GET
  • PUT
  • POST
  • DELETE

Operation Oriented Resource Oriented

slide-15
SLIDE 15

SearchOrders CreateQuote SubmitQuote CancelOrder GET PUT POST DELETE GET PUT POST DELETE

Specialization and Innovation depends on an open set

slide-16
SLIDE 16

Ø A hypermedia system comprises a client, one or more server-governed resources, and some systemic behavior. This systemic behavior is initiated when a client makes a request of a resource – in a web application this will be a resource identified by a URI. Ø The resource responds with a representation of its resource state. This representation includes one or more hypermedia controls – links and forms – which advertise legitimate interactions with other resources. Ø The client processes the response and updates its understanding of the current state of the application. Ø If it hasn’t yet achieved its application goal, the client chooses the hypermedia control best suited to making forward progress, and operates that control. Ø Operating the control triggers another request, and the cycle begins again.

Hypermedia Systems change application state

slide-17
SLIDE 17

Hypermedia Systems Transform Application State

slide-18
SLIDE 18

Step 3

slide-19
SLIDE 19

Media types

  • A RESTful API is documented using media

types, link relations and HTTP idioms.

  • A media type value, such as application/atom+

xml, is a key into a data format. Hypermedia system implements: Ø The format to be used for representing content. Ø One or more schemas to which content must conform. Ø Hypermedia control formats. Ø Semantic annotations for hypermedia controls.

slide-20
SLIDE 20

Two Common Hypermedia Formats: XHTML and ATOM

  • Both are commonplace today
  • Both are hypermedia formats
  • – They contain links
  • Both have a processing model that explicitly

supports links

  • Which means both can describe protocols…
slide-21
SLIDE 21

XHTML

  • XHTML is just HTML that is also XML
  • For example:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" xmlns:r="http://restbucks.org"> <head> <title> XHTML Example </title> </head> <body> <p> ...

Default XML namespace Other XML namespace

slide-22
SLIDE 22

XHTML in Action

<html xmlns="http://www.w3.org/1999/xhtml"> <body> <div class="order"> <p class="location">takeAway</p> <ul class="items"> <li class="item"> <p class="name">latte</p> <p class="quantity">1</p> <p class="milk">whole</p> <p class="size">small</p> </li> </ul> <a href=http://restbucks.com/payment/1234 rel="payment">payment</ a> </div> </body> </html>

slide-23
SLIDE 23

Atom Syndication Format

  • The application/atom +

xml media type is hypermedia aware

  • We should expect links

when processing such representations.

  • Atom is a hypermedia

format – Both feeds and entries contains hypermedia controls that can describe protocols

HTTP/1.1 200 OK Content-Length: 342 Content-Type: application/atom+xml Date: Sun, 22 Mar 2009 17:04:10 GMT <entry xmlns="http://www.w3.org/2005/ Atom"> <title>Order 1234</title> <link rel="payment" href="http:// restbucks.com/payment/1234"/> <link rel="special-offer" href="http://restbucks.com/offers/ freeCookie"/> <id>http://restbucks.com/order/ 1234</id> <updated>2009-03-22T16:57:02Z</ updated> <summary>1x Cafe Latte</summary> </entry>

slide-24
SLIDE 24
  • <atom:link> element describes as a hypermedia

control, and defines five link relation values (alternate, related, self, enclosure, and via) with which links can be annotated with semantic context.

slide-25
SLIDE 25

Link Relations

  • One of the most popular ways of adding semantic context

to hypermedia controls is to annotate links with link relations.

  • Link relations describe the purpose of a link, the meaning
  • f a target resource, or the relationship between a link’s

context and the target resource.

  • A link relation helps a client use the link according to its

purpose.

  • The link has been typed with the link relation value

rb:order. This value acts as a key into a semantic. In this instance, the associated semantic indicates that the linked

  • r destination resource is an order.
slide-26
SLIDE 26

Documenting a protocol

  • A protocol can draw on pre-existing media types and

link relations, as well as invent its own.

  • AtomPub is a good example of this compose-and-

invent approach.

  • AtomPub reuses the Atom media type, which is

defined in the separate Atom Syndication Format specification; to this, it adds two new media types,

  • application/atomsvc+xml and application/atomcat

+xml,for representing service and category documents.

  • To Atom’s five link relations, AtomPub adds two

more: edit and edit-media.

slide-27
SLIDE 27

ATOM PUBLISHING PROTOCOL

slide-28
SLIDE 28

HTTP Idioms

  • HTTP idioms defines which method, status

codes and headers consumer should use to interact with a linked resource.

  • There are two approaches to communicating

which HTTP idioms a client should use over the course of an application: upfront, and inline.

  • With the upfront approach, we create a

document describing the appropriate idioms.

  • With the inline approach, we use HTTP headers

and status codes, plus entity body control data, to communicate at runtime which idioms a client can use to manipulate resource representations.

slide-29
SLIDE 29

Ways to use HTTP idioms

  • Cache-Control directives instruct intermediaries to cache content in

accordance with HTTP’s caching rules.

  • Forms (HTML, XForms, etc.) program clients with control data

(such as a URI,HTTP verb, and required Content-Type value), which the client can then use to encode and submit the form.

  • ETag headers indicate to the client that subsequent requests for the

same resource should use a conditional idiom: either a conditional GET, which uses an If-None-Match header with an entity tag value to instruct the server to return a full-blown response only if the resource addressed in the request has changed since the entity tag value was issued

  • Location – Used to flag the location of a created/moved resource –

In combination with: 201 Created, 301 Moved Permanently, 302Found,307 Temporary Redirect, 300 Multiple Choices,303 See Other.

  • 405 Method Not Allowed tells the client that the verb in the request

cannot be used; issuing an OPTIONS request for the same resource will return a 200 OK response with an Allow header specifying which verbs can be used.

slide-30
SLIDE 30
slide-31
SLIDE 31

A RESTful Procurement Application

slide-32
SLIDE 32

Application Start

It influences the behavior of any caching intermediaries – local caches, proxies, and reverse proxies – along the request–response path Generalized, format for representing quotes and orders Entry point resources

slide-33
SLIDE 33

LINK AND SEMANTIC CONTEXT

slide-34
SLIDE 34

Start

STARTED….

slide-35
SLIDE 35

Request GET /request-for-quote HTTP/1.1 Host: restbucks.com Response HTTP/1.1 200 OK Date: Mon, 26 Jul 2010 10:00:05 GMT Cache-Control: public, max-age=86400 Content-Type: application/restbucks+xml Content-Length: ... <model xmlns="http://www.w3.org/2002/xforms" schema="http://schemas.restbucks.com/shop.xsd"> <instance/> <submission resource="http://restbucks.com/quotes" method="post" mediatype="application/restbucks+xml"/> </model>

Get request--‐for--‐quote form

XForms <model> element is used to communicate control data to the client Specify the URI, HTTP method and Content-Type header value to be used when submitting the form

slide-36
SLIDE 36

Control data (forms)

HTML <form name="input" action="/registration" method="post" enctype="application/x-www-form-urlencoded"> <input type="text" name="user" /> <input type="submit" value="Submit" /> </form> XForms <model xmlns="http://www.w3.org/2002/xforms" schema="http://schemas.restbucks.com/rfq.xsd"> <instance/> <submission resource="http://restbucks.com/quotes" method="post" mediatype="application/restbucks+xml"/> </model>

slide-37
SLIDE 37

Submit request for quote

The client “fills out” the form – that is, it creates a request.POSTs it to the URI supplied in the control data

slide-38
SLIDE 38

Return quote

Server returns a response with a 201 Created status code, a Location header indicating the URI of the newly created quote & entity body containing a representation (contains : a self link an rb:order-form link.

slide-39
SLIDE 39

Started to Quote Requested

slide-40
SLIDE 40

Design strategies

Typed link to Form: Link relation describes meaning of form in context of current representation Inline control data :Form provides inline control data—HTTP idioms Collection: POST form to collection resource

slide-41
SLIDE 41

Client gets the order form

Content-Location header indicates the source for this form data.

slide-42
SLIDE 42

Resources adapt the domain for hypermedia clients

slide-43
SLIDE 43

Submit an order form

slide-44
SLIDE 44

Indicate order submitted successfully

status code separates the action of accepting the request from the work necessary to fulfil it.

slide-45
SLIDE 45

Quote Requested to Goods Ordered

slide-46
SLIDE 46

Design Strategies

Prefilled form Alternate representation of quote data, adapted for submitting an order 202 Accepted Long--‐running operations Successfully accepted request but result is not immediately available.

slide-47
SLIDE 47

Check state of order

Request GET /orders/9876 HTTP/1.1 Host: restbucks.com Response HTTP/1.1 404 Not Found

Check state of order again

Request GET /orders/9876 HTTP/1.1! Host: restbucks.com!

slide-48
SLIDE 48

Return order

The order’s <status> element indicates, the

  • rder is Awaiting

Payment

slide-49
SLIDE 49

Etag Header

slide-50
SLIDE 50

Goods Ordered to order confirmed

slide-51
SLIDE 51

Navigate to payment form

Request GET https://example.org/payments/1010 HTTP/ 1.1

slide-52
SLIDE 52

Return payment form

200 ok from awaiting state

slide-53
SLIDE 53

Submit payment details

Request POST https://example.org/payments/1010 HTTP/1.1 Content-Type: application/x-www-form urlencoded Content-Length: ... cardtype=Visa+Debit&name=MR+JOHN+SMITH! ➥&card-number=4876512418675010&security- code=212!

slide-54
SLIDE 54

Confirm Payment

slide-55
SLIDE 55

Notify Restbucks

Request GET /orders/9876 HTTP/1.1! Host: restbucks.com! If-None-Match: "4d3e88c9"!

Get order

slide-56
SLIDE 56

Return Order

slide-57
SLIDE 57

Order confirmed to paid

slide-58
SLIDE 58
slide-59
SLIDE 59

References

  • Rest in Practice by Jim

Webber, Ian Robinson

  • http://dev.opera.com/

articles/view/http-basic- introduction/

  • http://blog.

2partsmagic.com/restful-uri- design/