embracing http in the era of api’s

69
An evening with Hadi Hariri

Upload: visug

Post on 14-Jul-2015

84 views

Category:

Software


0 download

TRANSCRIPT

An evening with

Hadi Hariri

Next event • Next%event:%The%highlights%of%TechEd%2014%%

•  Kurt%Claeys%%•  December%8th%2014%

Prize draw • Put$your$business$card$(or$a$piece$of$paper…$or$a$napkin!)$in$the$box$outside$

• Win$a$DevExpress$DevExtreme$Web$license$

Thanks for partnering with Visug!

And now… • Hadi%Hariri%

•  Embracing*HTTP*in*the*era*of*API’s*•  So*you*write*JavaScript?*Keep*the*crap*out*of*there*then!*

Embracing HTTP in the era of Web API’s

Hadi Hariri

Announcement

With HTTP, your API is your Application

With HTTP, your API is your Application

Two Interfaces

Web Application

User Interface

API

The Advantages

• It’s easy

• No real “other” way

The Disadvantages

• Two Systems to maintain

• API is usually an afterthought

• Limitations of API

One Interface

Web Application

API

One Interface?

NEWMSG(“[email protected]”)

One Interface

API Web Application

The Advantages

• One Interface

• Single System

• The API is designed from the get-go

The Disadvantages

• The API is designed from the get-go

Is this possible?

Why is this possible? Why now?

• Frameworks Evolve - More push to Client Side

• JavaScript has become a viable language

• HTTP has been undervalued

• REST is now a buzzword

HTTP - Application Protocol

HTTP in the OSI LayerApplication

Presentation

Session

Transport

Network

Data Link

Physical

Application

HTTP 101

Demo of server. Headers / Responses, etc.

Let’s talk API

POST /domain.com/appServices 200 OK

We can do better

Multiple Endpoints

POST /domain.com/calculateAge 200 OK

POST /domain.com/getCustomers 200 OK

Typical CRUD Apps

• Create

• Read

• Update

• Delete

Typical CRUD Apps

• Create - POST

• Read - GET

• Update - PUT

• Delete - DELETE

* Don’t take this literally…

200 OKPOST /domain.com/customer

GET /domain.com/customer/1 200 OK

DemoResponse with Json

Verbs

• Client-Side Support

• Hidden Field with POST

• Query Parameter with GET/POST

• X-Http-Method-Override Header

Allowed Verbs

• Not every resource allows every method

• Allows you to restrict what can be done

DemoAuto Options

Creating Resource and Location Headers

200 OKPOST /domain.com/customer

DemoLocation Headers

Errors

500 Internal Server ErrorPOST /domain.com/customer

Status CodesStatus Code

Description200 OK201 Created. New resource202 Accepted. Async Ops301 Moved Permanently. 302 Found. (Used for Redirect)304 Not Modified. Conditional GET400 Bad Request401 Unauthorized403 Forbidden404 Not Found405 Method Not allowed. Control Flow409 Conflict500 Internal Server Error501 Not Implemented503 Service unavailable….

Let’s talk Interfaces

GET /checkvist.com/checklists/440751

GET /checkvist.com/checklists/440751

Resource Representation

URL Extension

http://domain.com/customer/25.json

http://domain.com/news/article1.es

URL Query Parameters

http://domain.com/customer/25?format=json

Accept Headers

Content Negotiation

DemoSingle Entry. Single Representation

DemoManual Content Negotiation

DemoAuto Content Negotiation

Performance and Reliability

Making things reliable

Safe and Idempotent Verbs

Caching Built In

• Max-Age

• Expires

• Conditional GETs (ETags)

Demo of Caching

DemoE-Tags

Let’s talk Program State

HATEOAS

Hypermedia

Hypermedia

POST /domain.com/order

Media Types• XML and Microformats

• Existing Format:s ATOM

• Custom Formats:

Content-Type: application/vnd.company.doman+xml

• JSON:

• HAL

• JSON-DL

State Maintenance

• State is pushed to client

• Server cannot maintain state

• What about Cookies?

Added Benefit of Discoverability

• Known Entities - It’s all a resource

• Known Operations - Constraint on Verbs

• Known Representations - Same Resource

• Hypermedia Navigation - Know next steps

Let’s talk Versioning

Versioning

• URL Versioning

GET /domain.com/api/v1/customer

• Custom Header

X-Version: 2

• Accept Header

Accept: application/vnd.mytype.v2+json

Let’s talk Security

Options

• HTTP

• HTTPS

• Digest

• OAuth

• Federated Security

Let’s talk ReST

Richardson Maturity Model

Level 3:

Hypermedia

Level 2: HTTP Verbs

Level 1: Resources

Level 0:POX

Summary

• HTTP API == Application Interface

• You can have HTTP API and not be ReSTful

• ReSTFul systems over certain benefits when abiding by certain constraints

Recommended Reading

Thank you@hhariri

[email protected]