Skip to main content

Framework for making good API client libraries using urllib3.

Project description

Tiny framework for building good API client libraries thanks to urllib3.

Highlights

  • Threadsafely reuses connections with Keep-Alive (via urllib3).

  • Small and easy to understand codebase perfect for extending and building upon.

  • Built-in support for rate limiting and request throttling.

  • Functional examples for the Klout API and the Facebook OpenGraph API.

Examples

How to make your own super-simple client API library:

>>> from apiclient import APIClient
>>> class AcmePublicAPI(APIClient):
...    BASE_URL = 'https://localhost:1234/'

>>> acme_api = AcmePublicAPI()

>>> acme_api.call('/hello')
{'what': 'world'}
>>> acme_api.call('/echo', params={"ping": "pong"})
{'ping': 'pong'}

How to add rate limiting to your client API library so that we don’t exceed 10 requests per minute:

>>> from apiclient import RateLimiter
>>> lock = RateLimiter(max_messages=10, every_seconds=60)
>>> acme_api = AcmePublicAPI(rate_limit_lock=lock)

>>> # Get the first 100 pages
>>> for page in xrange(100):
...     # Whenever our request rate exceeds the specifications of the API's
...     # RateLimiter, the next request will block until the next request window
...     r = acme_api.call('/stream', page=str(page))

For more specific API examples, see the examples/ directory.

Extending

To handle different calling conventions, apiclient can be extended through subclassing.

For example, if an API requires that all arguments be JSON encoded, the _compose_url method could be implemented like this:

>>> class JSONArgsAPIClient(APIClient):
...     def _compose_url(self, path, params=None):
...         if params is not None:
...             params = dict((key, json.dumps(val))
...                            for (key, val) in params.iteritems())
...         return APIClient._compose_url(self, path, params=params)

Or if an API returns YAML instead of JSON, the _handle_response method could be overridden:

>>> class YAMLResponseAPIClient(APIClient):
...     def _handle_response(self, response):
...         return yaml.load(response.data)

TODO

  • Tests.

  • More documentation.

  • More types of API handshakes, like OAuth and OAuth2.

  • More examples.

Contributing

Any contribution is highly encouraged and desired. :)

  1. Fork on Github.

  2. Make the changes. Bonus points if changes include documentation and tests.

  3. Send a pull request.

If you’re unsure if it’s a good idea, open an Issue or contact me to discuss your proposal. Extra juicy bonus points if you pick off some of the items in the TODO list.

License

MIT

Changes

1.0.3 (2016-05-15)

  • Python 3 fixes.

1.0.2 (2011-10-20)

  • Fixed setup.py from failing upon not seeing README.rst when installing using easy_install.

1.0.1 (2011-10-10)

  • Fixed setup.py package not including a MANIFEST.in

1.0 (2011-10-03)

  • Published decoupled code from SocialGrapple.

Project details


Download files

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

Source Distribution

apiclient-1.0.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

apiclient-1.0.3-py2-none-any.whl (6.7 kB view details)

Uploaded Python 2

File details

Details for the file apiclient-1.0.3.tar.gz.

File metadata

  • Download URL: apiclient-1.0.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for apiclient-1.0.3.tar.gz
Algorithm Hash digest
SHA256 e97b1bb13646df12284a8915c99728f380fb9a3edbeb2c20927cf47afd73d0ac
MD5 b7caa05b6c41ab6a852ad8501359a5a7
BLAKE2b-256 b65e08d11020ba6087be7352c8a1856d3a6601f9f1aa7ea434ef1c584b489325

See more details on using hashes here.

File details

Details for the file apiclient-1.0.3-py2-none-any.whl.

File metadata

File hashes

Hashes for apiclient-1.0.3-py2-none-any.whl
Algorithm Hash digest
SHA256 49d1b5a6eb36281461a931243c142ea57cffcfd125ae399bf59421cccf6836c4
MD5 2e7259570bc3f3469af6a4b71f38933d
BLAKE2b-256 e67e86811aa7e86883291f4cf4f7dc027be74f745ef9e096b1cd2375247a64ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page