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
What it's really like building RESTful Web APIs...
Search
Paul Hallett
July 23, 2015
0
87
What it's really like building RESTful Web APIs with Django
Experiences building RESTful web services using Django REST Framework, from EuroPython 2015
Paul Hallett
July 23, 2015
Tweet
Share
More Decks by Paul Hallett
See All by Paul Hallett
Django Microservices Made Easy
phalt
0
590
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
200k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
The Invisible Side of Design
smashingmag
301
51k
Faster Mobile Websites
deanohume
307
31k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
282
13k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
Transcript
1 Paul Hallett 23.07.2015 What it’s really like building RESTful
APIs with Django.
2 Paul Hallett Writing software for ~7 years Working at
Lyst @phalt_ Me ! API Fanatic
3 http://pokeapi.co
4 https://swapi.co
5
6
7
8 curl -s -X POST -d ' { "jsonrpc": "2.0",
"method": "product.get", "params": { "username":"USERNAME", "password":"PASSWORD", "product":"PRODUCT_SLUG" }, "id": 1}' https://api.lyst.com/rpc/v1/
9 curl -s -X POST -d ' { "jsonrpc": "2.0",
"method": "product.get", "params": { "username":"USERNAME", "password":"PASSWORD", "product":"PRODUCT_SLUG" }, "id": 1}' https://api.lyst.com/rpc/v1/
10 curl -s -X POST -d ' { "jsonrpc": "2.0",
"method": "user.register", "params": { "email":"
[email protected]
", "username":"dummy", "password":"p123456", "gender":"M" }, "id": 1}' https://api.lyst.com/rpc/v1/
11 curl -s -X POST -d ' { "jsonrpc": "2.0",
"method": "user.register", "params": { "email":"
[email protected]
", "username":"dummy", "password":"p123456", "gender":"M" }, "id": 1}' https://api.lyst.com/rpc/v1/
12 curl -s -X POST -d ' { "jsonrpc": "2.0",
"method": "product.get", "params": { "username":"USERNAME", "password":"PASSWORD", "product":"PRODUCT_SLUG" }, "id": 1}' https://api.lyst.com/rpc/v1/
13 RPC for HTTP is bad 13
14 Don’t use it 14
15 Rebuilding with Django Rest Framework 15
16 Why Django Rest Framework? 16
17 Batteries Included Web Framework
18 Batteries Included API Framework Batteries Included Web Framework
19 Batteries Included Web Framework Batteries Included API Framework
20 What we learned 20
21 21 A chance to rebuild
22 22 Design first. Code second.
23 23 Resources
24 24 Resources != Models
25 Creating resources - Cart - state - country -
cart_type - user - shipping_title - shipping_firstname - shipping_lastname - shipping_email - shipping_phone - shipping_address_1 - shipping_address_2 - billing_title - billing_firstname - billing_lastname - billing_email - billing_phone - billing_address_1 - billing_address_2
26 26 Serializers
27 class CartSerializer(serializers.ModelSerializer): def get_item_count(self, instance): return instance.items.count() item_count =
serializers.SerializerMethodField() class Meta: model = Cart fields = ( 'state', 'country', 'cart_type', 'user_type', 'date_added', 'date_updated', 'item_count', 'shipping_price', 'currency', 'resource_url’, )
28 - Cart - state - country - cart_type -
user - shipping_title - shipping_firstname - shipping_lastname - shipping_email - shipping_phone - shipping_address_1 - shipping_address_2 - billing_title - billing_firstname - billing_lastname - billing_email - billing_phone - billing_address_1 - billing_address_2 - Cart - state - country - cart_type - user - item_count - shipping - shipping_title - shipping_firstname - shipping_lastname - shipping_email - shipping_phone - shipping_address_1 - shipping_address_2 - billing - billing_title - billing_firstname - billing_lastname - billing_email - billing_phone - billing_address_1 - billing_address_2 Creating resources
29 - Cart - state - country - cart_type -
user - shipping_title - shipping_firstname - shipping_lastname - shipping_email - shipping_phone - shipping_address_1 - shipping_address_2 - billing_title - billing_firstname - billing_lastname - billing_email - billing_phone - billing_address_1 - billing_address_2 - Cart - state - country - type - user - item_count - shipping - title - firstname - lastname - email - phone - address_1 - address_2 - billing - title - firstname - lastname - email - phone - address_1 - address_2 Creating resources
30 30 Routers
31 31 Write your own URLconf
32 32 Permissions
33 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.AllowAny', )
34 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.AdminOnly', )
35 class CheckoutAPIView(BaseAPIView): """ Common methods for all Checkout resources.
""" permission_classes = (IsAuthenticated,)
36 36 Authentication
37 pip install django-oauth-toolkit
38 INSTALLED_APPS = ( . . . 'ouath2_toolkit', . .
. )
39 'DEFAULT_AUTHENTICATION_CLASSES': ( 'oauth2_provide.ext.rest_framework.OAuth2Authentication', )
40 request.user
41 41
42 42 HTTP only provides so much
43 43 HTTP CHECKOUT?
44 44 HTTP POST api.lyst.com/cart/checkout/
45 45 Asynchronous?
46 46 HTTP POST api.lyst.com/cart/checkout/ { callback_url : ‘mywebsite.com/checkout_complete/' }
47 47 HTTP 202 ACCEPTED { . . . }
48 48 Documentation
Django REST Framework has so many cool things, use them.
Design before you build. Use this opportunity to redesign your data model and services. Go off the beaten track only when you have to. Document well. Don’t use RPC 49 Wrap up
thank you Paul Hallett @phalt_