FAST REST APIS DEVELOPMENT                        WITH   MONGODBPablo Enfedaque Vidal@pablitoev56
WHO?•  Pablo Enfedaque   •  Computers Engineer   •  Tech Lead and R&D software engineer at Telefonica Digital   •  Working...
TELEFONICA DIGITAL. WHAT?•  Telefonica   •  Fifth largest telecommunications company in the world   •  Operations in Europ...
TELEFONICA DIGITAL. WHAT?•  Telefonica Digital    •  Web and mobile digital contents and services division    •  Product D...
OUR PROJECTS•  Full product development, with life cycle and several deployments    •  20 people team, 1 year or more•  Pi...
SO…      FAST DEVELOPMENT IS      REALLY   CRUCIAL FOR US
HOW TO SPEED UP OUR DEVELOPMENTS?•  Agile methodologies•  Lean startup•  eXtreme Programming•  Continuous Integration•  …
HOW TO SPEED UP OUR DEVELOPMENTS?              CHOOSE THE      RIGHT TECHNOLOGY                (AT FIRST)
¿ RIGHT TECHNOLOGY ?
THE RIGHT TECHNOLOGY•  Faster development with Dynamic Languages   •  3x   •  4x   •  10x
THE RIGHT TECHNOLOGY   THE SAME CAN BE STATED FOR  •  3x               MONGODB  •  4x  •  10x
THE RIGHT TECHNOLOGY•  Several times faster development with Dynamic Languages•  Several times faster development with Mon...
WHY? HOW?LET’S SEE SOME EXAMPLES
ONLY DYNAMIC LANGUAGES?
JAVA VERSIONpublic int[] getDims() {	          	if (this.dims != null) {	          	                	return this.dims;	   ...
PYTHON VERSIONdef get_dims(self):	    ud = self.counters_coll.find_one({_id: users})[value]	    ad = self.counters_coll.fi...
THE RIGHT TECHNOLOGY
LET’S PLAY TO SPOT THE     DIFFERENCES
EXAMPLE: SPEAKER JSON{    "name": "Pablo Enfedaque",    "company": "Telefonica Digital",    "accepted": true,    "registra...
EXAMPLE: DECODED JSON (PYTHON){    "name": "Pablo Enfedaque",    "company": "Telefonica Digital",    "accepted": True,    ...
EXAMPLE: MONGODB BSON{    "name": "Pablo Enfedaque",    "company": "Telefonica Digital",    "accepted": true,    "registra...
LOOKS PRETTYSTRAIGHT FORWARD,      RIGHT?
SPEAKER CREATIONdecoded_input = json.loads(input_json)	decoded_input[registration_date] =datetime.strptime(decoded_input[r...
SPEAKER RETRIEVALretrieved = dbconn[speakers].find_one({name: Pablo}, {_id: 0})	retrieved[registration_date] =retrieved[re...
IT IS REALLYSTRAIGHT FORWARD!
WHAT IF WE WANT TOCHANGE SPEAKERS DATA?
EXAMPLE: SPEAKER JSON{    "name": "Pablo Enfedaque",    "company": "Telefonica Digital",    "position": "R&D SW Engineer",...
SPEAKER CREATIONdecoded_input = json.loads(input_json)	decoded_input[registration_date] =datetime.strptime(decoded_input[r...
INPUT VALIDATION NEEDED?
SPEAKER VALIDATIONfrom rest_framework import serializers	class SpeakerSerializer(serializers.Serializer):	    name = seria...
SPEAKER CREATIONdecoded_input = json.loads(input_json)	serializer = SpeakerSerializer(decoded_input)	print dbconn[speakers...
DON’T LIKE TO WORK WITHDICTIONARIES / HASHES?
CUSTOMISE ATTRIBUTES ACCESSclass AttrDict(dict):	    def __getattr__(self, name):	        try:	            return super(At...
USE DICTIONARIES AS OBJECTSdecoded_input = json.loads(input_json)	serializer = SpeakerSerializer(decoded_input)	speaker_ob...
USE AN ORM?
NO
OBJECT-RELATIONAL MAPPER NO RELATIONAL  NO ORM         NEEDED
CONCLUSIONS
CONCLUSIONS•    MongoDB + dynamic languages = fastest development speed      •  14 months project with Oracle à 3 months ...
Upcoming SlideShare
Loading in …5
×

Fast REST APIs Development with MongoDB

3,039 views

Published on

In an R&D company fast prototyping is vital to develop new projects or proofs of concept quickly and inexpensively. In this talk we will demonstrate how real fast and agile development can be achieved with MongoDB and dynamic languages, with examples and best practices. All the code shown is already uploaded to a public Git repository - https://github.com/pablito56/py-eshop

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,039
On SlideShare
0
From Embeds
0
Number of Embeds
1,486
Actions
Shares
0
Downloads
38
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Fast REST APIs Development with MongoDB

  1. 1. FAST REST APIS DEVELOPMENT WITH MONGODBPablo Enfedaque Vidal@pablitoev56
  2. 2. WHO?•  Pablo Enfedaque •  Computers Engineer •  Tech Lead and R&D software engineer at Telefonica Digital •  Working with MongoDB for some years.
  3. 3. TELEFONICA DIGITAL. WHAT?•  Telefonica •  Fifth largest telecommunications company in the world •  Operations in Europe (7 countries), the United States and Latin America (15 countries) •  Movistar, O2, Vivo, Terra, Tuenti, Jahjah, Tokbox, everything.me, Open Web Device…
  4. 4. TELEFONICA DIGITAL. WHAT?•  Telefonica Digital •  Web and mobile digital contents and services division •  Product Development and Innovation unit •  Products & services development, research, technology strategy, user experience, deployment & operations… •  Around 70 different on going projects
  5. 5. OUR PROJECTS•  Full product development, with life cycle and several deployments •  20 people team, 1 year or more•  Pilot or small product to be deployed in a certain environment •  6 people team, 6 months•  Seedbed or proof of concept to be run with reduced set of users •  3 people team, 3 months•  Ten Fridays open exploratory project to work on your ideas •  2 people team, 10 days (consecutive Fridays)
  6. 6. SO… FAST DEVELOPMENT IS REALLY CRUCIAL FOR US
  7. 7. HOW TO SPEED UP OUR DEVELOPMENTS?•  Agile methodologies•  Lean startup•  eXtreme Programming•  Continuous Integration•  …
  8. 8. HOW TO SPEED UP OUR DEVELOPMENTS? CHOOSE THE RIGHT TECHNOLOGY (AT FIRST)
  9. 9. ¿ RIGHT TECHNOLOGY ?
  10. 10. THE RIGHT TECHNOLOGY•  Faster development with Dynamic Languages •  3x •  4x •  10x
  11. 11. THE RIGHT TECHNOLOGY THE SAME CAN BE STATED FOR •  3x MONGODB •  4x •  10x
  12. 12. THE RIGHT TECHNOLOGY•  Several times faster development with Dynamic Languages•  Several times faster development with MongoDB AND BOTH TOGETHER IS A WIN WIN
  13. 13. WHY? HOW?LET’S SEE SOME EXAMPLES
  14. 14. ONLY DYNAMIC LANGUAGES?
  15. 15. JAVA VERSIONpublic int[] getDims() { if (this.dims != null) { return this.dims; } BasicDBObject query = new BasicDBObject(); query.put("_id", "ctxt_dimensions"); DBObject setup = setup_coll.findOne(query); BasicDBList dbl = (BasicDBList)setup.get("dims"); this.dims = new int[dbl.size() + 2]; BasicDBObject users_counter_ref = new BasicDBObject("_id", users_coll_name); BasicDBObject apps_counter_ref = new BasicDBObject("_id", apps_coll_name); dims[0] = (Integer)counters_coll.findOne(users_counter_ref).get("value") + 1; dims[1] = (Integer)counters_coll.findOne(apps_counter_ref).get("value") + 1; for (int i=0; i<dbl.size(); i++) { dims[i + 2] = (Integer)dbl.get(i); } return dims; }
  16. 16. PYTHON VERSIONdef get_dims(self): ud = self.counters_coll.find_one({_id: users})[value] ad = self.counters_coll.find_one({_id: applications})[value] res = [ud, ad] res.extend(self.setup_coll.find_one({}, {dims: 1})[dims]) return res IT’S UP TO YOU…
  17. 17. THE RIGHT TECHNOLOGY
  18. 18. LET’S PLAY TO SPOT THE DIFFERENCES
  19. 19. EXAMPLE: SPEAKER JSON{ "name": "Pablo Enfedaque", "company": "Telefonica Digital", "accepted": true, "registration_date": "2012-03-15T14:35:05", "num_talks": 1, ”votes": 4, "email": "pev@td.com"}
  20. 20. EXAMPLE: DECODED JSON (PYTHON){ "name": "Pablo Enfedaque", "company": "Telefonica Digital", "accepted": True, "registration_date": datetime(2012, 3, 15, 14, 35, 5), "num_talks": 1, ”votes": 4, "email": "pev@td.com"}
  21. 21. EXAMPLE: MONGODB BSON{ "name": "Pablo Enfedaque", "company": "Telefonica Digital", "accepted": true, "registration_date": ISODate("2012-03-15T14:35:05Z"), "num_talks": 1, ”votes": 4, "email": "pev@td.com", ”_id": ObjectId("5142d08c5db1362abc2d208b”)}
  22. 22. LOOKS PRETTYSTRAIGHT FORWARD, RIGHT?
  23. 23. SPEAKER CREATIONdecoded_input = json.loads(input_json) decoded_input[registration_date] =datetime.strptime(decoded_input[registration_date], "%Y-%m-%dT%H:%M:%S”) return dbconn[speakers].insert(decoded_input) > ObjectId(5142d2845db1362bb3155322)
  24. 24. SPEAKER RETRIEVALretrieved = dbconn[speakers].find_one({name: Pablo}, {_id: 0}) retrieved[registration_date] =retrieved[registration_date].strftime("%Y-%m-%dT%H:%M:%S") return retrieved
  25. 25. IT IS REALLYSTRAIGHT FORWARD!
  26. 26. WHAT IF WE WANT TOCHANGE SPEAKERS DATA?
  27. 27. EXAMPLE: SPEAKER JSON{ "name": "Pablo Enfedaque", "company": "Telefonica Digital", "position": "R&D SW Engineer", "accepted": true, "registration_date": "2012-03-15T14:35:05", "num_talks": 1, ”votes": 4.3,  WAS AN INTEGER "email": "pev@td.com"}
  28. 28. SPEAKER CREATIONdecoded_input = json.loads(input_json) decoded_input[registration_date] =datetime.strptime(decoded_input[registration_date], "%Y-%m-%dT%H:%M:%S”) return dbconn[speakers].insert(decoded_input) SPEAKER RETRIEVALretrieved = dbconn[speakers].find_one({name: Pablo}, {_id: 0}) retrieved[registration_date] =retrieved[registration_date].strftime("%Y-%m-%dT%H:%M:%S") 0 LINES CHANGEDreturn retrieved
  29. 29. INPUT VALIDATION NEEDED?
  30. 30. SPEAKER VALIDATIONfrom rest_framework import serializers class SpeakerSerializer(serializers.Serializer): name = serializers.CharField(max_length=150) company = serializers.CharField(max_length=150) position = serializers.CharField(required=False) accepted = serializers.BooleanField() registration_date = serializers.DateTimeField() num_talks = serializers.IntegerField() votes = serializers.FloatField() email = serializers.EmailField(max_length=150) def restore_object(self, attrs, instance=None): return attrs
  31. 31. SPEAKER CREATIONdecoded_input = json.loads(input_json) serializer = SpeakerSerializer(decoded_input) print dbconn[speakers].insert(serializer.object) SPEAKER RETRIEVALretrieved = dbconn[speakers].find_one({name: Pablo}) serializer = SpeakerSerializer(retrieved) return serializer.object
  32. 32. DON’T LIKE TO WORK WITHDICTIONARIES / HASHES?
  33. 33. CUSTOMISE ATTRIBUTES ACCESSclass AttrDict(dict): def __getattr__(self, name): try: return super(AttrDict, self).__getitem__(name) except KeyError, e: raise AttributeError(e) def __setattr__(self, name, value): if name in self: super(AttrDict, self).__setitem__(name, value) else: super(AttrDict, self).__setattr__(name, value)
  34. 34. USE DICTIONARIES AS OBJECTSdecoded_input = json.loads(input_json) serializer = SpeakerSerializer(decoded_input) speaker_obj = AttrDict(serializer.object) print speaker_obj.company print speaker_obj[position] > Telefonica Digital R&D SW Engineer
  35. 35. USE AN ORM?
  36. 36. NO
  37. 37. OBJECT-RELATIONAL MAPPER NO RELATIONAL  NO ORM NEEDED
  38. 38. CONCLUSIONS
  39. 39. CONCLUSIONS•  MongoDB + dynamic languages = fastest development speed •  14 months project with Oracle à 3 months project with MongoDB•  REST API best practices •  Use JSON •  Use dictionaries / hashes •  Access dictionaries as objects •  No relational model à no ORM •  No other mappers •  Use decorators to handle AutoReconnect

×