Created
September 26, 2019 12:04
-
-
Save bafonins/39eee90c3f7af119121d19d140194eaa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const assert = require('assert') | |
| const TTN = require('ttn-lw').default | |
| const IS = process.env.TTN_LW_CONSOLE_UI_IS_BASE_URL || 'http://localhost:1885' | |
| const AS = process.env.TTN_LW_CONSOLE_UI_AS_BASE_URL || 'http://localhost:1885' | |
| const token = process.env.access_token | |
| const ttnClient = new TTN(token, { | |
| stackConfig: { is: `${IS}/api/v3`, as: `${AS}/api/v3` }, | |
| connectionType: 'http', | |
| proxy: false, | |
| }) | |
| ;(async function() { | |
| try { | |
| const userId = 'admin' | |
| const appId = 'test-app' | |
| const mqttPubsubId = 'test-pubsub' | |
| const mqttPubsubBaseTopic = 'base-topic' | |
| const mqttPubsub = { | |
| ids: { | |
| application_ids: { | |
| application_id: appId, | |
| }, | |
| pub_sub_id: mqttPubsubId, | |
| }, | |
| base_topic: mqttPubsubBaseTopic, | |
| format: 'json', | |
| mqtt: { | |
| server_url: 'mqtts://example.com', | |
| client_id: 'client', | |
| username: 'username', | |
| password: 'password', | |
| subscribe_qos: 'AT_MOST_ONCE', | |
| publish_qos: 'AT_MOST_ONCE', | |
| use_tls: false, | |
| }, | |
| } | |
| const natsPubsubId = 'test-pubsub-2' | |
| const natsPubsubBaseTopic = 'base-topic-2' | |
| const natsPubsub = { | |
| ids: { | |
| application_ids: { | |
| application_id: appId, | |
| }, | |
| pub_sub_id: natsPubsubId, | |
| }, | |
| base_topic: natsPubsubBaseTopic, | |
| format: 'json', | |
| nats: { | |
| server_url: 'nats://derek:secretpassword@demo.nats.io:4222', | |
| }, | |
| } | |
| const app = { | |
| ids: { | |
| application_id: appId, | |
| }, | |
| created_at: '2018-08-29T14:00:20.793Z', | |
| updated_at: '2018-08-29T14:00:20.793Z', | |
| name: 'string', | |
| description: 'string', | |
| } | |
| await ttnClient.Applications.create(userId, app) | |
| console.log(`created application:${appId}`) | |
| await ttnClient.Applications.PubSubs.create(appId, mqttPubsub) | |
| console.log(`created mqtt pubsub:${mqttPubsubId}`) | |
| await ttnClient.Applications.PubSubs.create(appId, natsPubsub) | |
| console.log(`created nats pubsub:${natsPubsubId}`) | |
| const { pubsubs, totalCount } = await ttnClient.Applications.PubSubs.getAll(appId, [ | |
| 'base_topic', | |
| ]) | |
| assert(totalCount === 2) | |
| assert(pubsubs[0].ids.pub_sub_id === mqttPubsubId) | |
| assert(pubsubs[0].base_topic === mqttPubsubBaseTopic) | |
| assert(pubsubs[1].ids.pub_sub_id === natsPubsubId) | |
| assert(pubsubs[1].base_topic === natsPubsubBaseTopic) | |
| const nats = await ttnClient.Applications.PubSubs.getById(appId, natsPubsubId, ['base_topic']) | |
| console.log('got nats pubsub:', nats) | |
| assert(natsPubsub.format === nats.format) | |
| assert(natsPubsubBaseTopic === nats.base_topic) | |
| const mqtt = await ttnClient.Applications.PubSubs.getById(appId, mqttPubsubId, ['base_topic']) | |
| console.log('got mqtt pubsub:', mqtt) | |
| assert(mqttPubsub.format === mqtt.format) | |
| assert(mqttPubsubBaseTopic === mqtt.base_topic) | |
| const updatedNatsPubsubBaseTopic = 'updated-nats-base-topic' | |
| const natsPatch = { base_topic: updatedNatsPubsubBaseTopic } | |
| const updatedNats = await ttnClient.Applications.PubSubs.updateById( | |
| appId, | |
| natsPubsubId, | |
| natsPatch, | |
| ) | |
| console.log('updated nats pubsub:', updatedNats) | |
| assert(natsPubsub.format === updatedNats.format) | |
| assert(updatedNatsPubsubBaseTopic === updatedNats.base_topic) | |
| const updatedMqttPubsubBaseTopic = 'updated-mqtt-base-topic' | |
| const mqttPatch = { base_topic: updatedMqttPubsubBaseTopic } | |
| const updatedMqtt = await ttnClient.Applications.PubSubs.updateById( | |
| appId, | |
| mqttPubsubId, | |
| mqttPatch, | |
| ) | |
| console.log('updated mqtt pubsub:', updatedMqtt) | |
| assert(mqttPubsub.format === updatedMqtt.format) | |
| assert(updatedMqttPubsubBaseTopic === updatedMqtt.base_topic) | |
| await ttnClient.Applications.PubSubs.deleteById(appId, natsPubsubId) | |
| console.log('deleted nats pubsub') | |
| let result = await ttnClient.Applications.PubSubs.getAll(appId, ['base_topic']) | |
| assert(result.totalCount === 1) | |
| assert(result.pubsubs[0].ids.pub_sub_id === mqttPubsubId) | |
| await ttnClient.Applications.PubSubs.deleteById(appId, mqttPubsubId) | |
| console.log('deleted mqtt pubsub') | |
| result = await ttnClient.Applications.PubSubs.getAll(appId, ['base_topic']) | |
| assert(result.totalCount === 0) | |
| } catch (error) { | |
| console.log(error) | |
| } | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment