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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Paul Hallett
July 23, 2015
0
91
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
610
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.1k
What does AI have to do with Human Rights?
axbom
PRO
0
2k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Agile that works and the tools we love
rasmusluckow
331
21k
How to Think Like a Performance Engineer
csswizardry
28
2.5k
[SF Ruby Conf 2025] Rails X
palkan
1
760
エンジニアに許された特別な時間の終わり
watany
106
230k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
79
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
How to make the Groovebox
asonas
2
1.9k
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_