RESTful Web APIs
and MongoDB go for a picnic

Nicola Iarocci

@nicolaiarocci
Nicola Iarocci
Eve REST API Framework, Cerberus, Events
Co-founder @ gestionaleamica.com,
Book Author,
MongoDB Master
Nicola Iarocci
TL;DR
Passionate full stack developer
Il Piccolo
Libro di MongoDB
edizione italiana del libro di Karl Seguin
disponibile per il download @ nicolaiarocci.com
gestionaleamica.com
invoicing & accounting
gestionaleamica.com
your typical old school desktop application
gestionaleamica.com
now going web and mobile
gestionaleamica.com
“ we need a remote

API to keep everything in sync ”

-ha
A

ent
mo m
Before
Client

Database

Desktop!
Application

LAN/SQL
Initial draft
Clients

API

Database

RESTful !
Web API

“Cloud”

?

?

iOS
Android
Website
Desktop Client
Constraints
• minimum viable product first
• add features over time
• frequent database schema updates
• avoid downtime as...
So we started
exploring new paths
MongoDB and REST
!

or why we picked MongoDB for our REST API
JSON-style data store
true selling point for me
JSON for transport
all client to API communication is going to be JSON
JSON & RESTful API
GET

Client

Mongo

JSON!

JSON!

accepted media type

(BSON)

maybe we can push directly to client?
JSON & RESTful API
GET

Client

API

JSON!

JSON!

accepted media type

subset of python dict!
(kinda)

almost.

Mongo
JSO...
JSON & RESTful API
POST

Client
JSON!
objects

API
JSON/dict!
maps to python dict!
(validation layer)

Mongo
JSON!
(BSON)
...
Similarity with
RDBMS
made NoSQL easy to grasp (even for a dumbhead like me)
Terminology
RDBMS

Mongo

Database

Database

Table

Collection

Rows(s)

JSON Document

Index

Index

Join

Embedding & L...
What about Queries?
Queries in MongoDB are represented as JSON-style objects
// select * from things where x=3 and y="foo"...
Filtering and Sorting
let’s simply expose MongoDB syntax
?where={x: 3, y: "foo”}

Client
native!
Mongo!
query syntax

API
...
JSON
all along the pipeline
mapping to and from the database feels more natural
ORM
Where we’re going we don’t need ORMs.
Schema-less
dynamic documents allow for painless, progressive evolution
PyMongo
MongoDB drivers are beautiful. Really.
Also in MongoDB
• setup is a breeze
• lightweight
• fast inserts, updates and queries
• excellent documentation
• great su...
REST in practice
!

with some MongoDB love
Collections
API entry point + plural nouns
api.example.com/contacts

collection
to a Mongo
Maps
Document
API entry point + plural nouns + ID
api.example.com/contacts/4f46445fc88e201858000000

on ObjectID
o a collecti
M...
GET
Retrieving Resoruce Data
Resource GET
/contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}

def get_collection(collection):!
documents =...
Resource GET
/contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}

def get_collection(collection):!
documents =...
Resource GET
/contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1}

def get_collection(collection):!
documents =...
JSON Rendering
JSON Rendering

from Mongo
straight
JSON Rendering
PATCH
Editing a Document
PATCHing
def patch_document(collection, original):!
(...)!
# Perform the update!
db(collection).update({"_Id": ObjectId(ob...
PATCHing
def patch_document(collection, original):!
(...)!
# Perform the update!
db(collection).update({"_Id": ObjectId(ob...
PATCHing
def patch_document(collection, original):!
(...)!
# Perform the update!
db(collection).update({"_Id": ObjectId(ob...
POST
Creating Resources
POSTing
Take #1

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

Bulk inserts!!
(let’s look at the code)
after a lot of tinkering
we released an ambitious open

source project
Eve
REST API Framework powered by
Flask, MongoDB and good intentions

python-eve.org
Beta 0.2

• 1.000+ stargazers
• 120 forks

• 24 contributors
• 7.935 downloads
Eve Extensions
contributed by the community

• Eve-ElasticSearch
• Eve-SQLAlchemy
• Eve-Docs
• Eve-Mocks
Wanna see it running?
Hopefully it won’t explode right into my face
Initial draft
Clients

API

Database

RESTful !
Web API

“Cloud”

?

?

iOS
Android
Website
Desktop Client
Production
Clients

API

Database

Adam!

MongoDB

iOS
Android
Website
Desktop Client

eve instance
MongoDB Rocks!
your RESTful Web API
Thank you.
nicolaiarocci
RESTful Web API and MongoDB go for a pic nic
Upcoming SlideShare
Loading in...5
×

RESTful Web API and MongoDB go for a pic nic

11,391

Published on

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).

Published in: Technology, Education

RESTful Web API and MongoDB go for a pic nic

  1. 1. RESTful Web APIs and MongoDB go for a picnic Nicola Iarocci @nicolaiarocci
  2. 2. Nicola Iarocci Eve REST API Framework, Cerberus, Events Co-founder @ gestionaleamica.com, Book Author, MongoDB Master
  3. 3. Nicola Iarocci TL;DR Passionate full stack developer
  4. 4. Il Piccolo Libro di MongoDB edizione italiana del libro di Karl Seguin disponibile per il download @ nicolaiarocci.com
  5. 5. gestionaleamica.com invoicing & accounting
  6. 6. gestionaleamica.com your typical old school desktop application
  7. 7. gestionaleamica.com now going web and mobile
  8. 8. gestionaleamica.com “ we need a remote API to keep everything in sync ” -ha A ent mo m
  9. 9. Before Client Database Desktop! Application LAN/SQL
  10. 10. Initial draft Clients API Database RESTful ! Web API “Cloud” ? ? iOS Android Website Desktop Client
  11. 11. Constraints • minimum viable product first • add features over time • frequent database schema updates • avoid downtime as much as possible
  12. 12. So we started exploring new paths
  13. 13. MongoDB and REST ! or why we picked MongoDB for our REST API
  14. 14. JSON-style data store true selling point for me
  15. 15. JSON for transport all client to API communication is going to be JSON
  16. 16. JSON & RESTful API GET Client Mongo JSON! JSON! accepted media type (BSON) maybe we can push directly to client?
  17. 17. JSON & RESTful API GET Client API JSON! JSON! accepted media type subset of python dict! (kinda) almost. Mongo JSON! (BSON)
  18. 18. JSON & RESTful API POST Client JSON! objects API JSON/dict! maps to python dict! (validation layer) Mongo JSON! (BSON) also works when posting (adding) items to the database
  19. 19. Similarity with RDBMS made NoSQL easy to grasp (even for a dumbhead like me)
  20. 20. Terminology RDBMS Mongo Database Database Table Collection Rows(s) JSON Document Index Index Join Embedding & Linking
  21. 21. What about Queries? Queries in MongoDB are represented as JSON-style objects // select * from things where x=3 and y="foo" db.things.find({x: 3, y: "foo”});
  22. 22. Filtering and Sorting let’s simply expose MongoDB syntax ?where={x: 3, y: "foo”} Client native! Mongo! query syntax API (very) thin parsing! & validation layer Mongo JSON! (BSON)
  23. 23. JSON all along the pipeline mapping to and from the database feels more natural
  24. 24. ORM Where we’re going we don’t need ORMs.
  25. 25. Schema-less dynamic documents allow for painless, progressive evolution
  26. 26. PyMongo MongoDB drivers are beautiful. Really.
  27. 27. Also in MongoDB • setup is a breeze • lightweight • fast inserts, updates and queries • excellent documentation • great support by 10gen • great community
  28. 28. REST in practice ! with some MongoDB love
  29. 29. Collections API entry point + plural nouns api.example.com/contacts collection to a Mongo Maps
  30. 30. Document API entry point + plural nouns + ID api.example.com/contacts/4f46445fc88e201858000000 on ObjectID o a collecti Maps t
  31. 31. GET Retrieving Resoruce Data
  32. 32. Resource GET /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1} def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents find() accepts a python dict as query expression, and returns a cursor we can iterate
  33. 33. Resource GET /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1} def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents find() accepts a python dict as query expression, and returns a cursor we can iterate
  34. 34. Resource GET /contacts?where={“age”: {“$gt”: 20}}&projection={“lastname”: 1} def get_collection(collection):! documents = []! cursor = db(collection).find(where, projection)! for document in cursor:! documents.append(document)! return documents find() accepts a python dict as query expression, and returns a cursor we can iterate
  35. 35. JSON Rendering
  36. 36. JSON Rendering from Mongo straight
  37. 37. JSON Rendering
  38. 38. PATCH Editing a Document
  39. 39. PATCHing def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})! mongo update() method commits updates to the database.
  40. 40. PATCHing def patch_document(collection, original):! (...)! # Perform the update! db(collection).update({"_Id": ObjectId(object_id)}, ! {"$set": updates})! udpate() takes the unique Id of the document to update
  41. 41. PATCHing def 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
  42. 42. POST Creating Resources
  43. 43. POSTing Take #1 def post(collection):! (...)! for key, item in docs.items():! response[ID_FIELD] = db(collection).insert(item) push document and get its ObjectId back from Mongo. like other CRUD operations, inserting is trivial in mongo.
  44. 44. POSTing Take #2 Bulk inserts!! (let’s look at the code)
  45. 45. after a lot of tinkering we released an ambitious open source project
  46. 46. Eve REST API Framework powered by Flask, MongoDB and good intentions python-eve.org
  47. 47. Beta 0.2 • 1.000+ stargazers • 120 forks • 24 contributors • 7.935 downloads
  48. 48. Eve Extensions contributed by the community • Eve-ElasticSearch • Eve-SQLAlchemy • Eve-Docs • Eve-Mocks
  49. 49. Wanna see it running? Hopefully it won’t explode right into my face
  50. 50. Initial draft Clients API Database RESTful ! Web API “Cloud” ? ? iOS Android Website Desktop Client
  51. 51. Production Clients API Database Adam! MongoDB iOS Android Website Desktop Client eve instance
  52. 52. MongoDB Rocks! your RESTful Web API
  53. 53. Thank you. nicolaiarocci
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×