Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

What's wrong with api wrappers and how can we do better [EP 2015]

1.858 visualizaciones

Publicado el

Wrappers are an essential tool for interacting with web APIs. They reduce the amount of work needed to make requests and sometimes, only sometimes prevent the developer from dealing with extensive documentations. It’s common to encounter libs that require not only the study of their own documentation, but also the APIs one, duplicating the needed work. This is caused because wrappers do not follow a design pattern, each developer creates it’s own design, coding style and use their preferred tools.

Tapioca is what can be called: “a wrapper generator”. Creating API wrappers with Tapioca is extremely easy and fast. For example, it took 1 hour to write the full wrapper for the Parse.com REST API. But this is not the more important thing, Tapioca libs have a similar interface so once understood how they work, developers can work with any other without the need to learn a new interface.

Tapioca is also thought to comply with REST features and takes HATEOAS (Hypermedia as the engine of application state) seriously, so “following” links and pagination are natively supported. Explorability is also a key concept and developers are encouraged to play with Tapioca packages and find their way through APIs before writing their final code. Although there are some production ready Tapioca wrappers, it is a work in progress, there are still many features to be explored.

Publicado en: Tecnología
  • 1 minute a day to keep your weight away! ■■■ https://tinyurl.com/1minweight4u
       Responder 
    ¿Estás seguro?    No
    Tu mensaje aparecerá aquí

What's wrong with api wrappers and how can we do better [EP 2015]

  1. 1. What is wrong with API wrappers and how can we do better
  2. 2. FILIPE XIMENES @xima
  3. 3. Recife Recife
  4. 4. http://www.vinta.com.br
  5. 5. API Application Programming Interface
  6. 6. 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
  7. 7. 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))
  8. 8. Eg.: web service GET /v1/media/000000 HOST: https://api.instagram.com POST /v1/media/000000/comments HOST: https://api.instagram.com DELETE /v1/media/000000/comment/111111 HOST: https://api.instagram.com
  9. 9. Integrating web services
  10. 10. I want to retrieve basic user data after he logs in using his facebook account.
  11. 11. 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.
  12. 12. 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
  13. 13. facepy
  14. 14. 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
  15. 15. facepy graph = GraphAPI(oauth_access_token) endpoint = '{}/{}'.format(user_id, 'statuses') data = ?? user_data = graph.post(endpoint, **data)
  16. 16. 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
  17. 17. python-twitter
  18. 18. 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)
  19. 19. python-twitter
  20. 20. python-twitter
  21. 21. Some notes ◇ pydoc documentation ◇ Not very pythonic code ◇ No pagination support ◇ A method for each endpoint ◇ Models instead of dictionaries
  22. 22. I need a system that monitors Twitter, Facebook, Instagram, Blogger and Tumblr, to capture posts from each one of my users.
  23. 23. 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
  24. 24. Tapioca Wrapper Delicious and comes in many flavours!
  25. 25. tapioca-wrapper https://github.com/vintasoftware/tapioca-wrapper
  26. 26. What's tapioca- wrapper? Python framework to create web API wrappers
  27. 27. Demo time!! tapioca-facebook
  28. 28. Congratulations you now know how to use ANY tapioca wrapper
  29. 29. Developing a tapioca wrapper
  30. 30. The code Python https://github.com/vintasoftware/tapioca- facebook/blob/master/tapioca_facebook/tapioca_fac ebook.py Endpoint mapping https://github.com/vintasoftware/tapioca- facebook/blob/master/tapioca_facebook/resource_ mapping.py
  31. 31. tapioca wrapper features ◇ A method for each endpoint ◇ Explorable ◇ Requests lib ◇ Pagination support ◇ Hypermedia support ◇ Quick documentation access
  32. 32. ~250 lines of code tapioca-facebook ~1,000 lines of code facepy
  33. 33. ~6,000 lines of code python-twitter ~150 lines of code tapioca-twitter
  34. 34. 1 hour tapioca-parse
  35. 35. 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.
  36. 36. QUESTIONS? @xima github.com/filipeximenes ximenes@vinta.com.br www.vinta.com.br

×