46
@serialseb [email protected] http://codebetter.com/sebastienlambla/ Architect, trainer, speaker, developer, mayhem inducer…

Links, forms and unicorns

Embed Size (px)

Citation preview

Page 1: Links, forms and unicorns

@[email protected]

http://codebetter.com/sebastienlambla/

Architect, trainer, speaker, developer, mayhem inducer…

Page 2: Links, forms and unicorns

by the people, for the people

Page 3: Links, forms and unicorns

Web Development Done Right

Page 4: Links, forms and unicorns

Building Hypermedia APIs

Why links and forms will make your ReSTful API better

Page 5: Links, forms and unicorns

Building Hypermedia APIs

Why links and forms will make your ReSTful API better

Links, forms and unicorns

Page 6: Links, forms and unicorns
Page 7: Links, forms and unicorns

Oh no!

Mission Completed

Page 8: Links, forms and unicorns

Agent Resty Galore

Page 9: Links, forms and unicorns

Your mission should you accept it…

• Go to Paris, give the agent your name and ask for his favourite monument

• Go to Brussels, tell the agent what you learnt, ask for their favourite food

• Go to Copenhagen, tell them what you learnt, ask for their favourite painter

• In Malmo, tell them what you learnt, receive the secret message

Page 10: Links, forms and unicorns

Your mission should you accept it…

• POST to /paris, expect {“status”: “ok”, “monument”: ??}

• POST to /brussels with {“monument”: ??}, expect {“status”: “ok”, “food”: ??}

• POST to /copenhagen with {“food”: ??}, expect {“status”:”ok”, “artist”: ??}

• POST to /malmo with {“artist”: ??}, expect {“status”: “ok”, “secret”: ??}

Page 11: Links, forms and unicorns

public void travel() { var response = _client .Post(“/paris”, new{name=“Pixie”}) .Send(); if (response.body.status == “ok”) { response = _client.Post(“/brussels”, new { monument=response.body.monument }).Send(); // etc }}

Page 12: Links, forms and unicorns

> POST /paris> {“name”: “resty galore”}

Page 13: Links, forms and unicorns

Oh no!

The French don’t speak English!(who knew!)

Oh no!

Page 14: Links, forms and unicorns

> POST /paris> {“name”: “resty galore”}

< 200 OK< {“status”: “oui”, “monument”: “eiffel tower”}

Page 15: Links, forms and unicorns

public void travel() { var response = _client .Post(“/paris”, new{name=“Pixie”}) .Send(); if (response.status == 200) { response = _client.Post(“/brussels”, new { monument=response.body.monument }).Send(); // etc }}

Page 16: Links, forms and unicorns

> POST /paris> {“name”: “resty galore”}

< 200 OK< {“monument”: “eiffel tower”}

Page 17: Links, forms and unicorns

Mission Paris Completed

Page 18: Links, forms and unicorns

> POST /brussels> {“monument”: “eiffel tower”}

Page 19: Links, forms and unicorns

Oh no!

The Belgium agent was captured and the message is now London!

Oh no!

Page 20: Links, forms and unicorns

> POST /brussels> {“monument”: “eiffel tower”}

< 410 Gone somewhere colder

Page 21: Links, forms and unicorns

We could send an agent to Brussels to warn Resty Galore…

Page 22: Links, forms and unicorns

> POST /brussels> {“monument”: “eiffel tower”}

< 307 Redirect to London, quick!

Page 23: Links, forms and unicorns

Keeping agents in each location we need to redirect is expensive.

Travelling for nothing is also expensive.

Or Resty Galore could start her journey with…

Page 24: Links, forms and unicorns

Keeping agents in each location we need to redirect is expensive.

Or Resty Galore could start her journey with…

Page 25: Links, forms and unicorns
Page 26: Links, forms and unicorns

> POST /paris> {“name”: “resty galore”}

< 200 OK< Link: </london>; rel=“next”< {“monument”: “eiffel tower”}

Page 27: Links, forms and unicorns

public void travel() { var response = _client .Post(“/paris”, new{name=“Pixie”}) .Send(); if (response.status == 200) { response = _client.Post(response.links.next, new { monument = response.body.monument }).Send(); // etc }}

Page 28: Links, forms and unicorns

> POST /london> {“monument”: “eiffel tower”}

< 418 I’m a teapot< < < < <

Page 29: Links, forms and unicorns

> POST /london> {“monument”: “eiffel tower”}

< 200 OK< Link: </copenhagen>; rel=“next”< {“food”: “Chicken Tikka Massala”}

Page 30: Links, forms and unicorns

Mission London Completed

Page 31: Links, forms and unicorns

> POST /copenhagen> {“monument”: “eiffel tower”}

Page 32: Links, forms and unicorns

Oh no!

The Danish wants more security and require both the food and the

monument!

Oh no!

Page 33: Links, forms and unicorns

> POST /copenhagen> {“monument”: “eiffel tower”}

< 400 Bad request dudette

Page 34: Links, forms and unicorns

Forms

Page 35: Links, forms and unicorns

> GET /copenhagen< 200 OK< {“form”: {< “monument”: null,< “food”: null< }}

> POST /copenhagen> {“monument”: “eiffel tower”,> “food”: “Chicken Tikka Massala”}< 200 OK< Link: </malmo>; rel=“next”< {“author”: “hans christian andersen”}

Page 36: Links, forms and unicorns

Mission Copenhagen Completed

Page 37: Links, forms and unicorns

> GET /malmo< 200 OK< {“form”: {< “author”: null< }}

Page 38: Links, forms and unicorns

Oh no!

The agent in Malmo is a Mole! They have to be decommissioned!

Oh no!

Page 39: Links, forms and unicorns
Page 40: Links, forms and unicorns

> GET /malmo< 200 OK< {“form”: {< “author”: null< }}

> POST /malmo> {“author”: “hans christian andersen”}< 200 OK< {“secret”: “not yet…”}

Page 41: Links, forms and unicorns

Forms with control data

Page 42: Links, forms and unicorns

> GET /malmo< 200 OK< {“form”: {< “method”: “DELETE”< “href”: “/malmo/mole”< }}

> DELETE /malmo/mole> 200 OK< {“secret”: “ReST is amazing.”}

Page 43: Links, forms and unicorns

Mission AccomplishedWell done agent Resty Galore!

Page 44: Links, forms and unicorns

Resty Galore can take a holiday

Page 45: Links, forms and unicorns

We lowered coupling by…

• Respecting the Uniform Interface (status codes, verbs…)

• Introducing Links to navigate• Introducing forms to know what to send• Introducing control data in forms to enable a

change in workflow with no change in the client code.

Page 46: Links, forms and unicorns

• OpenRasta - http://openrasta.org• OpenWrap – http://openwrap.org

• Web linking - http://tools.ietf.org/html/rfc5988