Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
restkiss: Make REST simple again
Search
Bruno Marques
October 18, 2016
Programming
0
66
restkiss: Make REST simple again
Lightning talk apresentada na Python Brasil [12], em Florianópolis/SC.
Bruno Marques
October 18, 2016
Tweet
Share
Other Decks in Programming
See All in Programming
Data-Centric Kaggle
isax1015
2
780
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
7.4k
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
130
組織で育むオブザーバビリティ
ryota_hnk
0
180
AWS re:Invent 2025参加 直前 Seattle-Tacoma Airport(SEA)におけるハードウェア紛失インシデントLT
tetutetu214
2
120
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
390
CSC307 Lecture 06
javiergs
PRO
0
690
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
610
CSC307 Lecture 09
javiergs
PRO
1
840
Package Management Learnings from Homebrew
mikemcquaid
0
230
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
130
なるべく楽してバックエンドに型をつけたい!(楽とは言ってない)
hibiki_cube
0
140
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
A designer walks into a library…
pauljervisheath
210
24k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
270
Music & Morning Musume
bryan
47
7.1k
sira's awesome portfolio website redesign presentation
elsirapls
0
150
Building the Perfect Custom Keyboard
takai
2
690
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
It's Worth the Effort
3n
188
29k
Documentation Writing (for coders)
carmenintech
77
5.3k
Test your architecture with Archunit
thirion
1
2.2k
How to build a perfect <img>
jonoalderson
1
4.9k
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
Transcript
restkiss Make REST simple again
Quem? Bruno Oliveira Marques • Porto Alegre - RS •
1ª PythonBrasil ◦ *clap, clap, clap* • Pythonista, sulista, baixista • Backend @ Crave Food Services • Mestrado em Computação @ UFRGS http://twitter.com/assinales http://twitter.com/DataMarques http://github.com/ElSaico
[email protected]
Às vezes você só precisa de verbos HTTP que falem
JSON
Abordagem tradicional (mágica) 1. Escolher framework full-stack 2. Definir schema
no ORM 3. Definir modelo de autenticação/autorização 4. Criar classe(s) referenciando tudo isso 5. ??? 6. Profit!
E se eu não quiser que o framework tome todas
essas decisões por mim?
E se eu não quiser que o framework tome todas
essas decisões por mim? ¯\_(ツ)_/¯
Com Django class PostResource(DjangoResource): preparer = FieldsPreparer(fields={ 'id': 'id', 'title':
'title', 'author': 'user.username', 'body': 'content', 'posted_on': 'posted_on', }) # GET /api/posts/ def list(self): return Post.objects.all() # GET /api/posts/<pk>/ def detail(self, pk): return Post.objects.get(id=pk) # POST /api/posts/ def create(self): return Post.objects.create( title=self.data['title'], content=self.data['body'] ) ... url(r'posts/$', PostResource.as_list())
Sem Django! class PostResource(FlaskResource): posts = SomePostClass() preparer = FieldsPreparer(fields={
'id': 'id', 'title': 'title', 'author': 'username', 'body': 'content', 'posted_on': 'posted_on', }) # GET /api/posts/ def list(self): return self.posts.get_all() # GET /api/posts/<pk>/ def detail(self, pk): return self.posts.get_by_id(pk) # POST /api/posts/ def create(self): return self.posts.create( title=self.data['title'], content=self.data['body'] ) ... PostResource.add_url_rules(app, rule_prefix='/posts/')
Autenticação def is_authenticated(self): return AuthBackend.stuff_which_returns_a_bool(self.request)
Quero retornar um erro! raise Forbidden({‘message’: ‘This is dangerous knowledge
which could kill you’}) raise NotFound({‘message’: ‘Object not found’}) raise IAmATeapot({‘short’: True, ‘stout’: True})
Quero serializar de outros jeitos! class MultiSerializer(Serializer): def deserialize(self, body):
if self.request.GET.get('fmt') == 'yaml': return yaml.safe_load(body) else: return json.load(body) def serialize(self, data): if self.request.GET.get('fmt') == 'yaml': return yaml.dump(body) else: return json.dumps(body) class PostResource(Resource): serializer = MultiSerializer
Obrigado! pip install restkiss http://restkiss.readthedocs.io/