Rest in flask

4,268 views
4,073 views

Published on

My way to build RESTful solution with python and flask

Published in: Technology
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,268
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
78
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Rest in flask

  1. 1. REST in Flask raising
  2. 2.  the
  3. 3.  serviceI am: Yehor NazarkinFollow me: @nimnullEmail me: nimnull@gmail.comSaturday, June 2, 12
  4. 4. RESTin
  5. 5.  pieces • Representative state transfer • Hypertext transfer protocol: ✓ URL endpoint as resource address ✓ Content-Type ✓ Status codes • The Web is RESTSaturday, June 2, 12
  6. 6. RESTin
  7. 7.  pieces Web Front Mobile client API BackendsSaturday, June 2, 12
  8. 8. Why Flask? as
  9. 9.  we
  10. 10.  already
  11. 11.  have
  12. 12.  tones
  13. 13.  of
  14. 14.  stuff • I’m not a framework junkie • I want to customize, not rewrite • Clean codebase (20 python modules) • Aware of django.contrib.admin, django.form, etc. • Lack of ORM functionality ✓ I don’t like to write raw SQL ✓ I need composite keys ✓ I want to optimize for a DB backend (we are on PostgreSQL)Saturday, June 2, 12
  15. 15. Why Flask? as
  16. 16.  we
  17. 17.  already
  18. 18.  have
  19. 19.  tones
  20. 20.  of
  21. 21.  stuff Because we canSaturday, June 2, 12
  22. 22. Validation resource
  23. 23.  tier,
  24. 24.  model
  25. 25.  tier Trafaret http://pypi.python.org/pypi/trafaret import trafaret as t t.Dict({first_name: t.String, last_name: t.String, phone: t.String, t.Key(role_id, optional=True): t.Int }).ignore_extra(*)Saturday, June 2, 12
  26. 26. Validation resource
  27. 27.  tier,
  28. 28.  model
  29. 29.  tier Key subset: t.Dict({first_name: t.String, te.KeysSubset(pass, confirm): cmp_pwds}) def cmp_pwds(value): if value[pass] != value[confirm]: return {confirm: t.DataError(Doesnt match)} return {pass: value[pass]}Saturday, June 2, 12
  30. 30. Validation resource
  31. 31.  tier,
  32. 32.  model
  33. 33.  tier Type check: • Null, Bool, String, Int, Float, List, Dict • Atom, Email, URL, Enum, Callable • Converters, ignorance, excludes, optional keys @guard(a=String, b=Int, c=String) def guarded(a, b, c): return a, b, cSaturday, June 2, 12
  34. 34. Achievements yes,
  35. 35.  we
  36. 36.  did Based on Flask’s MethodView Extended with method-decorators Simple to attach Resource — for generic cases ModelResource — SQLA mapped classesSaturday, June 2, 12
  37. 37. Achievements yes,
  38. 38.  we
  39. 39.  did Inspired by • django-tastypie • flask-restless • rainbows and unicornsSaturday, June 2, 12
  40. 40. Achievements yes,
  41. 41.  we
  42. 42.  did In Practice @api_resource(account, sessions, {id: None}) class SessionResource(Resource): validation = t.Dict({email: t.Email}).allow_extra(*) @api_resource(account, addresses, {id: int}) class AddressResource(ModelResource): validation = t.Dict({city: t.String, street: t.String, type: t.String(regex=(bill|delivery))} ).allow_extra(*) model = Address def get_objects(self): ... def get_object(self): ...Saturday, June 2, 12
  43. 43. Achievements yes,
  44. 44.  we
  45. 45.  did Differences @api_resource(account, profiles, {id: None}) class RoleResource(ModelResource): validation = ... decorators = [login_required, ] method_decorators = {post: check_permission(is_superuser), put: check_permission(is_superuser)}Saturday, June 2, 12
  46. 46. Roadmap no,
  47. 47.  we
  48. 48.  don’t To be done • Filtering (DSL?) • Caching • Throttling • Customizing field set for ModelResourceSaturday, June 2, 12
  49. 49. Questions? write
  50. 50.  code,
  51. 51.  not
  52. 52.  hollywarSaturday, June 2, 12

×