Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
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
600
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.6k
The Language of Interfaces
destraynor
162
25k
Code Reviewing Like a Champion
maltzj
527
40k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
A designer walks into a library…
pauljervisheath
210
24k
What's in a price? How to price your products and services
michaelherold
246
13k
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_