Python API clients with Tapioca

Filipe Ximenes
October 25, 2016
<p>In this post I'll present to you <a href="">Tapioca</a>, a Python library to create powerful API clients with very few lines of code. If you don't want to read through the reasons why I've built it, you may just jump straight to the <a href="">Tapioca Wrapper</a> section.</p><h2 id="why-do-we-need-a-better-way-to-build-api-clients">Why do we need a better way to build API clients</h2><p>Integrating with external services is painful. Here at Vinta, we build and deal with both public and private APIs on the daily basis. Having worked in many of those projects I can safely tell that we are currently in hell when it comes to integrations. There are two ends in this mess: one is the result of poorly designed APIs and the other is the lack of standards and good practices for building API clients.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="" class="kg-image"><figcaption><a href=""></a></figcaption></figure><p>It begins with API design. As we get away from RPC based architectures and move to more REST style systems, there is clearly a lack of well formed practical set of rules or at least a broad misunderstanding of the rules for building APIs. This is actually not a surprise since we were used to RPC and its well defined <em>protocols</em> while REST provides a set of guides, and is actually an <em>architectural style</em>. Luckily there are people trying to solve this problem, the <a href="">json:api</a> project is a good example of where we should aim for good design and standardization.</p><figure class="kg-card kg-image-card kg-card-hascaption"><img src="" class="kg-image"><figcaption><em>Roy Fielding, creator of the REST <strong>architectural style</strong></em></figcaption></figure><p>On the other side there are people trying to use these poorly designed APIs. Most developers doing integrations don't want to deal with the complicated and unique patterns API creators freely chose for their systems. The easiest [and most common] way of trying to stay away from this mess is to use open source available API clients (or API wrappers). Which sounds like a good plan as long as you are doing a simple integration with a single service. As you move to multiple complex integrations you start noticing that the API client spectrum is as messy as the API services. Every single wrapper lib has it own unique way of dealing with authentication, pagination, versioning, throttling and parameter passing. Not to mention the huge amount of duplicated code to perform the exact same set of features among them.</p><p>While we developers using public APIs do not have control over how they are built, we do have on the API client side. This takes us to Tapioca.</p><h2 id="tapioca-wrapper">Tapioca Wrapper</h2><figure class="kg-card kg-image-card"><img src="" class="kg-image"></figure><p><a href=""><strong>Tapioca</strong></a> is a Python API client maker. It gathers most of the features API clients implement and puts them in an extensible core. Wrappers will then extend this core implementing only the specifics from each service (such as authentication and pagination) and get all the common API client features for free. Tapioca approach also comes in handy because regardless of the service, clients look the same in the way you interact with them. Once you've worked with a Tapioca lib you know how to use them all, no need to learn things from scratch.</p><p>Here is the <a href=""><code>tapioca-facebook</code></a> in action.</p><pre><code>&gt;&gt;&gt; from tapioca_facebook import Facebook &gt;&gt;&gt; &gt;&gt;&gt; api = Facebook(access_token='{your-access-token}') &gt;&gt;&gt; feed = api.user_feed(id='me').get() &gt;&gt;&gt; print([0].link) &lt;TapiocaClient object ''&gt; </code></pre><p>And now the <a href=""><code>tapioca-twitter</code></a>:</p><pre><code>&gt;&gt;&gt; from tapioca_twitter import Twitter &gt;&gt;&gt; &gt;&gt;&gt; api = Twitter( &gt;&gt;&gt; api_key='{your-api-id}', &gt;&gt;&gt; api_secret='{your-api-secret}', &gt;&gt;&gt; access_token='{your-access-token}', &gt;&gt;&gt; access_token_secret='{your-access-token-secret}') &gt;&gt;&gt; &gt;&gt;&gt; statuses = api.statuses_user_timeline().get() &gt;&gt;&gt; print(statuses[0].text) &lt;TapiocaClient object ('RT @vintasoftware: Our latest blog post was featured in @thedjangoweekly and ' '@importpython newsletters! Go check it out:')&gt; </code></pre><p>Pagination is also very easy and similar for both services.</p><p>Facebook:</p><pre><code>&gt;&gt;&gt; feed = api.user_feed(id='me').get() &gt;&gt;&gt; for item in feed().pages(): &gt;&gt;&gt; print( ... </code></pre><p>Twitter:</p><pre><code>&gt;&gt;&gt; statuses = api.statuses_user_timeline().get() &gt;&gt;&gt; for item in statuses().pages(): &gt;&gt;&gt; print( ... </code></pre><p>There are many other features (e.g.: serialization) you can check <a href="">here</a>.</p><p>To give you an idea on how easy it is to build a client with Tapioca, here is the source code for the <code>tapioca-facebook</code> we just played with. <a href="">See the code in the repository</a>.</p><pre><code>from tapioca import ( TapiocaAdapter, generate_wrapper_from_adapter, JSONAdapterMixin) from requests_oauthlib import OAuth2 from .resource_mapping import RESOURCE_MAPPING class FacebookClientAdapter(JSONAdapterMixin, TapiocaAdapter): api_root = '' resource_mapping = RESOURCE_MAPPING def get_request_kwargs(self, api_params, *args, **kwargs): params = super(FacebookClientAdapter, self).get_request_kwargs( api_params, *args, **kwargs) params['auth'] = OAuth2( api_params.get('client_id'), token={ 'access_token': api_params.get('access_token'), 'token_type': 'Bearer'}) return params def get_iterator_list(self, response_data): return response_data['data'] def get_iterator_next_request_kwargs(self, iterator_request_kwargs, response_data, response): paging = response_data.get('paging') if not paging: return url = paging.get('next') if url: return {'url': url} Facebook = generate_wrapper_from_adapter(FacebookClientAdapter) </code></pre><p>That's right, it's a entire client lib from a 37 line file plus the [ridiculously simple] <a href="">resource mapping</a>.</p><p>You can benefit from the <a href=""><strong>flavours</strong> that were already developed</a>. Or you can create a new Tapioca for a public API or your private service using the <a href="">Cookiecutter template</a>.</p><p><strong>Resources:</strong></p><ul><li><a href="">Project Github page</a></li><li><a href="">How to build a Tapioca wrapper</a></li><li><a href="">Full project documentation</a></li></ul><p>Want read more about <em>APIs</em>? Check out these posts from our blog:</p><ul><li><a href="">Seriously, we can build better web API clients</a></li><li><a href="">Classy Django REST Framework Release</a></li></ul>