TapiocaAdapter class

class TapiocaAdapter



This should contain the base URL that will be concatenated with the resource mapping itens and generate the final request URL. You can either set this attribute or use the get_api_root method.


For more information about the serializer_class attribute, read the serializers documentation.


get_api_root(self, api_params)

This method can be used instead of the api_root attribute. You might also use it to decide which base URL to use according to a user input.

def get_api_root(self, api_params):
        if api_params.get('development'):
                return 'http://api.the-dev-url.com/'
        return 'http://api.the-production-url.com/'
get_request_kwargs(self, api_params, *args, **kwargs)

This method is called just before any request is made. You should use it to set whatever credentials the request might need. The api_params argument is a dictionary and has the parameters passed during the initialization of the tapioca client:

cli = Facebook(access_token='blablabla', client_id='thisistheis')

For this example, api_params will be a dictionary with the keys access_token and client_id.

Here is an example of how to implement Basic Auth:

from requests.auth import HTTPBasicAuth

class MyServiceClientAdapter(TapiocaAdapter):
        def get_request_kwargs(self, api_params, *args, **kwargs):
                params = super(MyServiceClientAdapter, self).get_request_kwargs(
                        api_params, *args, **kwargs)

                params['auth'] = HTTPBasicAuth(
                        api_params.get('user'), api_params.get('password'))

                return params
process_response(self, response)

This method is responsible for converting data returned in a response to a dictionary (which should be returned). It should also be used to raise exceptions when an error message or error response status is returned.

format_data_to_request(self, data)

This converts data passed to the body of the request into text. For example, if you need to send JSON, you should use json.dumps(data) and return the response. See the mixins section above.

response_to_native(self, response)

This method receives the response of a request and should return a dictionay with the data contained in the response. see the mixins section above.

get_iterator_next_request_kwargs(self, iterator_request_kwargs, response_data, response)

Override this method if the service you are using supports pagination. It should return a dictionary that will be used to fetch the next batch of data, e.g.:

def get_iterator_next_request_kwargs(self,
                iterator_request_kwargs, response_data, response):
        paging = response_data.get('paging')
        if not paging:
        url = paging.get('next')

        if url:
                iterator_request_kwargs['url'] = url
                return iterator_request_kwargs

In this example, we are updating the URL from the last call made. iterator_request_kwargs contains the paramenters from the last call made, response_data contains the response data after it was parsed by process_response method, and response is the full response object with all its attributes like headers and status code.

get_iterator_list(self, response_data)

Many APIs enclose the returned list of objects in one of the returned attributes. Use this method to extract and return only the list from the response.

def get_iterator_list(self, response_data):
        return response_data['data']

In this example, the object list is enclosed in the data attribute.

is_authentication_expired(self, exception, *args, **kwargs)

Given an exception, checks if the authentication has expired or not. If so and `refresh_token_by_default=True` or the HTTP method was called with `refresh_token=True`, then it will automatically call `refresh_authentication` method and retry the original request.

If not implemented, `is_authentication_expired` will assume `False`, `refresh_token_by_default` also defaults to `False` in the client initialization.

refresh_authentication(self, api_params, *args, **kwargs):

Should do refresh authentication logic. Make sure you update api_params dictionary with the new token. If it successfully refreshs token it should return a truthy value that will be stored for later access in the executor class in the refresh_data attribute. If the refresh logic fails, return a falsy value. The original request will be retried only if a truthy is returned.