Management

Get All Reports

This example shows how to use the Mode API to get information about all the reports in your organization. Organization admins can use the API to retrieve all reports in public Spaces and in private Spaces of which they’re a member (but not in other members’ personal Spaces or private Spaces of which they’re not a member). This example can help you run admin analytics on your org’s reports usage, gathering data like report name and creators, schedules, users subscribed to a schedule, and so on.

Endpoints used

.../{organization}/spaces?filter=all
Supported method:

  • GET: returns all Spaces in your org, including the Spaces an admin has not joined

.../{organization}/spaces/{space_token}/reports?page=1
Supported method:

  • GET: returns all reports in a Space. This result is paginated and shows 30 reports per page

How this example works

In this example, the get_all_reports_tokens method first makes a GET request to modeanalytics.com/api/{organization}/spaces?filter=all to return a list of Spaces in the organization. Organization admins are able to access all public Spaces and private spaces they’ve joined. Once we get the space token for each Space, we then loop through each space token and make a GET request to modeanalytics.com/api/{organization}/spaces/{space_token}/reports?page=1 to get all reports in each Space. Since this endpoint result is paginated and shows 30 reports per page, we create a while loop to send GET request to each endpoint, breaking the while loop when the page returns less than 30 reports. This example returns report tokens that allow you to make subsequent GET requests to modeanalytics.com/api/{organization}/reports/{report_token} to get further information about a report.

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 get_all_reports_tokens():
  url = '%s/api/%s/spaces?filter=all' % (host, org)
  r = requests.get(url, auth=HTTPBasicAuth(un, pw))
  result = r.json()
  spaces = result['_embedded']['spaces']
  space_tokens = [s['token'] for s in spaces]
  all_tokens = []
  for s in space_tokens:
    page = 1
    report_tokens = make_request(s, page)
    all_tokens += report_tokens
    while(len(report_tokens)==30):
      page += 1
      report_tokens = make_request(s, page)
      all_tokens += report_tokens

  return all_tokens


def make_request(space_token, page):
  url = '%s/api/%s/spaces/%s/reports?page=%d' % (host, org, space_token, page)
  r = requests.get(url, auth=HTTPBasicAuth(un, pw))
  results = r.json()
  reports = results['_embedded']['reports']
  report_tokens = [r['token'] for r in reports]
  return report_tokens


get_all_reports_tokens()
const request = require('request-promise');

const host = 'https://modeanalytics.com';
const org = 'orgName';
const username = 'apiToken';
const password = 'apiSecret';

const getAllReportsTokens = async () => {
  const url = `${host}/api/${org}/spaces?filter=all`;
  const response = await request({
    url: url,
    auth: { username, password },
    json: true,
  });
  const spaces = response._embedded.spaces;
  const spaceTokens = spaces.map((space) => space.token);
  let allTokens = [];

  let page = 1;
  let tokenIndex = 0;
  do {
    let reportTokens = await makeRequest(spaceTokens[tokenIndex], page);
    allTokens = allTokens.concat(reportTokens);

    while (reportTokens.length === 30) {
      page++;
      reportTokens = await makeRequest(spaceTokens[tokenIndex], page);
      allTokens = allTokens.concat(reportTokens);
    }

    tokenIndex++;
  } while (tokenIndex < spaceTokens.length);
};

const makeRequest = async (spaceToken, page) => {
  url = `${host}/api/${org}/spaces/${spaceToken}/reports?page=${page}`;
  const response = await request({
    url: url,
    auth: { username, password },
    json: true,
  });
  const reports = response._embedded.reports;
  const reportTokens = reports.map((report) => report.token);

  return reportTokens;
};

getAllReportsTokens();
# README: This script requires that you install the HTTP gem:
#         gem install http

require 'http'
require 'json'

HOST         = 'https://modeanalytics.com'
ORGANIZATION = 'organization_name' # TODO: change to your organization's name
USERNAME     = 'api token'         # TODO: change to your API token
PASSWORD     = 'api secret'        # TODO: change to your API secret

def space_tokens
  url = "#{HOST}/api/#{ORGANIZATION}/spaces?filter=all"

  response = HTTP.basic_auth(user: USERNAME, pass: PASSWORD).get(url)
  result   = JSON.parse response

  spaces = result['_embedded']['spaces']
  spaces.map { |space| space['token'] }
end

def report_tokens(space_token: , page:)
  url = "#{HOST}/api/#{ORGANIZATION}/spaces/#{space_token}/reports?page=#{page}"

  response = HTTP.basic_auth(user: USERNAME, pass: PASSWORD).get(url)
  result   = JSON.parse response

  reports = result['_embedded']['reports']
  reports.map { |r| r['token'] }
end

def all_reports_tokens
  all_tokens   = []

  # iterate through tokens for all of your spaces
  space_tokens.each do |space_token|
    page = 1
    report_tokens = report_tokens(space_token: space_token, page: page)
    all_tokens += report_tokens

    # iterate through pagination on each
    loop do
      page += 1
      report_tokens = report_tokens(space_token: space_token, page: page)
      all_tokens += report_tokens

      puts all_tokens

      break if report_tokens.length != 30
    end
  end

  all_tokens
end

puts all_reports_tokens