Management
Move Report
You can use the Mode API to move a single report or a batch of reports from one Collection to another, without having to make individual changes in the UI.
Note: Spaces have been rebranded as Collections in Mode, but the API will continue to refer to spaces
. The spaces object and resources are the same as Collections in the Mode UI.
Endpoints used
.../{workspace}/spaces
Supported methods:
- GET: returns all Collections in an Workspace
- POST: creates a new Collection
.../{workspace}/reports/{report_token}
Supported methods:
- GET: returns a report
- POST: creates a new report run
- PATCH: changes report’s Collection, description, title, or layout
How this example works
In this example, the move_report_to_space
method first makes a GET request to app.mode.com/api/{workspace}/reports/{report_token}
. This request returns information about a report (title, description, layout, etc.), which are used in the payload of a PATCH request to the same endpoint later. The helper function get_space_token
returns the space token by making a GET request to app.mode.com/api/{workspace}/spaces
. This GET request returns all Collections of which you’re a member; get_space_token
then finds the space token based on the Collection name, which you provide as an argument to the function. Back in the move_report_to_space
method, we now make a PATCH request to app.mode.com/api/{workspace}/reports/{report_token}
to change the report’s space token.
To try it yourself
- Download the example code to a
.py
file - Input the API token, API secret, Workspace name, and other custom information as needed. Follow these steps to create an API token.
- Save the .py file in your desired directory
- Open the terminal and navigate to that directory
- Run the command
python3 {file_name}.py
- All of our Python recipes require Python 3
import json
import requests
from requests.auth import HTTPBasicAuth
host = 'https://app.mode.com'
ws = 'workspace_name' # Note: workspace_name value should be all lowercase
un = 'api_token'
pw = 'api_secret'
def move_report_to_space(report_token, space_name):
url_report_info = '%s/api/%s/reports/%s' % (host, ws, report_token)
r = requests.get(url_report_info, auth=HTTPBasicAuth(un, pw))
result = r.json()
name = result['name'] or ''
layout = result['layout']
account_id = result['account_id']
desc = result['description']
published = result['_forms']['edit']['input']['report']['published']['value']
space_token = get_space_token(space_name, host, ws, un, pw)
headers = {'Content-Type': 'application/json'}
payload = {'report':{'name':name,'layout':layout,'description':desc,'account_id':account_id,'space_token':space_token,'published':published}}
r = requests.patch(url_report_info, auth=HTTPBasicAuth(un, pw), json=payload, headers=headers)
return r.status_code
def get_space_token(space_name, host, ws, un, pw):
url = '%s/api/%s/spaces' % (host, ws)
r = requests.get(url, auth=HTTPBasicAuth(un, pw))
result = r.json()
spaces = result['_embedded']['spaces']
selected_space = [s['token'] for s in spaces if s['name'] == space_name]
return selected_space[0]
move_report_to_space('report_token', 'space_name')
const request = require('request-promise');
const host = 'https://modeanalytics.com';
const ws = 'workspaceName'; // Note: workspaceName value should be all lowercase
const username = 'apiToken';
const password = 'apiSecret';
const moveReportToSpace = async (reportToken, spaceName) => {
const url = `${host}/api/${ws}/reports/${reportToken}`;
const res = await request({
url: `${host}/api/${ws}/spaces`,
json: true,
auth: { username, password },
});
const { spaces } = res._embedded;
const spaceToken = spaces.filter(space => space.name === spaceName)[0].token;
const report = await request({
url,
json: true,
auth: { username, password },
});
const {
name = '',
layout,
account_id,
description,
_forms,
} = report;
const published = _forms.edit.input.report.published.value;
const payload = {
report: {
space_token: spaceToken,
name,
layout,
description,
account_id,
published,
},
};
return await request({
url,
method: 'PATCH',
json: true,
auth: { username, password },
body: payload,
});
};
moveReportToSpace('reportToken', 'spaceName');