
Delighted API Python Client
Official Python client for the Delighted API.
Installation
pip install --upgrade delighted
or
easy_install --upgrade delighted
Upgrading from delighted-python
If you previously used the python package named delighted-python
, please note that the package name is now just delighted
.
Configuration
To get started, you need to configure the client with your secret API key.
import delighted
delighted.api_key = 'YOUR_API_KEY'
For further options, read the advanced configuration section.
Note: Your API key is secret, and you should treat it like a password. You can find your API key in your Delighted account, under Settings > API.
Usage
Adding/updating people and scheduling surveys:
person1 = delighted.Person.create(email='foo+test1@delighted.com')
person2 = delighted.Person.create(email='foo+test2@delighted.com', delay=60)
person3 = delighted.Person.create(email='foo+test3@delighted.com', send=False)
person4 = delighted.Person.create(
email='foo+test4@delighted.com',
name='Joe Bloggs',
properties={'customer_id': 123, 'country': 'USA',
'question_product_name': 'The London Trench'},
delay=30)
updated_person1 = delighted.Person.create(email='foo+test1@delighted.com',
name='James Scott', send=False)
Unsubscribing people:
delighted.Unsubscribe.create(person_email='foo+test1@delighted.com')
Listing people:
people = delighted.Person.list()
while True:
try:
for person in people.auto_paging_iter():
except TooManyRequestsError as e:
e.retry_after
continue
people = delighted.Person.list(auto_handle_rate_limits=True)
for person in people.auto_paging_iter():
Listing people who have unsubscribed:
delighted.Unsubscribe.all()
delighted.Unsubscribe.all(page=2)
Listing people whose emails have bounced:
delighted.Bounce.all()
delighted.Bounce.all(page=2)
Deleting a person and all of the data associated with them:
delighted.Person.delete(id=42)
delighted.Person.delete(email='test@example.com')
delighted.Person.delete(phone_number='+14155551212')
Deleting pending survey requests
delighted.SurveyRequest.delete_pending(person_email='foo+test1@delighted.com')
Adding survey responses:
survey_response1 = delighted.SurveyResponse.create(person=person1.id,
score=10)
survey_response2 = delighted.SurveyResponse.create(person=person1.id,
score=5,
comment='Really nice.')
Retrieving a survey response:
survey_response3 = delighted.SurveyResponse.retrieve('123')
Updating survey responses:
survey_response4 = delighted.SurveyResponse.retrieve('234')
survey_response4.score = 10
survey_response4.save()
survey_response4.person_properties = {'segment': 'Online'}
survey_response4.save()
survey_response4.person = '321'
survey_response4.save()
Listing survey responses:
survey_responses_page1 = delighted.SurveyResponse.all()
survey_responses_page2 = delighted.SurveyResponse.all(page=2)
survey_responses_page1_expanded = delighted.SurveyResponse.all(expand=['person'])
survey_responses_page1_expanded[0].person
survey_responses_page1_trend = delighted.SurveyResponse.all(trend='123')
survey_responses_page1_desc = delighted.SurveyResponse.all(order='desc')
import pytz
timezone = pytz.timezone('America/Chicago')
filtered_survey_responses = delighted.SurveyResponse.all(
page=5,
per_page=100,
since=timezone.localize(datetime.datetime(2014, 3, 1)),
until=timezone.localize(datetime.datetime(2014, 4, 30))
)
Retrieving metrics:
metrics = delighted.Metrics.retrieve()
metrics = delighted.Metrics.retrieve(trend='123')
import pytz
timezone = pytz.timezone('America/Chicago')
metrics = delighted.Metrics.retrieve(
since=timezone.localize(datetime.datetime(2013, 10, 1)),
until=timezone.localize(datetime.datetime(2013, 11, 1))
)
Managing Autopilot:
autopilot = delighted.AutopilotConfiguration.retrieve('email')
people_autopilot = delighted.AutopilotMembership.forEmail().list(auto_handle_rate_limits=True)
for person in people_autopilot.auto_paging_iter():
autopilot = delighted.AutopilotMembership.forEmail().create(person_email='test@example.com')
properties = {'customer_id': 123, 'country': 'USA', 'question_product_name': 'The London Trench'}
autopilot = delighted.AutopilotMembership.forSms().create(person_phone_number='+14155551212', properties=properties)
delighted.AutopilotMembership.forSms().delete(person_id=42)
delighted.AutopilotMembership.forEmail().delete(person_email='test@example.com')
delighted.AutopilotMembership.forSms().delete(person_phone_number='+14155551212')
Rate limits
If a request is rate limited, a TooManyRequestsError
exception is raised. You can rescue that exception to implement exponential backoff or retry strategies. The exception provides a retry_after
attribute to tell you how many seconds you should wait before retrying. For example:
try:
metrics = delighted.Metrics.retrieve()
except delighted.errors.TooManyRequestsError as err:
retry_after_seconds = err.retry_after
Advanced configuration & testing
The following options are configurable for the client:
delighted.api_key
delighted.api_base_url
delighted.http_adapter
By default, a shared instance of delighted.Client
is created lazily in delighted.get_shared_client()
. If you want to create your own client, perhaps for test or if you have multiple API keys, you can:
import delighted
from delighted import Client
client = Client(api_key='API_KEY',
api_base_url='https://api.delighted.com/v1/',
http_adapter=HTTPAdapter())
metrics_from_custom_client = delighted.Metrics.retrieve(client=client)
delighted.shared_client = delighted.Client(
api_key='API_KEY',
api_base_url='https://api.delighted.com/v1/',
http_adapter=delighted.HTTPAdapter()
)
metrics_from_custom_shared_client = delighted.Metrics.retrieve()
Supported versions
- 2.6+, 3.3+ (PyPy supported)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Run the tests (
tox
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Releasing
- Bump the version in
delighted/__init__.py
. - Update the README and CHANGELOG as needed.
- Tag the commit for release.
- Create the distribution
python setup.py sdist
- Update the package against PyPI's test server with twine
twine upload --repository-url https://test.pypi.org/legacy/ dist/TEST_PACKAGE_NAME
. - If (4 and 5) work, repeat all steps, then push to PyPI's live servers with
twine upload dist/PACKAGE_NAME
.
Author
Originally by Jason Pearson. Graciously transfered and now officially maintained by Delighted.