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
89
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
How GitHub (no longer) Works
holman
315
140k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
A better future with KSS
kneath
239
17k
Documentation Writing (for coders)
carmenintech
74
5k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
188
55k
KATA
mclloyd
32
14k
Scaling GitHub
holman
463
140k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.7k
Rebuilding a faster, lazier Slack
samanthasiow
83
9.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
51
5.6k
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_