36
Building RESTful APIs Vancouver Python Day November 16, 2013 Ganesh Swami www.silota.com

Building RESTful APIs

Embed Size (px)

DESCRIPTION

Fundamentals of building a Restful API with Django and django-rest-framework. Intended for new developers interested in developing a REST API for their applications. Basic knowledge of Python is nice to have, but the concepts are transferable. Presented at Vancouver Python Day 2013.

Citation preview

Page 1: Building RESTful APIs

Building RESTful APIsVancouver Python Day

November 16, 2013

Ganesh Swamiwww.silota.com

Page 2: Building RESTful APIs

Hi

• Programming professionally for 10+ years

• x86 assembly, STL, boost, python-boost, python

!

• Built emacs-­‐wiki-­‐blog: first blogging engine for Emacs!

Page 3: Building RESTful APIs

SILOTA• Search As A Service

• full stack: crawling, indexing, retrieving, tag deployment

• Python shop:

• pelican  

• ansible  

• sentry  

• django  

• django-­‐rest-­‐framework  

• In beta testing: love more feedback!

Page 4: Building RESTful APIs

APIs: What & Why

Page 5: Building RESTful APIs

What is an API?Application Programming Interface

!An API is the interface implemented by an

application which allows other applications to communicate with it.

Page 6: Building RESTful APIs

What is an API?

communicate

Page 7: Building RESTful APIs

What is REST?• REpresentational State Transfer

• logical resources manipulated with HTTP verbs

• modern best practice

• wide adoption

• contrast with SOAP

Page 8: Building RESTful APIs

Why build an API?

• explosion of devices connected to the internet

• can be a company’s greatest asset

• bizdev 2.0: internal developers, consultants, partners, customers

Page 9: Building RESTful APIs

Sample APIs• aws

• dropbox

• instagram

• pinterest

• github

• stripe

• salesforce

• parse

• …

Page 10: Building RESTful APIs

Source: Mary Meeker’s Internet Trends 2013

Page 11: Building RESTful APIs

APIs: How

Page 12: Building RESTful APIs

Top 3 qualities• Intuitive

• no surprises, easy to learn

• Documented

• simple answers to simple questions

• references, tutorials & quick starts

• Opinionated

• camelCase, ids, responses, pagination, etc.

Page 13: Building RESTful APIs
Page 14: Building RESTful APIs

Resources, Status Codes &

Errors

Page 15: Building RESTful APIs

Resources

• Nouns, not verbs

• Coarse grained, not fine grained

• example: let’s build a document datastore!

Page 16: Building RESTful APIs

Smells like RPC• /getDocument  

• /getAllDocuments  

• /createDocument  

• /updateDocument  

• /deleteDocument

Page 17: Building RESTful APIs

Smells like RPC• /getDocument  

• /getAllDocuments  

• /createDocument  

• /updateDocument  

• /deleteDocument

This is a bad example. !Don’t do this!

Page 18: Building RESTful APIs

Embrace HTTP

• GET,  POST,  PUT,  PATCH,  DELETE  

!

• Explorable with simple tools

Page 19: Building RESTful APIs

Embrace HTTPGET  /document Retrieve all documents

GET  /document/19 Retrieve a specific document #19

POST  /document Create a new document

PUT  /document/19 Update an existing document #19

DELETE  /document/19 Delete an existing document #19

Page 20: Building RESTful APIs

Bipartite graph/documents /documents/:id …

GET

POST error

PUT error

PATCH error

DELETE

Page 21: Building RESTful APIs

Status Codes

2xx OK, created, all good, carry on

4xx User error: bad API key, malformed data, item not found, etc.

5xx Server error

Page 22: Building RESTful APIs

Errors

• Errors

• as descriptive as possible

• developers are your customers

• never naked 4xx/5xx HTTP errors

Page 23: Building RESTful APIs

Errors

<xml  version="1.0"?>  <Error>          <Message>A  server  error  has  occurred</Message>          <Description>Unknown  Error</Description>          <Id>1234</Id>  </Error>

Just no.

Page 24: Building RESTful APIs

Errors

{      "code"  :  1234,      "message"  :  "Unsupported  media  type  ‘text/html’  in  request",      "description"  :  "Requests  need  to  have  the  Content-­‐Type  HTTP  header  set  to  ‘application/json’"  }

Page 25: Building RESTful APIs

pip-install httpie

Page 26: Building RESTful APIs

Best practicessecurity

base URLs

serialization

timestamps

versioning

caching

gzip

logging

Page 27: Building RESTful APIs

Best practicessecurity https all the way

base URLs api.companyname.com

serialization json

timestamps ISO 8601 & UTC

versioning /v1/

caching ETag & Last-Modified

gzip always & pretty print responses

logging if possible

Page 28: Building RESTful APIs

Recap

• https + gzip + json

• draw bipartite graph of nouns and verbs

• great documentation

• no surprises

Page 29: Building RESTful APIs

django-­‐rest-­‐framework

Page 30: Building RESTful APIs

Why use a framework?

Page 31: Building RESTful APIs

Myths

• roll your own

• use a ‘lightweight’ framework

• too tied to django

• too slow

Page 32: Building RESTful APIs

Features• pagination

• permission

• authentication

• serialization

• throttling

• data validation

• proper HTTP response handling

Page 33: Building RESTful APIs

Magic formula: MixinsViews Authentication Permissions Throttling

CreateAPIView Token Any SimpleRate

ListAPIView Session Token AnonRate

RetrieveAPIView OAuth Authentication

DeleteAPIView

Page 34: Building RESTful APIs

Four step formula

1. create the model

2. write the serializer

3. write the view

4. configure the urls

Page 35: Building RESTful APIs

References• How to Design a Good API and Why it Matters:

• http://lcsd05.cs.tamu.edu/slides/keynote.pdf

• Best Practices for Designing a Pragmatic RESTful API

• http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

• REST worst practices:

• http://jacobian.org/writing/rest-worst-practices/

• http://django-rest-framework.org/

Page 36: Building RESTful APIs

Keep in touch!

Ganesh Swami!

www.silota.com

[email protected]

@gane5h