38
Paw luckymarmot.com The ultimate HTTP client for Mac @mittsh Micha Mazaheri Solving problems Focus on UX

Pragmatic Hypermedia REST APIs

Embed Size (px)

Citation preview

Page 1: Pragmatic Hypermedia REST APIs

Paw

luckymarmot.com

The ultimate HTTP client for Mac

@mittsh

Micha Mazaheri

• Solving problems • Focus on UX

Page 2: Pragmatic Hypermedia REST APIs
Page 3: Pragmatic Hypermedia REST APIs

Pragmatic Hypermedia REST

Page 4: Pragmatic Hypermedia REST APIs

Mike Amundsen: A Hypermedia Hero's Tale

What is hypermedia?

Page 5: Pragmatic Hypermedia REST APIs

$ curl https://api.github.com { "current_user_url": "https://api.github.com/user", "code_search_url": "https://api.github.com/search/code?…", "events_url": "https://api.github.com/events", "feeds_url": "https://api.github.com/feeds", "followers_url": "https://api.github.com/user/followers", "following_url": "https://api.github.com/user/following{/target}", "repository_url": "https://api.github.com/repos/{owner}/{repo}",

... }

"repository_url": "https://api.github.com/repos/{owner}/{repo}",

Page 6: Pragmatic Hypermedia REST APIs

$ curl https://api.github.com/repos/luckymarmot/paw { "id": 6778468, "name": "paw", "full_name": "luckymarmot/paw", "owner": { "login": "luckymarmot", "id": 3215332, "url": "https://api.github.com/users/luckymarmot", "html_url": "https://github.com/luckymarmot", ... }, "private": true, "html_url": "https://github.com/luckymarmot/paw", "description": "Paw", "fork": false, "url": "https://api.github.com/repos/luckymarmot/paw", "forks_url": "https://api.github.com/repos/luckymarmot/

"url": "https://api.github.com/users/luckymarmot",

Page 7: Pragmatic Hypermedia REST APIs

It’s also about Describing Resources

Page 8: Pragmatic Hypermedia REST APIs

JSON-LD• Linked Data • RDF • SPARQL (search) • Handle complex data sets

Page 9: Pragmatic Hypermedia REST APIs

{ "name": "Jon Snow", "homepage": "http://jon-snow.winterfell/" }

Page 10: Pragmatic Hypermedia REST APIs

{ "@type": "Person", "name": "Jon Snow", "homepage": "http://jon-snow.winterfell/" }

Page 11: Pragmatic Hypermedia REST APIs

{ "@id": "http://me.example.com", "@type": "Person", "name": "Jon Snow", "homepage": "http://jon-snow.winterfell/" }

Page 12: Pragmatic Hypermedia REST APIs

{ "@context": { "name": "http://xmlns.com/foaf/0.1/name", "homepage": { "@id": "http://xmlns.com/foaf/0.1/workplaceHomepage", "@type": "@id" }, "Person": "http://xmlns.com/foaf/0.1/Person" }, "@id": "http://me.example.com", "@type": "Person", "name": "Jon Snow", "homepage": "http://jon-snow.winterfell/" }

Page 13: Pragmatic Hypermedia REST APIs

{ "@context": { "name": "http://xmlns.com/foaf/0.1/name", "homepage": { "@id": "http://xmlns.com/foaf/0.1/workplaceHomepage", "@type": "@id" }, "geo": "http://schema.org/geo", "latitude": { "@id": "http://schema.org/latitude", "@type": "xsd:float" }, "longitude": { "@id": "http://schema.org/longitude", "@type": "xsd:float" }, "xsd": "http://www.w3.org/2001/XMLSchema#" "Person": "http://xmlns.com/foaf/0.1/Person" }, "@id": "http://me.example.com", "@type": "Person", "name": "Jon Snow", "homepage": "http://jon-snow.winterfell/" "geo": { "latitude": "40.75", "longitude": "73.98" } }

Page 14: Pragmatic Hypermedia REST APIs

What makes REST nice?• Stateless • No layer over HTTP • HTTPS made security trivial (and cheap) • One common format JSON • Not even a spec…

Page 15: Pragmatic Hypermedia REST APIs

Be YourselfDon’t make it REST if it’s not

Page 16: Pragmatic Hypermedia REST APIs

Hypermedia should be easy too

Page 17: Pragmatic Hypermedia REST APIs

HAL

{ "id": 14196149, "name": "paw", "owner": { "login": "luckymarmot", "id": 3215332 ... } }

Page 18: Pragmatic Hypermedia REST APIs

HAL{ "_links":{ "self": {"href":"/repos/luckymarmot/paw"}, "commits": {"href":"/repos/luckymarmot/paw/commits"} }, "id": 14196149, "name": "paw", "owner": { "_links":{ "self": {"href":"/users/luckymarmot"}, "followers": {"href":"/repos/luckymarmot/followers"} }, "login": "luckymarmot", "id": 3215332 ... } }

Page 19: Pragmatic Hypermedia REST APIs

_url

{ "id": 14196149, "name": "paw", "owner": { "login": "luckymarmot", "id": 3215332, ... } }

Page 20: Pragmatic Hypermedia REST APIs

_url{ "id": 14196149, "name": "paw", "url": "/repos/luckymarmot/paw", "commits_url":"/repos/luckymarmot/paw/commits" "owner": { "login": "luckymarmot", "id": 3215332, "url": "/users/luckymarmot", "followers_url": "/repos/luckymarmot/followers", ... } }

Page 21: Pragmatic Hypermedia REST APIs

Demo

Page 22: Pragmatic Hypermedia REST APIs

Make flexible apps?

Page 23: Pragmatic Hypermedia REST APIs
Page 24: Pragmatic Hypermedia REST APIs

{ "@context": { "name": "http://xmlns.com/foaf/0.1/name", "city": { "@id": "http://xmlns.com/foaf/0.1/workplaceHomepage", "@type": "@id" }, "Person": "http://xmlns.com/foaf/0.1/Person" }, "@id": "http://me.example.com", "@type": "Person", "name": "Jon Snow", "city": "http://city.winterfell/" }

Page 25: Pragmatic Hypermedia REST APIs

Bring Hypermedia Further

Page 26: Pragmatic Hypermedia REST APIs

{ "name": "Jon Snow", "partner": {

"status":"single" "url":null

} }

Page 27: Pragmatic Hypermedia REST APIs

POST /person/jon.snow/partner

Page 28: Pragmatic Hypermedia REST APIs

{ "name": "Jon Snow", "partner": {

"status":"married" "url":"/person/ygritte"

} }

"url":"/person/ygritte"

Page 29: Pragmatic Hypermedia REST APIs

The Greeting Card Problem

Page 30: Pragmatic Hypermedia REST APIs

{ "name": “Jon Snow", "partner": {

"status":"married" "url":"/person/ygritte", "congrats_url":"/greetings/…",

} }

"congrats_url":"/greetings/…",

Page 31: Pragmatic Hypermedia REST APIs

• Headline • Message • Recipient(s) • Place of the wedding for marriage cards • Name of the baby for birth cards

POST /greetings

Page 32: Pragmatic Hypermedia REST APIs

Links HAL, _url

Definition Swagger, RAML…

+

Page 33: Pragmatic Hypermedia REST APIs

{ "name": "Jon Snow", "partner": {

"status":"married" "url":"/person/ygritte", "congrats":{ "url":"/greetings", "params":{ "message":{"in": "body", "required":true}, "weddingPlace":{"in": "body", "required":false} } }

} }

Content-Type: application/**+json

Page 34: Pragmatic Hypermedia REST APIs

{ "name": "Jon Snow", "partner": {

"status":"married" "url":"/person/ygritte", "congrats":{ "url":"/greetings" }

} }

Content-Type: application/json

Page 35: Pragmatic Hypermedia REST APIs

Know who you are

Page 36: Pragmatic Hypermedia REST APIs

Make things people can (easily) use

Page 37: Pragmatic Hypermedia REST APIs

Solve real problems• Adding links • GraphQL • Describing API possibilities

Page 38: Pragmatic Hypermedia REST APIs

Thanks!

Paw

luckymarmot.com

The ultimate HTTP client for Mac

@luckymarmot