Top Banner
AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011
39

AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

Apr 27, 2018

Download

Documents

duongnhi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

AN IPHONE-PYTHONLOVE AFFAIR

Building APIs for Mobile

Friday, June 24, 2011

Page 2: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

Music Hack Day, February 2011

Samantha & Matt

Friday, June 24, 2011

Page 3: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

ACCOMPLICE #1Anna Callahan: iOS developer, jazz trumpet player

@jazztpt

Friday, June 24, 2011

Page 4: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

ACCOMPLICE #2Nate Aune: Django/Python/Plone developer, saxophonist

@natea

Friday, June 24, 2011

Page 5: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

VALENTUN.ES!

Friday, June 24, 2011

Page 6: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

http://www.youtube.com/watch?v=0C02wev40K0

Friday, June 24, 2011

Page 7: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

MOBILE TAKEOVER

If you’re not building for mobile now, you will be soon.

Friday, June 24, 2011

Page 8: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

• Isn’t REST appropriate for everything?

• Don’t I want a single API for all clients?

WHAT’S DIFFERENT ABOUT MOBILE?

* You or your customer controls the mobile app.

Friday, June 24, 2011

Page 9: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

WHAT IS REST?

Typical REST API implementation.

Friday, June 24, 2011

Page 10: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

WHAT’S DIFFERENT ABOUT MOBILE?

Mobile users are unwilling to wait.

2. Mobile is not as powerful at fetching & saving data or calculations

1. Connection = slow, spotty, or non-existent

Friday, June 24, 2011

Page 11: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

CREATE AN API DOC

Friday, June 24, 2011

Page 12: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

SO YOUR API SHOULD

• Return hierarchies of related data

• Authentication and Authorization

• Have mobile-specific error codes & messages

• Accept arrays of related or unrelated data

• Return pre-calculated data or data that doesn’t exist on device

Friday, June 24, 2011

Page 13: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

HIERARCHIES OF DATA

Bad :( Good!

Cards Tracks

Friday, June 24, 2011

Page 14: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

CARD CLASS

Friday, June 24, 2011

Page 15: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

TRACK CLASS

Friday, June 24, 2011

Page 16: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

SIMPLE REST API

Basic CRUD operations via API.Friday, June 24, 2011

Page 17: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

URLS.PY

Now access cards and tracks with /api/card/ and /api/track/Friday, June 24, 2011

Page 18: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

CREATE A CARD

Friday, June 24, 2011

Page 19: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

WHAT ABOUT TRACKS?

Default post_list from resources.py (create object via POST)

Friday, June 24, 2011

Page 20: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

WHAT ABOUT TRACKS?

api.py post_list (override method)

Friday, June 24, 2011

Page 21: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

MOBILE WANTS HIERARCHICAL DATA

Friday, June 24, 2011

Page 22: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

TASTYPIE MAKES IT EASY

Friday, June 24, 2011

Page 23: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

AUTHENTICATION & AUTHORIZATION

Authentication - let the user in the doorAuthorization - what the user can see

Friday, June 24, 2011

Page 24: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

LIMITING BY USER

Returns only the objects owned by the current user.

Friday, June 24, 2011

Page 25: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

ERROR CODES

Ideally your API should:

•Never return HTML

•Tailor response codes to actions on device

•Return messages designed for the end user

•Don’t forget the App Store

•Never, ever return HTML

Friday, June 24, 2011

Page 26: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

TAILOR ERROR CODES TO ACTIONS ON DEVICE

code 200 or 201 = success

code -10 = show alert; include user message

code -20 = show type x alert; log message

code -30 = don't alert user, but send certain info to the server

code -40 = try again

code -50 = push a web view and point it to this url

(a very simple example)

Friday, June 24, 2011

Page 27: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

JSON ERROR RESPONSES

override wrap_view from resources.pyFriday, June 24, 2011

Page 28: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

JSON ERROR HANDLING

Objective-C in Xcode

Friday, June 24, 2011

Page 29: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

http://www.youtube.com/watch?v=maZxd8K7Tjc

Friday, June 24, 2011

Page 30: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

ACCEPT ARRAYS OF DATA

• User enters tunnel

• User uses your app

• User closes your app

• User exits tunnel

Friday, June 24, 2011

Page 31: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

ACCEPT ARRAYS OF DATA

{ "pointevents": [ { "datetime":"2011-06-19 14:33:02", "level":"1", "points":"92" } ], "awards": [ { "datetime":"2011-06-19 14:29:44", "award":"12", } ]}

Friday, June 24, 2011

Page 32: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

PRE-CALCULATED DATA OR DATA NOT STORED ON

DEVICE

• No other users are stored on device

• Leaderboards or other calculated user data must come from the server

Friday, June 24, 2011

Page 33: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

TWITTER LEADERBOARD

• Compete with your friends

• Leaderboard shows daily statistics

• Best returned json for mobile:

[array of users containing username and

[array of days containing num tweets, mentions, etc]

]

Friday, June 24, 2011

Page 34: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

LEADERBOARD JSON

Friday, June 24, 2011

Page 35: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

WHEN THIS DOESN’T APPLY

• Large data sets -- only expose what client needs

• Multiple third-party clients

• Allow client to set depth level

• Create a few special expected api calls

api/card/?depth=1

or send in json package, or send in the accept header

Friday, June 24, 2011

Page 36: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

THANK YOU!

• Music Hack Day Accomplices: Matt Katz, Alexandre Passant, Jeff Novich, Twom Deryckere

• Danielzilla (Daniel Lindsley) - TastyPie

• IsaacKelly

• DjangoCon

Friday, June 24, 2011

Page 37: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

VALENTUNES

• Valentunes (Django code)https://github.com/natea/valentunes

• Valentunes (iPhone code)https://github.com/jazztpt/Valentunes_iPhone

• Valentunes (Twilio integration)https://github.com/terraces/valentunes-twilio

Friday, June 24, 2011

Page 38: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

DJANGO API FRAMEWORKS

• TastyPie documentation (the one we used)http://readthedocs.org/docs/django-tastypie/en/latest/

• django-pistonhttps://bitbucket.org/jespern/django-piston/

• Django REST frameworkhttp://django-rest-framework.org

Friday, June 24, 2011

Page 39: AN IPHONE-PYTHON LOVE AFFAIR · AN IPHONE-PYTHON LOVE AFFAIR Building APIs for Mobile Friday, June 24, 2011. ... Anna Callahan: iOS developer, jazz trumpet player @jazztpt Friday,

QUESTIONS?• Blog post with more detail on mobile api design:

http://www.annacallahan.com/blog/2011/06/24/mobile-api-design/

• Anna Callahan: annacallahan.com@jazztpt

• Nate Aune:djangozoom.com@natea

Friday, June 24, 2011