rest in good! - devcon 2017

36
REST in good! André Goliath

Upload: andre-goliath

Post on 22-Jan-2018

58 views

Category:

Technology


1 download

TRANSCRIPT

REST in good!

André Goliath

[skip motivation, because REST]

REST is an abbreviation for…

a) Really Strange Topic

b) Relational Enterprise Scheme Trade

c) Representational State Transfer

d) ??? [use “send a comment”]

Representational State Transfer

Building Blocks: HTTP(as far as this talk is concerned,

HTTP and HTTP/2 are the same)

URIs (Uniform Resource Identifier)

vs.

URLs (Uniform Resource Locator)

URLs locate resources

(“something that we can talk about

and that has ‘properties’ ”)

Verbs or methods

operate on resources

safe idempotent

GET

HEAD

OPTIONS

PUT

PATCH

DELETE

POST

HEAD and GET are the same,

except HEAD not sending the actual body

Browsers use OPTIONS to implement

Cross-Origin Resource Sharing (CORS)

OPTIONS and CORS Preflights

Use PUT to create or replace resources

if you know their exact URL

Otherwise, POST to the parent resource

Use PUT to replace a whole resource at once,

PUT operations must be idempotent!

Use PATCH to update partial data

We have a list of items, e.g. /messages

How do we get the number of all messages?

a) GET /messages and {…count: 5…}

b) GET /messages/count

c) GET /messages?count

d) ??? [use “send a comment”]

We have a list of items, e.g. /messages

How do we get the number of all messages?

> HEAD /messages

< …

< X-COUNT: 5

We GET the current temperature from

/house/livingroom/temp. But the sensor is

kinda unstable and needs to be power-cycled

from time to time. How would you do it?

a) POST /house/livingroom/temp/reset

b) PUT /house/livingroom/temp/reset

c) DELETE /house/livingroom/temp

d) ??? [use “send a comment”]

We GET the current temperature from

/house/livingroom/temp. But the sensor is

kinda unstable and needs to be power-cycled

from time to time. How would you do it?

> POST /house/scheduledTasks

> {

> time: ‘now’,

> action: ‘reset’,

> target: ‘livingroom/temp’

> }

HTTP is kinda bad

for long-running processes

No, you´re just

using it wrong

> POST /house/scheduledTasks

> {

> time: ‘in 2 hours’,

> action: ‘reset’,

> target: ‘livingroom/temp’

> }

> POST /house/scheduledTasks

> {

> time: ‘in 2 hours’,

> action: ‘reset’,

> target: ‘livingroom/temp’

> }

< HTTP/1.1 201 Created

< Location: https://…/scheduledTasks/7dhe9u

< HTTP/1.1 201 Created

< Location: https://…/scheduledTasks/7dhe9u

> GET /house/scheduledTasks/7dhe9u

< Cache-Control "max-age=3600, must-revalidate"

< {

< time: ‘2017-11-10T13:15:28+02:00’,

< action: ‘reset’,

< target: ‘livingroom/temp’,

< status: ‘scheduled’

< }

… two hours later …

> GET /house/scheduledTasks/7dhe9u

< {

< time: ‘2017-11-10T13:15:32+02:00’,

< action: ‘reset’,

< target: ‘livingroom/temp’,

< status: ‘done’,

< result: 200

< }

… one eternity later …

> GET /house/scheduledTasks/7dhe9u

< HTTP/1.1 404 Not Found

> GET /house/scheduledTasks/7dhe9u

< ???

a) 404 – Not Found

b) 202 - Accepted

c) 204 – No Content

d) 410 – Gone

e) ??? [use “send a comment”]

> GET /house/scheduledTasks/7dhe9u

< HTTP/1.1 410 Gone

> GET /house/scheduledTasks/1234567890

< HTTP/1.1 404 Not Found

HTTP

Status Codes

(just RTFM)

Why 404 has no ‘double meaning’

> GET /house/scheduledTasks/1234567890

< HTTP/1.1 404 Not Found

> GET /house/scheduledTasks/7dhe9u

< HTTP/1.1 410 Gone

> GET /house/scheduledTasks/8fks3k

< HTTP/1.1 200 OK

> GET /house/scheduledTasks/9edm4d

< HTTP/1.1 200 OK

Why 404 has no ‘double meaning’

> GET /houes/scheduledTasks/1234567890

< HTTP/1.1 404 Not Found

> GET /houes/scheduledTasks/7dhe9u

< HTTP/1.1 404 Not Found

> GET /houes/scheduledTasks/8fks3k

< HTTP/1.1 404 Not Found

> GET /houes/scheduledTasks/9edm4d

< HTTP/1.1 404 Not Found

Level Up your API!

The Richardson Maturity Model

https://martinfowler.com/articles/richardsonMaturityModel.html

Hypertext as the Engine of Application State

(HATEOAS)

https://github.com/farrelmr/introtospringdatarest

Thank You!

André Goliath

Lead Developer

[email protected]