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
90
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
600
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Mobile First: as difficult as doing things right
swwweet
224
10k
KATA
mclloyd
32
15k
For a Future-Friendly Web
brad_frost
180
9.9k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Done Done
chrislema
185
16k
The Language of Interfaces
destraynor
162
25k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
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_