Download - RESTFul APIs

Transcript
Page 1: RESTFul APIs

RESTful APIs

Maceió Dev Meetup December 17, 2014

Page 2: RESTFul APIs

about.me/endersonmaia

Page 3: RESTFul APIs

What I’m going to talk

Page 4: RESTFul APIs

30 minutes to• REST

• Constraints

• Richard Maturity Model (RMM)

• HTTP Methods

• HTTP Satus Codes

• Resource Naming

• Format

• Linking

• …

Page 5: RESTFul APIs

RESTRepresentational State Transfer

(Roy Fielding, 2000)

Page 6: RESTFul APIs

Constraints

Page 7: RESTFul APIs

Client–server

Page 8: RESTFul APIs

Stateless

Page 9: RESTFul APIs

Cacheable

Page 10: RESTFul APIs

Layered system

Page 11: RESTFul APIs

Uniform interface

Page 12: RESTFul APIs

Uniform interface• Resource-Based

• Manipulation of Resources Through Representations

• Self-descriptive Messages

• Hypermedia as the Engine of Application State (HATEOAS)

Page 13: RESTFul APIs

RMM Richardson

Maturity Model

Page 14: RESTFul APIs

Level 0 The swamp of POX

Page 15: RESTFul APIs

Level 0 The swamp of POX

$ curl -X POST -d \ "<?xml version=“1.0"?> \ <methodCall> \ <methodName>examples.getStateName</methodName> \ <params> \ <param> \ <value><i4>40</i4></value> \ </param> \ </params> \ </methodCall>" http://api.example.com

Page 16: RESTFul APIs

Level 1 Resources

Page 17: RESTFul APIs

Level 1 Resources

• GET - http://host/person?action=add?name=John&age=25

• GET - http://host/city/delete

Page 18: RESTFul APIs

Level 2 HTTP Verbs

Page 19: RESTFul APIs

Level 2 HTTP Verbs

• GET - http://host/users

• PUT - http://host/users

• DELETE - http://host/users

Page 20: RESTFul APIs

Level 3 Hypermedia Controls

Page 21: RESTFul APIs

HTTP Methods

Page 22: RESTFul APIs

GET

Page 23: RESTFul APIs

POST

Page 24: RESTFul APIs

DELETE

Page 25: RESTFul APIs

PUT

Page 26: RESTFul APIs

PATCH

Page 27: RESTFul APIs

OPTIONS

Page 28: RESTFul APIs

– Leonard Richardson, 2007

"OPTIONS is a promising idea that nobody uses."

Page 29: RESTFul APIs

HEAD

Page 30: RESTFul APIs

HTTP Status Codes

Page 31: RESTFul APIs
Page 32: RESTFul APIs

2XX

Page 33: RESTFul APIs

2XX

• 200 - OK

• 201 - Created

• 202 - Accepted

• 204 - No Content

Page 34: RESTFul APIs

3XX

Page 35: RESTFul APIs

3XX

• 301 - Moved Permanently

• 304 - Not Modified

Page 36: RESTFul APIs

4XX

Page 37: RESTFul APIs

4XX• 400 - Bad Request

• 401 - Unauthorized

• 403 - Forbidden

• 404 - Not Found

• 409 - Conflict

Page 38: RESTFul APIs

5XX

Page 39: RESTFul APIs

5XX

• 500 - Internal Server Error

• 501 - Not Implemented

Page 40: RESTFul APIs

HTTP Headers

Page 41: RESTFul APIs

HTTPMethod x Status

Page 42: RESTFul APIs
Page 43: RESTFul APIs
Page 44: RESTFul APIs
Page 45: RESTFul APIs
Page 46: RESTFul APIs

More …

Page 47: RESTFul APIs

Verbs

Page 48: RESTFul APIs

Verbs• POST - to create (INSERT) a new resource

• GET - to recover (SELECT) a specific resource

• PUT/PATCH - to UPDATE a specific resource

• DELETE - to DELETE a specific resource

Page 49: RESTFul APIs

Resource Naming

Page 50: RESTFul APIs

Resource Naming• POST http://example.com/customers

• GET http://example.com/customers/33245

• GET|PUT|DELETE http://example.com/products/66432

• GET http://example.com/customers/33245/orders

• POST http://example.com/customers/33245/orders/8769/lineitems

• GET http://example.com/customers/33245/orders/8769/lineitems/1

Page 51: RESTFul APIs

Resource Naming Anti-Pattern

• GET http://api.example.com/services?op=update_customer&id=12345&format=json

• GET http://api.example.com/update_customer/12345

• GET http://api.example.com/customers/12345/update

• PUT http://api.example.com/customers/12345/update

Page 52: RESTFul APIs

Response

Page 53: RESTFul APIs

Format

Page 54: RESTFul APIs

Format

• XML (Atom, …)

• JSON (JSON-LD, HAL, …)

Page 55: RESTFul APIs
Page 56: RESTFul APIs

Linking

Page 57: RESTFul APIs
Page 58: RESTFul APIs

HATEOAS

Page 59: RESTFul APIs

Hypermedia As The Engine Of Application State

Page 60: RESTFul APIs

HATEOAS

Page 61: RESTFul APIs

HATEOAS

Page 62: RESTFul APIs

Idempotence &

Safe Methods

Page 63: RESTFul APIs

What’s missing ?

Page 64: RESTFul APIs

What’s missing ?• More about HTTP Header

• HTTP Authentication samples

• Using Tokens

• Versioning strategies

• ?

Page 65: RESTFul APIs

References• http://stateless.co/hal_specification.html

• http://jsonapi.org

• http://www.infoq.com/articles/Web-APIs-From-Start-to-Finish

• http://restfulwebapis.com

• http://www.restapitutorial.com

• http://martinfowler.com/articles/richardsonMaturityModel.html

• http://restcookbook.com

• http://www.w3.org/TR/json-ld/

• http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

• http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

• http://brockallen.com/2012/05/14/http-status-codes-for-rest/

Page 66: RESTFul APIs

Thank you!

@endersonmaia