Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What it's really like building RESTful Web APIs...

Avatar for Paul Hallett Paul Hallett
July 23, 2015
87

What it's really like building RESTful Web APIs with Django

Experiences building RESTful web services using Django REST Framework, from EuroPython 2015

Avatar for Paul Hallett

Paul Hallett

July 23, 2015
Tweet

Transcript

  1. 5

  2. 6

  3. 7

  4. 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/
  5. 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/
  6. 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/
  7. 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/
  8. 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/
  9. 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
  10. 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’, )
  11. 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
  12. 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
  13. 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