Rest in flask
Upcoming SlideShare
Loading in...5
×
 

Rest in flask

on

  • 4,061 views

My way to build RESTful solution with python and flask

My way to build RESTful solution with python and flask

Statistics

Views

Total Views
4,061
Views on SlideShare
4,056
Embed Views
5

Actions

Likes
12
Downloads
52
Comments
0

1 Embed 5

http://coderwall.com 5

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Rest in flask Rest in flask Presentation Transcript

  • REST in Flask raising
  •   the
  •   serviceI am: Yehor NazarkinFollow me: @nimnullEmail me: nimnull@gmail.comSaturday, June 2, 12
  • RESTin
  •   pieces • Representative state transfer • Hypertext transfer protocol: ✓ URL endpoint as resource address ✓ Content-Type ✓ Status codes • The Web is RESTSaturday, June 2, 12
  • RESTin
  •   pieces Web Front Mobile client API BackendsSaturday, 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
  • Why Flask? as
  •   we
  •   already
  •   have
  •   tones
  •   of
  •   stuff Because we canSaturday, June 2, 12
  • Validation resource
  •   tier,
  •   model
  •   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
  • Validation resource
  •   tier,
  •   model
  •   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
  • Validation resource
  •   tier,
  •   model
  •   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
  • Achievements yes,
  •   we
  •   did Based on Flask’s MethodView Extended with method-decorators Simple to attach Resource — for generic cases ModelResource — SQLA mapped classesSaturday, June 2, 12
  • Achievements yes,
  •   we
  •   did Inspired by • django-tastypie • flask-restless • rainbows and unicornsSaturday, June 2, 12
  • Achievements yes,
  •   we
  •   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
  • Achievements yes,
  •   we
  •   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
  • Roadmap no,
  •   we
  •   don’t To be done • Filtering (DSL?) • Caching • Throttling • Customizing field set for ModelResourceSaturday, June 2, 12
  • Questions? write
  •   code,
  •   not
  •   hollywarSaturday, June 2, 12