RESTful Web API and MongoDB go for a pic nic

54
RESTful Web APIs and MongoDB go for a picnic Nicola Iarocci @nicolaiarocci

description

Why and how MongoDB is a perfect match for building your next RESTful Web API. Presented at: NoSQL Day 2013 (Udine, Italy), MongoTorino 2013 (Turin, Italy), A Morning With MongoDB (Milan).

Transcript of RESTful Web API and MongoDB go for a pic nic

Page 1: RESTful Web API and MongoDB go for a pic nic

RESTful Web APIsand MongoDB go for a picnic

Nicola Iarocci @nicolaiarocci

Page 2: RESTful Web API and MongoDB go for a pic nic

Nicola IarocciEve REST API Framework, Cerberus, Events

Co-founder @ gestionaleamica.com, Book Author,

MongoDB Master

Page 3: RESTful Web API and MongoDB go for a pic nic

Nicola IarocciTL;DR

Passionate full stack developer

Page 4: RESTful Web API and MongoDB go for a pic nic

Il Piccolo Libro di MongoDB

edizione italiana del libro di Karl Seguin disponibile per il download @ nicolaiarocci.com

Page 5: RESTful Web API and MongoDB go for a pic nic

gestionaleamica.cominvoicing & accounting

Page 6: RESTful Web API and MongoDB go for a pic nic

your typical old school desktop application

gestionaleamica.com

Page 7: RESTful Web API and MongoDB go for a pic nic
Page 8: RESTful Web API and MongoDB go for a pic nic

gestionaleamica.comnow going web and mobile

Page 9: RESTful Web API and MongoDB go for a pic nic

gestionaleamica.com“ we need a remote API to keep everything in sync ”

A-ha moment

Page 10: RESTful Web API and MongoDB go for a pic nic

Before

Client

LAN/SQL

Database

Desktop!Application

Page 11: RESTful Web API and MongoDB go for a pic nic

Initial draft

Clients

“Cloud”

Database

RESTful !Web API

API

iOS

Android

Website

Desktop Client

? ?

Page 12: RESTful Web API and MongoDB go for a pic nic

Constraints

• minimum viable product first

• add features over time

• frequent database schema updates

• avoid downtime as much as possible

Page 13: RESTful Web API and MongoDB go for a pic nic

So we started exploring new paths

Page 14: RESTful Web API and MongoDB go for a pic nic

MongoDB and REST!

or why we picked MongoDB for our REST API

Page 15: RESTful Web API and MongoDB go for a pic nic

true selling point for me

JSON-style data store

Page 16: RESTful Web API and MongoDB go for a pic nic

all client to API communication is going to be JSON

JSON for transport

Page 17: RESTful Web API and MongoDB go for a pic nic

JSON & RESTful API

JSON!accepted media type

Client

JSON!(BSON)

Mongo

GET

maybe we can push directly to client?

Page 18: RESTful Web API and MongoDB go for a pic nic

JSON & RESTful API

JSON!accepted media type

Client

JSON!(BSON)

Mongo

JSON!subset of python dict!

(kinda)

API

GET

almost.

Page 19: RESTful Web API and MongoDB go for a pic nic

JSON & RESTful API

JSON!objects

Client

JSON!(BSON)

Mongo

JSON/dict!maps to python dict!

(validation layer)

API

POST

also works when posting (adding) items to the database

Page 20: RESTful Web API and MongoDB go for a pic nic

made NoSQL easy to grasp (even for a dumbhead like me)

Similarity with RDBMS

Page 21: RESTful Web API and MongoDB go for a pic nic

Terminology

RDBMS Mongo

Database Database

Table Collection

Rows(s) JSON Document

Index Index

Join Embedding & Linking

Page 22: RESTful Web API and MongoDB go for a pic nic

Queries in MongoDB are represented as JSON-style objects

What about Queries?

// select * from things where x=3 and y="foo" db.things.find({x: 3, y: "foo”});

Page 23: RESTful Web API and MongoDB go for a pic nic

Filtering and Sorting

native!Mongo!query syntax

Client

JSON!(BSON)

Mongo

(very) thin parsing!

& validation layer

API

let’s simply expose MongoDB syntax

?where={x: 3, y: "foo”}

Page 24: RESTful Web API and MongoDB go for a pic nic

mapping to and from the database feels more natural

JSON all along the pipeline

Page 25: RESTful Web API and MongoDB go for a pic nic

Where we’re going we don’t need ORMs.

ORM

Page 26: RESTful Web API and MongoDB go for a pic nic

dynamic documents allow for painless, progressive evolution

Schema-less

Page 27: RESTful Web API and MongoDB go for a pic nic

MongoDB drivers are beautiful. Really.

PyMongo

Page 28: RESTful Web API and MongoDB go for a pic nic

Also in MongoDB

• setup is a breeze

• lightweight

• fast inserts, updates and queries

• excellent documentation

• great support by 10gen

• great community

Page 29: RESTful Web API and MongoDB go for a pic nic

REST in practice!

with some MongoDB love

Page 30: RESTful Web API and MongoDB go for a pic nic

CollectionsAPI entry point + plural nounsapi.example.com/contacts

Maps to a Mongo collection

Page 31: RESTful Web API and MongoDB go for a pic nic

DocumentAPI entry point + plural nouns + ID

api.example.com/contacts/4f46445fc88e201858000000

Maps to a collection ObjectID

Page 32: RESTful Web API and MongoDB go for a pic nic

Retrieving Resoruce Data

GET

Page 33: RESTful Web API and MongoDB go for a pic nic

def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents

Resource GET

find() accepts a python dict as query expression, and returns a cursor we can

iterate

/contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}

Page 34: RESTful Web API and MongoDB go for a pic nic

def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents

Resource GET

find() accepts a python dict as query expression, and returns a cursor we can

iterate

/contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}

Page 35: RESTful Web API and MongoDB go for a pic nic

def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents

Resource GET

find() accepts a python dict as query expression, and returns a cursor we can

iterate

/contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}

Page 36: RESTful Web API and MongoDB go for a pic nic

JSON Rendering

Page 37: RESTful Web API and MongoDB go for a pic nic

JSON Rendering

straight from Mongo

Page 38: RESTful Web API and MongoDB go for a pic nic

JSON Rendering

Page 39: RESTful Web API and MongoDB go for a pic nic

Editing a Document

PATCH

Page 40: RESTful Web API and MongoDB go for a pic nic

PATCHing

mongo update() method commits updates to the

database.

def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})!

Page 41: RESTful Web API and MongoDB go for a pic nic

PATCHing

udpate() takes the unique Id of the document to update

def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})!

Page 42: RESTful Web API and MongoDB go for a pic nic

PATCHingdef patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, !

{"$set": updates})!

$set accepts a dict!with the updates for the db

eg: {“active”: False}.

updates are atomic

Page 43: RESTful Web API and MongoDB go for a pic nic

Creating Resources

POST

Page 44: RESTful Web API and MongoDB go for a pic nic

def post(collection):! (...)! for key, item in docs.items():! response[ID_FIELD] = db(collection).insert(item)

POSTing Take #1

push document and get its ObjectId back from Mongo.

like other CRUD operations, inserting is trivial in

mongo.

Page 45: RESTful Web API and MongoDB go for a pic nic

POSTing Take #2

Bulk inserts!!(let’s look at the code)

Page 46: RESTful Web API and MongoDB go for a pic nic

after a lot of tinkeringwe released an ambitious open source project

Page 47: RESTful Web API and MongoDB go for a pic nic

Eve REST API Framework powered by

Flask, MongoDB and good intentions

python-eve.org

Page 48: RESTful Web API and MongoDB go for a pic nic

Beta 0.2

• 1.000+ stargazers

• 120 forks

• 24 contributors

• 7.935 downloads

Page 49: RESTful Web API and MongoDB go for a pic nic

Eve Extensions contributed by the community

• Eve-ElasticSearch

• Eve-SQLAlchemy

• Eve-Docs

• Eve-Mocks

Page 50: RESTful Web API and MongoDB go for a pic nic

Wanna see it running?Hopefully it won’t explode right into my face

Page 51: RESTful Web API and MongoDB go for a pic nic

Initial draft

Clients

“Cloud”

Database

RESTful !Web API

API

iOS

Android

Website

Desktop Client

? ?

Page 52: RESTful Web API and MongoDB go for a pic nic

Clients

MongoDB

Database

Adam!eve instance

API

iOS

Android

Website

Desktop Client

Production

Page 53: RESTful Web API and MongoDB go for a pic nic

MongoDB Rocks!your RESTful Web API

Page 54: RESTful Web API and MongoDB go for a pic nic

Thank you.nicolaiarocci