Retrieve installs via GraphQL API

Hosted app installation information can be obtained from obniz cloud API via API.
This eliminates the need to manage device information in the server as you can retrieve who and what device the app was installed on from the program running on the server via the API.

SDK

The SDK for manipulating obniz cloud API is available for Node.js.

https://github.com/obniz/obniz-cloud-sdk

You can install it from npm.

npm i obniz-cloud-sdk

tokens

When you use the API, you will find an "App Token" in the app settings screen, which is an alphanumeric string starting with apptoken_. You can get information about your app and installation by using this.

Get installation information.

The installation can be obtained from the app of query.

// Runkit Example
const getSdk = require('obniz-cloud-sdk').getSdk
const sdk = getSdk("apptoken_XXXXXXXXXXX");
const result = await sdk.app();
const installs = result.app.installs.edges;
for (const install of installs) {
  console.log(`obniz ${install.id}\n access_token ${install.access_token}\n configs ${install.configs}`)
}

The installation information you can get is in the following format. For more information, please refer to the obniz GraphQL API documentation.

  {
    id: '0000-0000',
    access_token: null,
    description: 'Tokyo',
    metadata: '{}',
    hardware: 'm5stack_basic',
    os: 'm5stack_basic',
    osVersion: '3.3.0',
    region: 'jp',
    status: 'active',
    createdAt: '2019-12-21T13:37:54.342Z',
    user: {
      id: 'usr_N8EbB9',
      name: 'Yuki Sato',
      email: 'supportteam@obniz.com',
      picture: 'https://s3-ap-northeast-1.amazonaws.com/obniz-uploads/136dd0eb998413371601912204404',
      plan: 'lite'
    },
    configs: '{}'
  }

The configs object is assigned the value of the installation configuration.

Since the API has an upper limit on the number of calls at one time, you need to call multiple times to get all the results.

// Runkit Example
const getSdk = require('obniz-cloud-sdk').getSdk

// apiではインストールの取得件数に上限があるため、全部の取得には複数回のcallが必要
async function getAllInstalls(token) {
    const sdk = getSdk(token);
    const allInstalls = [];
    let skip = 0;
    while (true) {
      // 取得
      const result = await sdk.app({ skip });
      for (const edge of result.app.installs.edges) {
        allInstalls.push(edge.node);
      }
      if (!result.app.installs.pageInfo.hasNextPage) {
        break;
      }
      skip += result.app.installs.edges.length;
    }
    return allInstalls;
}

async function showAllDevices() {
  // get All installed devices with configrations
  const installs = await getAllInstalls("apptoken_XXXXXXXXXXX");
  for (const install of installs) {
    console.log(`obniz ${install.id}\n access_token ${install.access_token}\n configs ${install.configs}`)
  }
}

showAllDevices();

If the number of installed devices is small (a few hundred), it is possible to load all devices when the Nodejs program is started on the server without saving them.

If there are more devices than that, it would take a lot of time just to read all of them, so it is preferable to store them in the database and refresh them when necessary.