REST Architecture with use case and example

24
REST(Representational State Transfer) - SHAILESH SINGH

Transcript of REST Architecture with use case and example

Page 1: REST Architecture with use case and example

REST(Representational State Transfer)

- SHAILESH SINGH

Page 2: REST Architecture with use case and example

1. Why so REST FUL?

Life before REST

Challenges of HTTP ?

RMI , SOAP , RPC and HTTP different famous technique to develop web services

Page 3: REST Architecture with use case and example

2. What is REST? Defined in 2000

Architects are Made, Not Born What REST father Roy Fielding says

An architecture style is a coordinated set of architectural constraints that restricts the roles and features of architectural elements . E.g. : UI layer and data layer segregation , statelessness , cacheability

Uniform Interface : Overall system architecture is simplified and the visibility of interactions is improved

Tradeoff : Degrades efficiency since Information is transferred in a standardised form rather than one which is specific to application's needs

Page 4: REST Architecture with use case and example

a. Uniform Interface

Four interface constraints

● Identification of resources ● Manipulation of resources through representations ● Self descriptive messages ● Hypermedia as the engine of application state (HATEOAS)

Page 5: REST Architecture with use case and example

b. What is resources Another way to describe REST is ROA : Resource Oriented Architecture

Any information that can be named is a resource A resource is a conceptual mapping to a set of entities not the entity

itself. Such a mapping can change over time. A resource can be a collection of entities too. Every resource has a name that uniquely identifies it – the URI Think of it like a primary key for each row in a database REST doesn't dictate URI choice. Leaves it to the application author.

Page 6: REST Architecture with use case and example

c.What If?

/getAccount/getAllAccounts/searchAccounts/createDirectory/updateGroup/updateGroupName/findGroupsByDirectory/verifyAccountEmailAddressAs you move from an action oriented design towards resource oriented design, thinking of everything as nouns is one of the early challenges to overcome

Page 7: REST Architecture with use case and example

Identification of Resources

Page 8: REST Architecture with use case and example

Identification of Resources

Page 9: REST Architecture with use case and example

d.The AnswerFundamentally two types of resources:

Collection Resource/applications

/books

/orders

Instance Resource/applications/a1b2c3

/books/1235

/orders/abcdef

Question :

Guess REST equivalent for : Transaction.approve and Account.pay

TransactionApproval and AccountPayment

Page 10: REST Architecture with use case and example

e.Behavior

POST, GET, PUT, DELETE

≠ 1:1Create, Read, Update, Delete

Page 11: REST Architecture with use case and example

f. PUT for Create

Identifier is known by the client:

PUT:

Used to create a resource, or overwrite it. While you specify the resources new URL.For a new resource:

PUT /questions/<new_question> HTTP/1.1

Host: www.example.com/

To overwrite an existing resource:PUT /questions/<existing_question> HTTP/1.1

Host: www.example.com/

 PUT is Idempotent

Page 12: REST Architecture with use case and example

g.POST as Create POST:Used to modify and update a resource

POST /questions/<existing_question> HTTP/1.1

Host: www.example.com/

Note that the following is an error:POST /questions/<new_question> HTTP/1.1Host: www.example.com/

If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a 'resource not found' error because <new question> does not exist yet. You should PUT the <new question> resource on the server first.You could though do something like this to create a resources using POST:

POST /applications{ “name”: “Best App Ever”}Response:201 Created

Location: https://api.singh.com/applications/a1b2c3

POST NOT Idempotent -> x++ vs. x=4

Page 13: REST Architecture with use case and example

4.a. Example/case Studies

Fine grained CRUD resources Vs Coarse Grained resources:

Like Operation on blog post (“/posts/{post_id}/likes”) Comment Operation on blog post (“/posts/{post_id}/comments”)

vs The single coarse grained resource “Post”(/posts/{post_id}” for “liking” or “commenting”

Page 14: REST Architecture with use case and example

4.b. Example/case Studies

Change the Address:We can update “Customer” address via “Customers/001/Address/KA001/” or “Address/KA001/”

VS

Design the API around the resources that are based on the business processes and domain events . To update an existing bank customer’s address, a POST request can be made to “ChangeOfAddress” resource. 

Very important to distinguish between resources in REST API and domain entities in a domain driven design.

Shailesh Singh
Page 15: REST Architecture with use case and example

4.c HATEOAS

HATEOAS=Hypermedia As The Engine Of Application State

Path is the hierarchical and the query is the non-hierarchical part of the URIs.

! Magic awesome sauce to improve REST!

According to the HATEOAS constraint your client has to follow hyperlinks sent by the service. Those hyperlinks must be annotated with metadata regarding the semantics of them

Page 16: REST Architecture with use case and example

HATEOAS constrain

A REST client enters a REST application through a simple fixed URL. All future actions the client may take are discovered within resource representations returned from the server.

E.g:

RequestGET /account/12345HTTP/1.1 Host: somebank.orgAccept: application/xml

Response:HTTP/1.1 200 OKContent-Type: application/xmlContent-Length: ...

<?xml version="1.0"?> <account> <account_number>12345</account_number> <balance currency="usd">100.00</balance> <link rel="deposit" href="https://somebank.org/account/12345/deposit" /> <link rel="withdraw" href="https://somebank.org/account/12345/withdraw" /> <link rel="transfer" href="https://somebank.org/account/12345/transfer" /> <link rel="close" href="https://somebank.org/account/12345/close" /> </account>

Later Response:HTTP/1.1 200 OK Content-Type: application/xml Content-Length: ...

<?xml version="1.0"?> <account> <account_number>12345</account_number> <balance currency="usd">-25.00</balance> <link rel="deposit" href="https://somebank.org/account/12345/deposit" /> </account>

Page 17: REST Architecture with use case and example

Book Flight Ticket

Page 18: REST Architecture with use case and example

Get Flight Search Result

Page 19: REST Architecture with use case and example

Confirm a Flight

Page 20: REST Architecture with use case and example

Payment still Pending

Page 21: REST Architecture with use case and example

Payment

Page 22: REST Architecture with use case and example

Fetch E-Ticket

Page 23: REST Architecture with use case and example

Worked examples

How to GET a Cup of Coffee by Jim Webber, Savas Parastatidis & Ian Robinson Oct 02, 2008 http://www.infoq.com/articles/webber-rest-workflow

Page 24: REST Architecture with use case and example

Questions

http://petstore.swagger.io/

http://start.spring.io/