REST in Flask
                                raising
 the
 service




I am: Yehor Nazarkin
Follow me: @nimnull
Email me: nimnull@gmail.com
Saturday, June 2, 12
RESTin
 pieces

                            • Representative state transfer

                            • Hypertext transfer protocol:
                                     ✓ URL endpoint as resource address

                                     ✓ Content-Type

                                     ✓ Status codes

                            • The Web is REST




Saturday, June 2, 12
RESTin
 pieces

                              Web Front                       Mobile client



                                                        API



                                                       Backends


Saturday, June 2, 12
Why Flask? as
 we
 already
 have
 tones
 of
 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

Rest in flask

  • 1.
  • 2.
  • 3.
     service I am: YehorNazarkin Follow me: @nimnull Email me: nimnull@gmail.com Saturday, June 2, 12
  • 4.
  • 5.
     pieces • Representative state transfer • Hypertext transfer protocol: ✓ URL endpoint as resource address ✓ Content-Type ✓ Status codes • The Web is REST Saturday, June 2, 12
  • 6.
  • 7.
     pieces Web Front Mobile client API Backends Saturday, June 2, 12
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 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.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
     stuff Because we can Saturday, June 2, 12
  • 22.
  • 23.
  • 24.
  • 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.
  • 27.
  • 28.
  • 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(Doesn't match)} return {'pass': value['pass']} Saturday, June 2, 12
  • 30.
  • 31.
  • 32.
  • 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, c Saturday, June 2, 12
  • 34.
  • 35.
  • 36.
     did Based on Flask’s MethodView Extended with method-decorators Simple to attach Resource — for generic cases ModelResource — SQLA mapped classes Saturday, June 2, 12
  • 37.
  • 38.
  • 39.
     did Inspired by • django-tastypie • flask-restless • rainbows and unicorns Saturday, June 2, 12
  • 40.
  • 41.
  • 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.
  • 44.
  • 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.
  • 47.
  • 48.
     don’t To be done • Filtering (DSL?) • Caching • Throttling • Customizing field set for ModelResource Saturday, June 2, 12
  • 49.
  • 50.
  • 51.
  • 52.