Slide 1

Slide 1 text

Developing web REST API { "name": "Shalva Usubov", "contact": "@usubov" }

Slide 2

Slide 2 text

Application Programming Interface ● Want to scale integration with customers and partners ● Need a mobile app ● Migrate on Service-oriented architecture ● ...

Slide 3

Slide 3 text

● HTTP (methods, status, headers) ● REST ● Representation ● Versioning ● Security ● Performance ● Caching

Slide 4

Slide 4 text

HTTP HyperText Transfer Protocol ● Methods ● Response status codes ● Headers

Slide 5

Slide 5 text

HTTP methods GET fetch a resource representation POST create a resource PUT update a resource PATCH partially update a resource DELETE remove a resource ...

Slide 6

Slide 6 text

HTTP status codes 200 OK 304 Not Modified 400 Bad Request 401 Unauthorized 403 Forbidden 404 Not Found 500 Internal Server Error 502 Bad Gateway

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

HTTP request and response headers Content-Type Content-Language Content-Length Content-Encoding Last-Modified Cache-Control Location ETag Vary Expires If-Modified-Since If-None-Match

Slide 9

Slide 9 text

Security Prefer HTTPS ● OAuth - most flexible ● Basic HTTP Authentication ● Custom

Slide 10

Slide 10 text

REST Representational State Transfer Uses URLs to identify resources HTTP verbs indicate the action to perform

Slide 11

Slide 11 text

REST Method (verbs) URL Description GET /gists Returns all gists POST /gists Create new gist GET /gists/1 Return given gist PATCH /gists/1 Update given gist DELETE /gists/1 Delete given gist

Slide 12

Slide 12 text

REST Example GET https://api.github.com/gists POST https://api.github.com/gists GET https://api.github.com/gists/6412448 PATCH https://api.github.com/gists/6412448 DELETE https://api.github.com/gists/6412448

Slide 13

Slide 13 text

REST hierarchical association GET https://api.github.com/gists/6412448/commits POST https://api.github.com/gists/6412448/commits GET https://api.github.com/gists/6412448/commits/1 PATCH https://api.github.com/gists/6412448/commits/1 DELETE https://api.github.com/gists/6412448/commits/1

Slide 14

Slide 14 text

REST filters, search and pagination GET https://api.github.com/gists?status=regular GET https://api.github.com/gists?public=true GET https://api.github.com/gists?q=something GET https://api.github.com/gists?page=2&per_page=20

Slide 15

Slide 15 text

Representation XML, JSON, etc...

Slide 16

Slide 16 text

XML eXtensible Markup Language ● Verbose ● Includes data type information ● Powerful/Complicated

Slide 17

Slide 17 text

XML orford_castle_taster hello! wooyay hoopla

Slide 18

Slide 18 text

JSON JavaScript Object Notation ● Widely support in programming languages ● Human readable ● No data type information

Slide 19

Slide 19 text

JSON { "url": "https://api.github.com/gists/6412448", "commits_url": "https://api.github.com/gists/6412448/commits", "id": "6412448", "public": true, "owner": { "login": "shaliko", "id": 36139, "avatar_url": "https://avatars.githubusercontent.com/u/36139?v=2", "url": "https://api.github.com/users/shaliko", } }

Slide 20

Slide 20 text

Content Negotiation Client says what formats it can handle, and the server works out what is best Accept: application/json;q=1.0, application/xml;q=0.6

Slide 21

Slide 21 text

Versioning Backward compatibility, maintaining multiple versions Accept: application/vnd.github.v3+json https://graph.facebook.com/v1/posts https://graph.facebook.com/v2.1/posts

Slide 22

Slide 22 text

HTTP caching Expires for statics content # Response Expires: Sun, 09 Aug 2015 10:56:14 GMT Cache-Control: max-age=36000,public

Slide 23

Slide 23 text

HTTP caching Conditional policy for dynamic content # Response ETag: "f6373f0fd7ccb539c6ec8f5991dddc30" Last-Modified: Wed, 08 Oct 2014 06:32:07 GMT

Slide 24

Slide 24 text

HTTP caching $ curl -I https://api.github.com/gists/6412448 HTTP/1.1 200 OK Server: GitHub.com Date: Sat, 11 Oct 2014 02:05:35 GMT Content-Type: application/json; charset=utf-8 Status: 200 OK Cache-Control: public, max-age=60 Last-Modified: Thu, 09 Oct 2014 10:58:09 GMT ETag: "82fc020c8b1e99c9562fed6ba56e8230" Content-Length: 2499 Vary: Accept, Accept-Encoding

Slide 25

Slide 25 text

HTTP caching $ curl -I https://api.github.com/gists/6412448 HTTP/1.1 200 OK Server: GitHub.com Date: Sat, 11 Oct 2014 02:05:35 GMT Content-Type: application/json; charset=utf-8 Status: 200 OK Cache-Control: public, max-age=60 Last-Modified: Thu, 09 Oct 2014 10:58:09 GMT ETag: "82fc020c8b1e99c9562fed6ba56e8230" Content-Length: 2499 Vary: Accept, Accept-Encoding

Slide 26

Slide 26 text

HTTP caching $ curl -I https://api.github.com/gists/6412448 -H 'If-None-Match:" 82fc020c8b1e99c9562fed6ba56e8230"' HTTP/1.1 304 Not Modified Server: GitHub.com Date: Sat, 11 Oct 2014 02:14:37 GMT Status: 304 Not Modified Cache-Control: public, max-age=60 Last-Modified: Thu, 09 Oct 2014 10:58:09 GMT ETag: "82fc020c8b1e99c9562fed6ba56e8230" Vary: Accept, Accept-Encoding

Slide 27

Slide 27 text

HTTP caching $ curl -I https://api.github.com/gists/6412448 -H 'If-None-Match:" 82fc020c8b1e99c9562fed6ba56e8230"' HTTP/1.1 304 Not Modified Server: GitHub.com Date: Sat, 11 Oct 2014 02:14:37 GMT Status: 304 Not Modified Cache-Control: public, max-age=60 Last-Modified: Thu, 09 Oct 2014 10:58:09 GMT ETag: "82fc020c8b1e99c9562fed6ba56e8230" Vary: Accept, Accept-Encoding Cut response time

Slide 28

Slide 28 text

HTTP caching $ curl -I https://api.github.com/gists/6412448 -H "If-Modified-Since: Thu, 09 Oct 2014 10:58:09 GMT" HTTP/1.1 304 Not Modified Server: GitHub.com Date: Sat, 11 Oct 2014 02:14:37 GMT Status: 304 Not Modified Cache-Control: public, max-age=60 Last-Modified: Thu, 09 Oct 2014 10:58:09 GMT ETag: "82fc020c8b1e99c9562fed6ba56e8230" Vary: Accept, Accept-Encoding

Slide 29

Slide 29 text

HTTP caching Last project had over ~75% requests cache hit

Slide 30

Slide 30 text

Error handling Code for code, message for people HTTP/1.1 400 Bad Request { "code": 34, "message": "Missing required field", "url": "https://developers.example.com/errors/34" }

Slide 31

Slide 31 text

Performance ● Cache on client and server sides ● HTTP compression ● Delay async tasks ● SPDY/HTTP 2.0 - N+1 over HTTP is expensive

Slide 32

Slide 32 text

Start from ● RESTful Web Services Cookbook By: Subbu Allamaraju ● Web API Design by Brian Mulloy (apigee) ● http://jsonapi.org ● GitHub API https://developer.github.com/v3/

Slide 33

Slide 33 text

Thanks!