ChartMogul API Python Client
Clone or download
spyrbri and pkopac Add support for rate limiting (#21)
* Add granular control over the conditions under which we retry a request

* Add retry functionality and allow config

* Update Readme

* Bump version to 1.2.0

* fixup! Add retry functionality and allow config

* Review fixes
Latest commit 2f8d968 Aug 24, 2018

README.md

Official ChartMogul API Python Client

chartmogul-python provides convenient Python bindings for ChartMogul's API.

PyPI version Build Status


Installation | Configuration | Usage | Development | Contributing | License



Installation

This library requires Python 2.7 or 3.3 to 3.6.

pip install chartmogul

Configuration

First create a Config object by passing your account token and secret key, available from the administration section of your ChartMogul account. You need to pass this configuration object as the first argument to each request.

import chartmogul
config = chartmogul.Config('token', 'secret')

Alternatively, you can use the library without the module prefix:

from chartmogul import *
config = Config('token', 'secret')

Note that both ways should import all necessary classes and submodules, but the first one is preferred due to being explicit.

To test authentication, try ping endpoint:

import chartmogul
chartmogul.Ping.ping(config).get()

This throws error or returns <Ping{data='pong!'}>

Options

You can also pass to the Config initializer:

  • request_timeout= sets timeout for requests (seconds), default: none (see requests docs for details)

Rate Limits & Exponential Backoff

The library will keep retrying if the request exceeds the rate limit or if there's any network related error. By default, the request will be retried for 20 times (approximately 15 minutes) before finally giving up.

You can change the retry count from the Config initializer:

  • max_retries= sets the maximum number of retries for failed requests, default: 20
  • backoff_factor= sets the exponential backoff factor, default: 2

Set max_retries 0 to disable it. Set backoff_factor 0 to disable it.

Usage

The library is based on promises (mechanism similar to futures). Every call therefore returns an object like <promise.promise.Promise object at 0x123456789> The requests are running asynchronously, but you can use them synchronously, just appending .get(), which will block your program until the response has come. Both sync/async ways will return/pass native Python objects mapping to the entities in the API, or raise/pass errors from the API, see an example:

import chartmogul

config = chartmogul.Config('token', 'secret')
req = chartmogul.Plan.create(config, data={'name': 'Awesome plan'...})

# Now either asynchronous reaction:
req.then(lambda plan: print(plan)).catch(reactOnException)

# or synchronous:
try:
    print(req.get())
except Exception as ex:
    reactOnException(ex)

Import API

Available methods in Import API:

Data Sources

chartmogul.DataSource.create(config, data={'name': 'In-house billing'})
chartmogul.DataSource.retrieve(config, uuid='ds_5915ee5a-babd-406b-b8ce-d207133fb4cb')
chartmogul.DataSource.all(config)
chartmogul.DataSource.destroy(config, uuid='ds_5915ee5a-babd-406b-b8ce-d207133fb4cb')

Customers

chartmogul.Customer.create(config, data={})
chartmogul.Customer.all(config, page=2, per_page=20)
chartmogul.Customer.retrieve(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb')
chartmogul.Customer.search(config, email='email@email.com')
chartmogul.Customer.merge(config, data={
  'from': {'customer_uuid': 'cus_5915ee5a-babd-406b-b8ce-d207133fb4cb'},
  'into': {'customer_uuid': 'cus_2123290f-09c8-4628-a205-db5596bd58f7'}
})
chartmogul.Customer.modify(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={
  'from': {'customer_uuid': 'cus_5915ee5a-babd-406b-b8ce-d207133fb4cb'},
  'into': {'customer_uuid': 'cus_2123290f-09c8-4628-a205-db5596bd58f7'}
})
chartmogul.Customer.destroy(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb')

Customer Attributes

Note that the returned attributes of type date are not parsed and stay in string.

chartmogul.Attributes.retrieve(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb')

Tags

chartmogul.Tags.add(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={
  'tags': ['important', 'Prio1']
})
chartmogul.Tags.add(config, data={
  'email': 'adam@smith.com',
  'tags': ['important', 'Prio1']
})
chartmogul.Tags.remove(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={
  'tags': ['important', 'Prio1']
})

Custom Attributes

chartmogul.CustomAttributes.add(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={
  'custom': [
    {'type': 'Integer', 'key': 'age', 'value': 8}
  ]
})
chartmogul.CustomAttributes.add(config, data={
  'email': 'adam@smith.com',
  'custom': [
    {'type': 'Integer', 'key': 'age', 'value': 8}
  ]
})
chartmogul.CustomAttributes.update(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={
  'custom': {
    'age': 20,
    'channel': 'Twitter'
  }
});
chartmogul.CustomAttributes.remove(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={
  'custom': ['CAC']
})

Plans

chartmogul.Plan.create(config, data={})
chartmogul.Plan.retrieve(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb')
chartmogul.Plan.modify(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={
    'name': 'new name'
})
chartmogul.Plan.all(config, page=2, external_id='')
chartmogul.Plan.destroy(config, uuid='')

Invoices

import chartmogul

chartmogul.Invoice.create(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={})
chartmogul.Invoice.all(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', page=2, per_page=10)
chartmogul.Invoice.all(config, customer_uuid='cus_f466e33d-ff2b-4a11-8f85-417eb02157a7', external_id='INV0001')
chartmogul.Invoice.retrieve(config, uuid='inv_22910fc6-c931-48e7-ac12-90d2cb5f0059')

Transactions

import chartmogul

chartmogul.Transaction.create(config, uuid='inv_745df1d4-819f-48ee-873d-b5204801e021', data={})

Subscriptions

import chartmogul

chartmogul.Subscription.list_imported(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb')
chartmogul.Subscription.cancel(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb' data={'cancelled_at': ''})
chartmogul.Subscription.modify(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb' data={'cancellation_dates': []})

Metrics API

Available methods in Metrics API:

chartmogul.Metrics.all(config,
                       start_date='2015-01-01', # notice the _ here
                       end_date='2015-11-24',
                       interval='month',
                       geo='GB',
                       plans='Bronze Plan'
})
chartmogul.Metrics.mrr(config,
                       start_date='2015-01-01',
                       end_date='2015-11-24',
                       interval='month',
                       geo='GB',
                       plans='PRO Plan')
chartmogul.Metrics.arr(config, data={})
chartmogul.Metrics.arpa(config, data={})
chartmogul.Metrics.asp(config, data={})
chartmogul.Metrics.customer_count(config, data={})
chartmogul.Metrics.customer_churn_rate(config, data={})
chartmogul.Metrics.mrr_churn_rate(config, data={})
chartmogul.Metrics.ltv(config, data={})

chartmogul.Activity.all(config, uuid='')
chartmogul.Subscription.all(config, uuid='')

Errors

The library throws TypeError if data parameter is not serializable.

The chartmogul.ArgumentMissingError is raised if obligatory uuid or data is missing in the call.

The error chartmogul.APIError is raised for any non-20x response from the API. It always has cause of type requests.HTTPError, which contains the response, so you can extract JSON/text in the following way (and program reaction):

from pprint import pprint
try:
    chartmogul.doStuff()
except chartmogul.APIError as e:
    response = e.__cause__.response
    try:
        pprint(response.json())
    except ValueError:
        pprint(response.text)

The cause default serialization doesn't give the API user much detail:

HTTPError('422 Client Error: Unprocessable Entity for url: https://api.chartmogul.com/v1/data_sources',)

That's why it's wrapped, so that you get the detail by default.

APIError(b'{"errors":{"name":"Has already been taken."}}',)

Development

To work on the library:

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Install dependencies: pip install -r requirements.txt && python setup.py develop
  • Fix bugs or add features. Make sure the changes pass the coding guidelines (use pylama).
  • Write tests for your new features. Use requests_mock for HTTP mocking.
  • Run tests with python setup.py test and check test coverage with:
    • pip install coverage
    • coverage run ./setup.py test
    • coverage html --include='chartmogul/*'
    • Find results in htmlcov/index.html
  • If all tests are passed, push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

For a testing project we recommend setting up a virtualenv with development mode installation and Jupyter. virtualenvwrapper is another handy tool.

Built using Requests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/chartmogul/chartmogul-python.

Releasing

Make sure that:

  1. you have prepared ~/.pypirc with credentials,
  2. a higher version has been set in chartmogul/__init__.py,
  3. code works python setup.py test,
  4. release works python setup.py sdist upload -r pypitest,
  5. release to production python setup.py sdist upload -r pypi,

Read full HOWTO

License

The library is available as open source under the terms of the MIT License.

The MIT License (MIT)

Copyright (c) 2016 ChartMogul Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.