61
HTTP/1.1 Server: 200 OK nginx Date: Wed, 14 Content-Type: Sep 2014 14:03:33 application/json; GMT charset=utf-8 Connection: Status: 200 keep-alive OK ETag: "ef742caec0c19e2169ffb05e7d200d17" Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT { } "talk": apis – the good, the bad & the ugly, "name” : nikhil bendre, "twitter” : @npbendre, “site”: npbendre.com

APIs - the good, the bad & the ugly

Embed Size (px)

DESCRIPTION

This describes what are the best practices of writing RESTful APIs. What are the different tools to create and test these APIs? What are the common pitfalls and how to avoid them? What are some of the industries best public APIs, etc. This also includes a quick and easy hands-on session to fire and launch some APIs in the cloud using Play! Framework.

Citation preview

Page 1: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT ! { !

} !

"talk": apis – the good, the bad & the ugly, !"name” : nikhil bendre, !"twitter” : @npbendre, !“site”: npbendre.com!

Page 2: APIs - the good, the bad & the ugly

API

Page 3: APIs - the good, the bad & the ugly

REST as a Guide

Page 4: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT !!{ ! } !

GET https://api.example.com/user/8913  

"talk": apis – the good, the bad & the ugly, !"name” : nikhil bendre, !"twitter” : @npbendre, !“site”: npbendre.com!

Page 5: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT !!{ ! } !

GET https://api.example.com/user/1234  

"talk": apis – the good, the bad & the ugly, !"name” : nikhil bendre, !"twitter” : @npbendre, !“site”: npbendre.com!

Page 6: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT !!{ ! } !

GET https://api.example.com/user/1234  

"talk": apis – the good, the bad & the ugly, !"name” : nikhil bendre, !"twitter” : @npbendre, !“site”: npbendre.com!

Page 7: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT !!{ ! } !

GET https://api.example.com/user/1234  

"talk": apis – the good, the bad & the ugly, !"name” : nikhil bendre, !"twitter” : @npbendre, !“site”: npbendre.com!

Page 8: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT !!{ ! } !

GET https://api.example.com/user/1234  

"talk": apis – the good, the bad & the ugly, !"name” : nikhil bendre, !"twitter” : @npbendre, !“site”: npbendre.com!

Page 9: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT !!{ ! } !

GET https://api.example.com/user/1234  

"talk": apis – the good, the bad & the ugly, !"name” : nikhil bendre, !"twitter” : @npbendre, !“site”: npbendre.com!

Page 10: APIs - the good, the bad & the ugly

api.example.com/me

Page 11: APIs - the good, the bad & the ugly

http://maps.company.com/maps/api/ staticmap?center=Brooklyn+Bridge,New!+York,NY&zoom=13&size=600x300&maptype=road map&markers=color:blue%7Clabel:S!%7C40.702147,-74.015794&markers=color:gree n%7Clabel:G%7C40.711614,-74.012318!&markers=color:red%7Ccolor:red%7Clabel:C!%7C40.718217,-73.998284&sensor=false!

Page 12: APIs - the good, the bad & the ugly

FIRST: DEFINE RESOURCES A Good Approach: Structure Your URLs

Page 13: APIs - the good, the bad & the ugly

what if /createUser

/getDirectory

/verifyUser

/updateUser

/createEvent

Page 14: APIs - the good, the bad & the ugly

what if /createUser

/getDirectory

/verifyUser

/updateUser

/createEvent

/verifyEvent

/deleteUser

/updateUser

/updateUserName

…………………….

Page 15: APIs - the good, the bad & the ugly

collection &

instance

/files /files/982

Page 16: APIs - the good, the bad & the ugly

SECOND: USE RESOURCES CRUD, but not really

Page 17: APIs - the good, the bad & the ugly

Partially update a resource

GET

POST

PUT

DELETE

PATCH

Retrieve resource

Create resource

Update a resource

Delete a resource

HEAD Get only the headers

Page 18: APIs - the good, the bad & the ugly

Media Type

Request: Accept header

Response: Content-Type header

Page 19: APIs - the good, the bad & the ugly

Meaningful Response Code

Page 20: APIs - the good, the bad & the ugly
Page 21: APIs - the good, the bad & the ugly

2xx: Success

3xx: Redirection

4xx: Client Error

5xx: Server Error

Page 22: APIs - the good, the bad & the ugly

Bad {“error”: “code 782” }

Good HTTP 1.1 400 {

“error” : “field was missing”, “link” : “apidocs.com/400”,

}  

error response

Page 23: APIs - the good, the bad & the ugly

Versioning

Page 24: APIs - the good, the bad & the ugly

Accept

Content-Type

application/json+foo;application&v1

https://api.example.com/v1

vs

Page 25: APIs - the good, the bad & the ugly

ETAGS ARE COOL. NOBODY USES’EM

Page 26: APIs - the good, the bad & the ugly

HTTP/1.1 !Server: !

200 OK !nginx !

Date:"Wed, 14 Content-Type: !

Sep 2014 14:03:33 !application/json; !

GMT !charset=utf-8 !

Connection: Status: 200 !

keep-alive OK !

ETag: ! "ef742caec0c19e2169ffb05e7d200d17" !Last-Modified: Sat, 13 Sep 2014 10:52:21 GMT ! { !

} !“data”: …!

Server Response

Page 27: APIs - the good, the bad & the ugly

HTTP/1.1 ! 304 Not Modified!Date:"Wed, 14 ! Sep 2014 14:03:33 ! GMT !Last-Modified: Sat, 13 Sep 2014 10:53:21 GMT !

Client Request

curl -i -H "If-None-Match: \"ef742caec0c19e2169ffb05e7d200d17\"" "https://graph.beta.facebook.com/me/adaccounts?access_token=___"!

Page 28: APIs - the good, the bad & the ugly

HYPERMEDIA

Page 29: APIs - the good, the bad & the ugly

SUN CLOUD API

GET /machines/1/ Host: example.com Accept: application/xml

Sample Request

Page 30: APIs - the good, the bad & the ugly

HTTP/1.1 200 OK Content-Type: application/xml <status>stopped</status> <link rel="start" method="post" href="machines/2?op=start" />

Sample Response

Page 31: APIs - the good, the bad & the ugly

Security

Page 32: APIs - the good, the bad & the ugly

Photo by Tojosan - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/28069288@N00   Created with Haiku Deck  

Page 33: APIs - the good, the bad & the ugly
Page 34: APIs - the good, the bad & the ugly
Page 35: APIs - the good, the bad & the ugly
Page 36: APIs - the good, the bad & the ugly

Models

Proprietary Solution Basic Authentication

OAuth 1.0 OAuth 2.0

Page 37: APIs - the good, the bad & the ugly

Make Docs Obvious

Page 38: APIs - the good, the bad & the ugly
Page 39: APIs - the good, the bad & the ugly

Supported Options

Examples

Required & Optional Attributes

Default Values

Error Codes

Page 40: APIs - the good, the bad & the ugly

Automate

Page 41: APIs - the good, the bad & the ugly
Page 42: APIs - the good, the bad & the ugly
Page 43: APIs - the good, the bad & the ugly
Page 44: APIs - the good, the bad & the ugly

Build something

meaningful

with your

APIs

Page 45: APIs - the good, the bad & the ugly
Page 46: APIs - the good, the bad & the ugly
Page 47: APIs - the good, the bad & the ugly

RATE LIMIT

Page 48: APIs - the good, the bad & the ugly

Photo by GirlieMac - Creative Commons Attribution License https://www.flickr.com/photos/70561195@N00   Created with Haiku Deck  

Page 49: APIs - the good, the bad & the ugly

CHATTY API

Page 50: APIs - the good, the bad & the ugly

Let’s

Play!

Page 51: APIs - the good, the bad & the ugly

Download Play 2.3.4

Page 52: APIs - the good, the bad & the ugly

Compile, Run & Test

activator new

select template & name

activator run

Page 53: APIs - the good, the bad & the ugly
Page 54: APIs - the good, the bad & the ugly

Controllers GET / controllers.Application.index() GET /users ...getUsers() GET /users/:id ...getUserById(id: String) …

Page 55: APIs - the good, the bad & the ugly

Models

Ebeans for Java

JPA Annotations

Versioned Evolution Scripts

Page 56: APIs - the good, the bad & the ugly

Test

Based on JUnit

Mockito

Integration & UI Test

Page 57: APIs - the good, the bad & the ugly

Pros

Easy Url routing

Class Reloading

Java&Scala support

Java/Ebean support

NIO Server

Page 58: APIs - the good, the bad & the ugly

TEST & PERFORMANCE

Page 59: APIs - the good, the bad & the ugly

RUNSCOPE

Page 60: APIs - the good, the bad & the ugly

Thanks

Page 61: APIs - the good, the bad & the ugly

Attributions •  Cat by Marco Petrucci from The Noun Project •  Chat by Alberto Guerra Quintanilla from The Noun Project •  Coffee by Monika Ciapala from The Noun Project •  Key by Simple Icons from The Noun Project •  Lock by Jardson A. from The Noun Project •  Magnifying Glass by Naomi Atkinson from The Noun Project •  Gauge by Olivier Guin from The Noun Project •  Lock by Andrew Forrester from The Noun Project •  Fork by Dmitry Baranovskiy from The Noun Project •  Click icon is open source •  Document icon by Joe Richardson from The Noun Project •  Robot icon by Jon Trillana •  Tasks list by Arthur Shlain from The Noun Project •  Graph by David Waschbüsch from The Noun Project •  HTTP cats photo from girliemac •  Photo by Tojosan