Slide 1

Slide 1 text

Embracing Change with REST Kyle Fuller

Slide 2

Slide 2 text

Representational State Transfer

Slide 3

Slide 3 text

What is REST?

Slide 4

Slide 4 text

Roy Fielding

Slide 5

Slide 5 text

“Anticipating change is one of the central themes of REST”

Slide 6

Slide 6 text

"Move fast and break things" Mark Zuckerberg, Facebook

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

"Each version will remain for at least 2 years from release." Facebook

Slide 10

Slide 10 text

Move Fast, Break Nothing

Slide 11

Slide 11 text

REST is not... 4 CRUD 4 Pretty URLs 4 JSON 4 HTTP Verbs 4 Routing

Slide 12

Slide 12 text

It’s not about exposing your database

Slide 13

Slide 13 text

But... we design apps this way

Slide 14

Slide 14 text

Actually, We Don't

Slide 15

Slide 15 text

We design implementations this way

Slide 16

Slide 16 text

Todo 4 id 4 title 4 status

Slide 17

Slide 17 text

Designing API by exposing implementation details

Slide 18

Slide 18 text

Designing API by exposing implementation details

Slide 19

Slide 19 text

We could design via states

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

How do we Transfer State from Server to Client?

Slide 22

Slide 22 text

We transfer Representations of State

Slide 23

Slide 23 text

Hence Representational State Transfer

Slide 24

Slide 24 text

How do we transfer state transitions?

Slide 25

Slide 25 text

HATEOAS

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

HATEOAS Hypermedia as the Engine of Application State

Slide 28

Slide 28 text

Relations

Slide 29

Slide 29 text

Transitions

Slide 30

Slide 30 text

"HATEOAS" is a REST constraint.

Slide 31

Slide 31 text

Not an option. Not an ideal. Hypermedia is a constraint.

Slide 32

Slide 32 text

As in, you either do it or you aren’t doing REST.

Slide 33

Slide 33 text

GET http://polls.com/

Things you can do

Slide 34

Slide 34 text

GET http://polls.com/

Things you can do

Slide 35

Slide 35 text

Creating a question the bad way 4 Construct a 'new question' URL 4 Submit a form given our pre-existing knowledge of how it may look 4 Visit the page (it may or may not work)

Slide 36

Slide 36 text

with hypermedia 4 We’re offered to create a question 4 Fill in the form it gives you 4 Submit the form

Slide 37

Slide 37 text

Exactly how you browse the web

Slide 38

Slide 38 text

Clients can have the same benefits by doing this too

Slide 39

Slide 39 text

Polls

Slide 40

Slide 40 text

Questions 4 create 4 list

Slide 41

Slide 41 text

Question 4 delete 4 choices

Slide 42

Slide 42 text

Choice 4 vote

Slide 43

Slide 43 text

One Does Not Simply Use JSON

Slide 44

Slide 44 text

Hypermedia Formats 4 HAL (application/hal+json) 4 Siren (application/vnd.siren+json)

Slide 45

Slide 45 text

Siren Link { "links": [ { "rel": ["next"], "href": "/questions?page=2" } ] }

Slide 46

Slide 46 text

Siren Link { "links": [ { "rel": ["choice"], "href": "/questions/1/choices/1" } ] }

Slide 47

Slide 47 text

Siren Link { "entities": [ { "links" { { "rel": ["self"], "href": "/questions/1/choices/1" } }, "rel": ["choices"], "properties": {"choice": "Swift", "votes": 22} } ] }

Slide 48

Slide 48 text

Siren Link { "entities": [ { "rel": ["choices"], "properties": {"choice": "Swift", "votes": 22}, "actions": [ { "href": "/questions/1/choices/1", "name": "vote", "method": "POST" } ] } ] }

Slide 49

Slide 49 text

Siren Action { "actions": { "create": { "href": "/questions", "method": "POST", "fields": [ { "name": "question" }, { "name": "choices" } ] } } }

Slide 50

Slide 50 text

Siren Action { "actions": { "create": { "href": "/questions", "method": "POST", "fields": [ { "name": "question" }, { "name": "choices" } ], "type": "application/x-www-form-urlencoded" } } }

Slide 51

Slide 51 text

Siren Action { "actions": { "create": { "href": "/questions", "method": "POST", "fields": [ { "name": "question" }, { "name": "choices" } ], "type": "application/json" } } }

Slide 52

Slide 52 text

{ "actions": { "create": { "href": "/questions", "method": "POST", "fields": [ { "name": "question" }, { "name": "choices" } ], "type": "application/json" } } }

Slide 53

Slide 53 text

Ability to change implementation details

Slide 54

Slide 54 text

Ability to change implementation details 4 Change URIs of resources (/polls/{id} -> / questions/{slug})

Slide 55

Slide 55 text

Ability to change implementation details 4 Change URIs of resources (/polls/{id} -> / questions/{slug}) 4 Change HTTP methods (PUT -> PATCH)

Slide 56

Slide 56 text

Ability to change implementation details 4 Change URIs of resources (/polls/{id} -> / questions/{slug}) 4 Change HTTP methods (PUT -> PATCH) 4 Change the content-type

Slide 57

Slide 57 text

Ability to change implementation details 4 Change URIs of resources (/polls/{id} -> / questions/{slug}) 4 Change HTTP methods (PUT -> PATCH) 4 Change the content-type 4 Change fields used in forms

Slide 58

Slide 58 text

Teach Clients Semantic meaning of the Domain

Slide 59

Slide 59 text

Don’t hard-code implementation details

Slide 60

Slide 60 text

Demo

Slide 61

Slide 61 text

Representor https://github.com/the-hypermedia-project/representor-swift

Slide 62

Slide 62 text

Using a transition if let transition = representor.transitions["create"] { }

Slide 63

Slide 63 text

Using a transition if let transition = representor.transitions["create"] { } else { // Gracefully handle the lack of this transition }

Slide 64

Slide 64 text

Using a transition if let transition = representor.transitions["create"] { transition.attributes }

Slide 65

Slide 65 text

Using a transition if let transition = representor.transitions["create"] { transition.method transition.uri transition.suggestedContentType }

Slide 66

Slide 66 text

Performing a Transition request(transition, attributes:[ "question": "Favourite programming language?", "choices": [ "Swift", "Python", ] ])

Slide 67

Slide 67 text

REST

Slide 68

Slide 68 text

Resources 4 REST APIs must be hypertext driven 4 Representor 4 Roy Fielding on Versioning 4 Polls API 4 rivr rest 4 Solving Fizz Buzz with Hypermedia

Slide 69

Slide 69 text

Kyle Fuller ! fuller.li/slides ! [email protected]