Restful Web Apis: and Mongodb Go For A Picnic
Restful Web Apis: and Mongodb Go For A Picnic
e nt
A -ha mo m
Before
Client Database
Desktop!
Application LAN/SQL
Initial draft
Android
RESTful !
“Cloud”
Website Web API
Desktop Client
? ?
Constraints
Client Mongo
JSON! JSON!
accepted media type (BSON)
almost.
JSON & RESTful API
POST
Table Collection
Index Index
?where={x: 3, y: "foo”}
• setup is a breeze
• lightweight
• fast inserts, updates and queries
• excellent documentation
• great support by 10gen
• great community
REST in practice
!
with some MongoDB love
Collections
API entry point + plural nouns
api.example.com/contacts
M o n go c ol l e c ti o n
Maps t o a
Document
API entry point + plural nouns + ID
api.example.com/contacts/4f46445fc88e201858000000
l e c t io n O b j ec t I D
Maps t o a c o l
GET
Retrieving Resoruce Data
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
def get_collection(collection):!
documents = []!
cursor = db(collection).find(where, projection)!
for document in cursor:!
documents.append(document)!
return documents
def get_collection(collection):!
documents = []!
cursor = db(collection).find(where, projection)!
for document in cursor:!
documents.append(document)!
return documents
f r o m Mo n go
straight
JSON Rendering
PATCH
Editing a Document
PATCHing
def patch_document(collection, original):!
(...)!
# Perform the update!
db(collection).update({"_Id": ObjectId(object_id)}, !
{"$set": updates})!
def post(collection):!
(...)!
for key, item in docs.items():!
response[ID_FIELD] = db(collection).insert(item)
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
• Eve-ElasticSearch
• Eve-SQLAlchemy
• Eve-Docs
• Eve-Mocks
Wanna see it running?
Hopefully it won’t explode right into my face
Initial draft
Android
RESTful !
“Cloud”
Website Web API
Desktop Client
? ?
Production
Android
Adam! MongoDB
Website eve instance
Desktop Client
MongoDB Rocks!
your RESTful Web API
Thank you.
nicolaiarocci