chartmogul-python
provides convenient Python bindings for ChartMogul's API.
Installation | Configuration | Usage | Development | Contributing | License
This library requires Python 2.7 or 3.3 and above.
pip install chartmogul
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!'}>
The library is based on promises.
You can use it synchronously with .get()
.
Here is an example:
import chartmogul
config = chartmogul.Config('token', 'secret')
req = chartmogul.Plan.create(config, data={...})
# Now either (asynchronous)
req.then(doSomething).catch(reactOnException)
# or (synchronous)
try:
doSomething(req.get())
except Exception as ex:
reactOnException(ex)
Available methods in Import API:
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')
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')
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')
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']
})
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']
})
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='')
import chartmogul.imp
chartmogul.imp.Invoice.create(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={})
chartmogul.imp.Invoice.all(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', page=2, per_page=10)
import chartmogul.imp
chartmogul.imp.Transaction.create(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb', data={})
import chartmogul.imp
chartmogul.imp.Subscription.all(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb')
chartmogul.imp.Subscription.cancel(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb' data={'cancelled_at': ''})
chartmogul.imp.Subscription.modify(config, uuid='cus_5915ee5a-babd-406b-b8ce-d207133fb4cb' data={'cancellation_dates': []})
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.customerCount(config, data={})
chartmogul.Metrics.customerChurnRate(config, data={})
chartmogul.Metrics.mrrChurnRate(config, data={})
chartmogul.Metrics.ltv(config, data={})
chartmogul.Activity.all(config, uuid='')
chartmogul.Subscription.all(config, uuid='')
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."}}',)
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/chartmogul/chartmogul-python.
The library is available as open source under the terms of the MIT License.
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.