Management

Move Report

You can use the Mode API to move a single report or a batch of reports from one Space to another, without having to make individual changes in the UI.

Endpoints used

.../{organization}/spaces
Supported methods:

  • GET: returns all Spaces in an organization
  • POST: creates a new Space

.../{organization}/reports/{report_token}
Supported methods:

  • GET: returns a report
  • POST: creates a new report run
  • PATCH: changes report’s Space, 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/{organization}/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/{organization}/spaces. This GET request returns all Spaces of which you’re a member; get_space_token then finds the space token based on the Space 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/{organization}/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, organization name, and other custom information as needed. An API token and secret can be generated here
  • 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://modeanalytics.com'
org = 'org_name'
un = 'api_token'
pw = 'api_secret'


def move_report_to_space(report_token, space_name):
  url_report_info = '%s/api/%s/reports/%s' % (host, org, 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, org, 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, org, un, pw):
  url = '%s/api/%s/spaces' % (host, org)
  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 org = 'orgName';
const username = 'apiToken';
const password = 'apiSecret';

const moveReportToSpace = async (reportToken, spaceName) => {
  const url = `${host}/api/${org}/reports/${reportToken}`;

  const res = await request({
    url: `${host}/api/${org}/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');