51
Introduction to Hypermedia APIs Eric Oestrich @ericoestrich oestri.ch smartlogicsolutions.com

Intro to Hypermedia APIs

Embed Size (px)

DESCRIPTION

An introduction to Hypermedia APIs from software developer Eric Oestrich.

Citation preview

Page 1: Intro to Hypermedia APIs

Introduction to Hypermedia APIs

Eric Oestrich@ericoestrich

oestri.chsmartlogicsolutions.com

Page 2: Intro to Hypermedia APIs

What is a Hypermedia API?

Page 3: Intro to Hypermedia APIs

RESTREpresentational State Transfer

Page 4: Intro to Hypermedia APIs

Relies Heavily on HTTP● Methods and URI● Media Types● Client Server

Page 5: Intro to Hypermedia APIs

Relies Heavily on HTTP (Cont)

● Stateless● Caching● Uniform Interface (HATEOAS)

Page 6: Intro to Hypermedia APIs

Methods and URIs

Page 7: Intro to Hypermedia APIs

Five RoutesGET /ordersPOST /ordersGET /orders/:idPUT /orders/:idDELETE /orders/:id

Page 8: Intro to Hypermedia APIs

GET /ordersList of orders

Page 9: Intro to Hypermedia APIs

POST /ordersCreate an order

Page 10: Intro to Hypermedia APIs

GET /orders/:idView a single order

Page 11: Intro to Hypermedia APIs

PUT /orders/:idReplace or create an order

Page 12: Intro to Hypermedia APIs

DELETE /orders/:idDelete an order

Page 13: Intro to Hypermedia APIs

A bit about HTTP Verbs

Page 14: Intro to Hypermedia APIs

GETRetrieve a resource

Page 15: Intro to Hypermedia APIs

POSTMost generic

Page 16: Intro to Hypermedia APIs

PUTReplace or Create

Not partial

Page 17: Intro to Hypermedia APIs

DELETEDelete a resource

Page 18: Intro to Hypermedia APIs

PATCHUpdate partiallyMust be a diff

Page 19: Intro to Hypermedia APIs

Media Types

Page 20: Intro to Hypermedia APIs

Formats● JSON● XML● XHTML

Page 21: Intro to Hypermedia APIs

Setting the Media Type● Accept Headers● Rails way - .:format

Page 22: Intro to Hypermedia APIs

Representational basedModels != Representations

Page 23: Intro to Hypermedia APIs

Uniform Interface

Page 24: Intro to Hypermedia APIs

Uniform Interface● Identification of resources● Manipulation of resources via

representations● Self descriptive messages● Hypermedia as the engine of application

state

Page 25: Intro to Hypermedia APIs

Identification of resources

Page 26: Intro to Hypermedia APIs

Manipulation of resources via representations

Page 27: Intro to Hypermedia APIs

Self descriptive messages

Statelessness

Page 28: Intro to Hypermedia APIs

Hypermedia as the engine of application state

HATEOAS

Page 29: Intro to Hypermedia APIs

Clients should not know how to build routes

Page 30: Intro to Hypermedia APIs

Enter HALJSON cannot describe a link

Page 31: Intro to Hypermedia APIs

HAL is a media type for defining links in JSON

application/hal+json

Page 32: Intro to Hypermedia APIs

The GoalGET /

Page 33: Intro to Hypermedia APIs

Great, now I know what a Hypermedia API is.

Page 34: Intro to Hypermedia APIs

How do I make one?

Page 35: Intro to Hypermedia APIs

Rails + Lots of GemsAlso monkey patching

Page 36: Intro to Hypermedia APIs

Gems● ActiveModel::Serializer● RspecApiDocumentation● Raddocs● A few other handy ones

Page 37: Intro to Hypermedia APIs

$ rails new hypermedia_api --skip-test-unit

Page 38: Intro to Hypermedia APIs

$ vim Gemfilegem 'raddocs'gem 'active_model_serializers'

Page 39: Intro to Hypermedia APIs

$ vim Gemfilegroup :test, :development do gem 'rspec-rails' gem 'rspec_api_documentation'end

Page 40: Intro to Hypermedia APIs

Get rspec set up

Page 41: Intro to Hypermedia APIs

Create our first acceptance test

Page 42: Intro to Hypermedia APIs

Generate models, index action, serializers

Page 43: Intro to Hypermedia APIs

Show

Page 44: Intro to Hypermedia APIs

Create

Page 45: Intro to Hypermedia APIs

Update

Page 46: Intro to Hypermedia APIs

Delete

Page 47: Intro to Hypermedia APIs

Setup HALLet the monkey patching begin

Page 48: Intro to Hypermedia APIs

We're missing something...

Page 49: Intro to Hypermedia APIs

$ curl http://localhost:3000/""

Page 50: Intro to Hypermedia APIs

Root Resource

Page 51: Intro to Hypermedia APIs

Questions?Eric Oestrich@ericoestrich

oestri.chsmartlogicsolutions.com