Skip to main content

A dynamic api client

Project description

ApiBee is a dynamic rest client built on top of the excellent restkit library.

It aims to be able to handle any uris for any apis.

How it work ?

Let’s say we want to fetch the json API of the great service Their api is served from

>>> from apibee import Client
>>> api = Client('')

Now we want to get the result from

>>> results ='Timy')

That’s it !

Going further

Getting json result


class ExampleClient(Client):
    def process_result(self, result):
        return json.loads(result)

Now the result is a python type:

>>> api = ExampleClient("")
>>> result ="timy")

Adding query automatically

Sometimes some api are a bit tricky. And we need to build a custom client to match thoses.

Previously, the version of the api was part of the resource but what if we have to specify it for each request:

We can specify one or more params which will be automatically add to the query:

class ExampleClient(Client):
    def set_persistent_query(**args):
        self._persistent_query = args

    def build_query(self, response, query):
        return response, query

>>> api = ExampleClient('')
>>> api.set_persistent_query(v="1.0")

Customize resources

Some apis like Twitter add .json after the resource but before the query : We can do it like this:

class TwitterClient(Client):
    def set_format(self, f):
        self._format = f

    def build_query(self, response, query):
        response = "%s.%s" % (response, self._format)
        return response, query

>>> api = TwitterClient('', end_resource='.json')
>>> results ='Timy')

Raising errors

Sometimes you may have to clean up the result before send it back. You can do it by overloading the Client.process_result method.


Google’s web service won’t send an http error 400 if the request failed. Instead, it will send a custom result:

will send back:

{"responseData": null, "responseDetails": "invalid version", "responseStatus": 400}

Let’s say we want to catch the error and raise an RequestFailed exception with a custom message which is in the “responseDetails” field:

class GoogleClient(Client):
    def process_result(self, result):
        if result["responseStatus"] == 400:
            raise RequestFailed(result['responseDetails'])
        return result

That’s it ! Don’t forget to return the result at the end of the process_result method.

>>> api = GoogleClient('')
Traceback (most recent call last):
RequestFailed: invalid version

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for apibee, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size apibee-0.2.2.tar.gz (37.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page