Package: delighted
Delighted API Python ClientOfficial Python client for theDelighted API.Installationpip install --upgrade delightedoreasy_install --upgrade delightedUpgrading from delighted-pythonIf you previously used the python package nameddelighted-python, please note that the package name is now justdelighted.ConfigurationTo get started, you need to configure the client with your secret API key.importdelighteddelighted.api_key='YOUR_API_KEY'For further options, read theadvanced 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, underSettings>API.UsageAdding/updating people and scheduling surveys:# Add a new person, and schedule a survey immediatelyperson1=delighted.Person.create(email='foo+test1@delighted.com')# Add a new person, and schedule a survey after 1 minute (60 seconds)person2=delighted.Person.create(email='foo+test2@delighted.com',delay=60)# Add a new person, but do not schedule a surveyperson3=delighted.Person.create(email='foo+test3@delighted.com',send=False)# Add a new person with full set of attributes, including a custom question# product name, and schedule a survey with a 30 second delayperson4=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)# Update an existing person (identified by email), adding a name, without# scheduling a surveyupdated_person1=delighted.Person.create(email='foo+test1@delighted.com',name='James Scott',send=False)Unsubscribing people:# Unsubscribe an existing persondelighted.Unsubscribe.create(person_email='foo+test1@delighted.com')Listing people:# List all people, auto pagination# Note: Make sure to handle the possible rate limits errorpeople=delighted.Person.list()whileTrue:try:forpersoninpeople.auto_paging_iter():# Do something with personexceptTooManyRequestsErrorase:# Indicates how long to wait (in seconds) before making this request againe.retry_aftercontinue# For convenience, this method can use a sleep to automatically handle rate limitspeople=delighted.Person.list(auto_handle_rate_limits=True)forpersoninpeople.auto_paging_iter():# Do something with personListing people who have unsubscribed:# List all people who have unsubscribed, 20 per page, first 2 pagesdelighted.Unsubscribe.all()delighted.Unsubscribe.all(page=2)Listing people whose emails have bounced:# List all people whose emails have bounced, 20 per page, first 2 pagesdelighted.Bounce.all()delighted.Bounce.all(page=2)Deleting a person and all of the data associated with them:# Delete by person iddelighted.Person.delete(id=42)# Delete by email addressdelighted.Person.delete(email='test@example.com')# Delete by phone number (must be E.164 format)delighted.Person.delete(phone_number='+14155551212')Deleting pending survey requests# Delete all pending (scheduled but unsent) survey requests for a person, by email.delighted.SurveyRequest.delete_pending(person_email='foo+test1@delighted.com')Adding survey responses:# Add a survey response, score onlysurvey_response1=delighted.SurveyResponse.create(person=person1.id,score=10)# Add *another* survey response (for the same person), score and commentsurvey_response2=delighted.SurveyResponse.create(person=person1.id,score=5,comment='Really nice.')Retrieving a survey response:# Retrieve an existing survey responsesurvey_response3=delighted.SurveyResponse.retrieve('123')Updating survey responses:# Update a survey response scoresurvey_response4=delighted.SurveyResponse.retrieve('234')survey_response4.score=10survey_response4.save()# <delighted.SurveyResponse object at 0xabc123># Update (or add) survey response propertiessurvey_response4.person_properties={'segment':'Online'}survey_response4.save()# <delighted.SurveyResponse object at 0xabc123># Update person who recorded the survey responsesurvey_response4.person='321'survey_response4.save()# <delighted.SurveyResponse object at 0xabc123>Listing survey responses:# List all survey responses, 20 per page, first 2 pagessurvey_responses_page1=delighted.SurveyResponse.all()survey_responses_page2=delighted.SurveyResponse.all(page=2)# List all survey responses, 20 per page, expanding person objectsurvey_responses_page1_expanded=delighted.SurveyResponse.all(expand=['person'])survey_responses_page1_expanded[0].person# <delighted.Person object at 0xabc123># List all survey responses, 20 per page, for a specific trend (ID: 123)survey_responses_page1_trend=delighted.SurveyResponse.all(trend='123')# List all survey responses, 20 per page, in reverse chronological order (newest first)survey_responses_page1_desc=delighted.SurveyResponse.all(order='desc')# List all survey responses, 100 per page, page 5, with a time rangeimportpytztimezone=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:# Get current metrics, 30-day simple moving average, from most recent responsemetrics=delighted.Metrics.retrieve()# Get current metrics, 30-day simple moving average, from most recent response,# for a specific trend (ID: 123)metrics=delighted.Metrics.retrieve(trend='123')# Get metrics, for given time rangeimportpytztimezone=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:# Get Autopilot configuration for the `email` platformautopilot=delighted.AutopilotConfiguration.retrieve('email')# List people in AutopilotMembership for the `email` platformpeople_autopilot=delighted.AutopilotMembership.forEmail().list(auto_handle_rate_limits=True)forpersoninpeople_autopilot.auto_paging_iter():# Do something with person# Add people to AutopilotMembershipautopilot=delighted.AutopilotMembership.forEmail().create(person_email='test@example.com')# Add people to AutopilotMembership, with a full set of attributesproperties={'customer_id':123,'country':'USA','question_product_name':'The London Trench'}autopilot=delighted.AutopilotMembership.forSms().create(person_phone_number='+14155551212',properties=properties)# Delete by person iddelighted.AutopilotMembership.forSms().delete(person_id=42)# Delete by email addressdelighted.AutopilotMembership.forEmail().delete(person_email='test@example.com')# Delete by phone number (must be E.164 format)delighted.AutopilotMembership.forSms().delete(person_phone_number='+14155551212')Rate limitsIf a request is rate limited, aTooManyRequestsErrorexception is raised. You can rescue that exception to implement exponential backoff or retry strategies. The exception provides aretry_afterattribute to tell you how many seconds you should wait before retrying. For example:try:metrics=delighted.Metrics.retrieve()exceptdelighted.errors.TooManyRequestsErroraserr:retry_after_seconds=err.retry_after# wait for retry_after_seconds before retrying# add your retry strategy here ...Advanced configuration & testingThe following options are configurable for the client:delighted.api_keydelighted.api_base_url# default: 'https://api.delighted.com/v1/'delighted.http_adapter# default: delighted.HTTPAdapterBy default, a shared instance ofdelighted.Clientis created lazily indelighted.get_shared_client(). If you want to create your own client, perhaps for test or if you have multiple API keys, you can:# Create an custom client instance, and pass as last argument to resource actionsimportdelightedfromdelightedimportClientclient=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)# Or, you can set Delighted.shared_client yourselfdelighted.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 versions2.6+, 3.3+ (PyPy supported)ContributingFork itCreate 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 RequestReleasingBump the version indelighted/__init__.py.Update the README and CHANGELOG as needed.Tag the commit for release.Create the distributionpython setup.py sdistUpdate the package against PyPI's test server withtwinetwine 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 withtwine upload dist/PACKAGE_NAME.AuthorOriginally byJason Pearson. Graciously transfered and now officially maintained by Delighted.