Join us on July 16th as we explore real-world Reinforcement Learning Learn more

How to programmatically set email recipients in a "Send email" reporter using the API?

How to programmatically set email recipients in a "Send email" reporter using the API?

The Dataiku DSS API allows the users to programmatically interact with the product.
It can be very useful when having to apply some operations that would be quite repetitive to achieve through the UI, or when it comes to automating some interactions with DSS.

In this use case, let's consider we have a project containing multiple scenarios, and for all of them, we want to add a new recipient for all the existing "Send email" reporters.

We're going to achieve that with a python script that will be executed from inside of DSS, but the same logic can be used from outside of DSS.

The idea of this operation is to list all the existing scenarios in a specified project then search for all the "Send email" reporters, then retrieve the list of recipients and then finally update the list if the new recipient doesn't already exist.

To interact with scenarios, we first need to access the REST API client.
From a script running inside of DSS, it's pretty straight forward using dataiku.api_client():

import dataiku
client = dataiku.api_client()

From outside of DSS, you need additional steps to configure how to access the instance. Please refer to the article Using the APIs outside of DSS from the reference documentation to know more.

Then, let's create a variable to store the new recipient email address:

new_recipient_email_address = "john.doe@here.com"

The next step is to retrieve the project and the list of scenario it contains:

project = client.get_project("PROJECT_KEY")
scenarios_list = project.list_scenarios()

list_scenarios() returns a dictionary, let's use the id property to retrieve a handle to interact with the scenario:

for scenario_metadata in scenarios_list:
scenario = project.get_scenario(scenario_metadata['id'])

Let's then retrieve the scenario definition to interact with the scenario attributes:

    scenario_definition = scenario.get_definition(with_status=False)

Now, it's time to iterate on all the existing reporters, check if they are "Send email" reporters, if so then retrieve the existing list of recipients, and add the new recipient email address when missing:

    update_scenario = False
for i in range(0, len(scenario_definition['reporters'])):
if scenario_definition['reporters'][i]['messaging']['type'] == "mail-scenario":
recipients = [recipient.strip() for recipient in scenario_definition['reporters'][i]['messaging']['configuration']['recipient'].split(',')]
if not new_recipient_email_address in recipients:
recipients.append(new_recipient_email_address)
scenario_definition['reporters'][i]['messaging']['configuration']['recipient'] = ', '.join(recipients)
update_scenario = True
print("Updating recipient for mail reporter \"{}\" of scenario \"{}\"".format(scenario_definition['reporters'][i]['name'], scenario_metadata['name']))

Finally, if we've edited the list of recipients, let's update the definition of the scenario :

    if update_scenario:
scenario.set_definition(scenario_definition,with_status=False)

Final code sample

import dataiku

client = dataiku.api_client()

project = client.get_project("PROJECT_KEY")
scenarios_list = project.list_scenarios()

new_recipient_email_address = "john.doe@here.com"

for scenario_metadata in scenarios_list:
    scenario = project.get_scenario(scenario_metadata['id'])
    scenario_definition = scenario.get_definition(with_status=False)
    
    update_scenario = False
    for i in range(0, len(scenario_definition['reporters'])):
        if scenario_definition['reporters'][i]['messaging']['type'] == "mail-scenario":
            recipients = [recipient.strip() for recipient in scenario_definition['reporters'][i]['messaging']['configuration']['recipient'].split(',')]
            if not new_recipient_email_address in recipients:
                recipients.append(new_recipient_email_address)
                scenario_definition['reporters'][i]['messaging']['configuration']['recipient'] = ', '.join(recipients)
                update_scenario = True
                print("Updating recipient for mail reporter \"{}\" of scenario \"{}\"".format(scenario_definition['reporters'][i]['name'], scenario_metadata['name']))
    if update_scenario:
        scenario.set_definition(scenario_definition,with_status=False)

 This code sample is also available on our GitHub repository.

What's next?

  • For more details about how to use the API, see Python APIs
  • For more details about how to interact with projects using the python API, see Managing projects
  • For more details about how to interact with scenarios using the python API, see Managing scenarios
Labels (2)
Version history
Revision #:
4 of 4
Last update:
3 weeks ago
Updated by: