what's wrong with api wrappers and how can we do better
TRANSCRIPT
![Page 1: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/1.jpg)
What is wrong with API wrappers and how can we do better
![Page 2: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/2.jpg)
FILIPE XIMENES@xima
![Page 3: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/3.jpg)
Recife
Recife
![Page 5: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/5.jpg)
APIApplication Programming Interface
![Page 6: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/6.jpg)
What???
◇ APIs define the interaction interface of a software.
◇ software might mean:○ a Python class○ a database○ some hardware○ some plugin○ a library○ a web service
![Page 7: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/7.jpg)
Eg.: Python class
class User(object):
name = ''
email = ''
def update_data(self, name, email):
self.name = name
self.email = email
def talk(self, message):
print('{} says: {}'.format(self.name, message))
![Page 8: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/8.jpg)
Eg.: web service
GET /v1/media/000000HOST: https://api.instagram.com
POST /v1/media/000000/commentsHOST: https://api.instagram.com
DELETE /v1/media/000000/comment/111111HOST: https://api.instagram.com
![Page 9: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/9.jpg)
Integrating web services
![Page 10: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/10.jpg)
I want to retrieve basic user data after he logs in using his facebook account.
![Page 11: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/11.jpg)
Our options
◇ Option 1○ Read Facebook's API documentation.○ Use Python's urllib2 to make requests.
◇ Option 2○ Read Facebook's API documentation.○ Use Requests lib to make requests
◇ Option 3○ Search for an open source lib with ready to
go python methods for making endpoint requests.
○ Go to the beach.
![Page 12: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/12.jpg)
Web API libs, aka web API wrappers◇ What is it?
○ Implementation of a web API documentation using a programming language.
◇ What is it for?○ Creates a layer over HTTP using your favorite
programming language■ authentication■ composing urls■ prepare requests■ process responses■ format data
![Page 13: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/13.jpg)
facepy
![Page 14: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/14.jpg)
facepy
from facepy import GraphAPI
graph = GraphAPI(oauth_access_token)
my_links = graph.get(path='me/links', page=True)
for link in my_links:
print(link) # link is a dict
![Page 15: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/15.jpg)
facepy
graph = GraphAPI(oauth_access_token)
endpoint = '{}/{}'.format(user_id, 'statuses')
data = ??
user_data = graph.post(endpoint, **data)
![Page 16: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/16.jpg)
Some notes
◇ Need to study facepy's documentation○ Request interfaces○ Parameter passing○ Response access○ Exception treatment
◇ Need to study Facebook's documentation○ Endpoints and HTTP methods available○ Parameters for each endpoint○ Data formatting for each request or response
![Page 17: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/17.jpg)
python-twitter
![Page 18: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/18.jpg)
python-twitter
import twitter
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
statuses = api.GetUserTimeline(
user_id=user_uid, count=20,
since_id=id_of_first_tweet)
![Page 19: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/19.jpg)
python-twitter
![Page 20: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/20.jpg)
python-twitter
![Page 21: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/21.jpg)
Some notes
◇ pydoc documentation◇ Not very pythonic code◇ No pagination support◇ A method for each endpoint◇ Models instead of dictionaries
![Page 22: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/22.jpg)
I need a system that monitors Twitter, Facebook, Instagram, Blogger and Tumblr, to capture posts from each one of my users.
![Page 23: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/23.jpg)
The ideal wrapper
◇ Authentication◇ Requests as the engine of requests◇ Pagination support (generators)◇ Exception raising according to error codes (403,
500...)◇ Hypermedia support (links, HATEOAS)◇ Explorable◇ Simple documentation
![Page 24: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/24.jpg)
Tapioca Wrapper Delicious and comes in many flavours!
![Page 25: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/25.jpg)
![Page 27: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/27.jpg)
tapioca-wrapper
https://github.com/vintasoftware/tapioca-wrapper
![Page 28: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/28.jpg)
What's tapioca-wrapper?Python framework to create web API wrappers
![Page 29: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/29.jpg)
Demo time!!tapioca-facebook
![Page 31: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/31.jpg)
Congratulationsyou now know how to use ANY tapioca wrapper
![Page 32: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/32.jpg)
Developing a tapioca wrapper
![Page 33: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/33.jpg)
The code
Pythonhttps://github.com/vintasoftware/tapioca-facebook/blob/master/tapioca_facebook/tapioca_facebook.py
Endpoint mappinghttps://github.com/vintasoftware/tapioca-facebook/blob/master/tapioca_facebook/resource_mapping.py
![Page 34: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/34.jpg)
tapioca wrapper features
◇ A method for each endpoint◇ Explorable◇ Requests lib◇ Pagination support◇ Hypermedia support◇ Quick documentation access
![Page 35: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/35.jpg)
~250 lines of code tapioca-facebook
~1,000 lines of codefacepy
![Page 36: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/36.jpg)
~6,000 lines of code python-twitter
~150 lines of code tapioca-twitter
![Page 37: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/37.jpg)
1 hourtapioca-parse
![Page 38: What's wrong with api wrappers and how can we do better](https://reader034.vdocuments.us/reader034/viewer/2022042716/55cd3005bb61eb9f518b4682/html5/thumbnails/38.jpg)
Notes
◇ We still need to read the API documentation but not the wrapper documentation.
◇ Fun to explore the package.◇ Writing new flavours:
○ Almost 100% declarative.○ Few lines of code.○ Batteries included.
◇ There's a lot more to improve.