Skip to main content

SDKs

This section demonstrates some of the tasks that you can complete with Macrometa SDKs. Macrometa offers the following SDKs:

Read through the complete example below to see how to perform tasks with SDKs or with APIs using Python or JavaScript calls.

Pre-requisites

  • You must have a Macrometa account with admin privileges
  • Install the SDK that corresponds to the programming language you want to use.

SDK Example

Assume your tenant name is nemo@nautilus.com and user password is xxxxx.

const jsc8 = require("jsc8");

// Email and password to authenticate client instance
const email = "nemo@nautilus.com";
const password = "xxxxxx";
const fabric = "_system";
const collectionName = "testCollection";
const streamName = "testStream";
// API key ID
const keyid = "id1";

const client = new jsc8({
url: "https://play.paas.macrometa.io",
fabricName: fabric
});

// Or create an authenticated instance with a token or API key.
// const client = new jsc8({url: "https://play.paas.macrometa.io", token: "XXXX", fabricName: fabric});
// const client = new jsc8({url: "https://play.paas.macrometa.io", apiKey: "XXXX", fabricName: fabric});
// console.log("Authentication done!");

function messageHandler (error) {
const message = {
"StatusCode ": error.statusCode,
"ErrorMessage ": error.message,
"ErrorNum ": error.errorNum
};
console.log(message);
}

async function main () {
await client
.login(email, password)
.then((e) => console.log("User authentication done!"))
.catch((error) => error);
console.log("1. Creating API key with KeyID = " + keyid);
await client
.createApiKey(keyid)
.then((apiKey) => console.log(apiKey))
.catch((error) => messageHandler(error));

console.log("\n2. Getting available API keys");
await client
.getAvailableApiKeys()
.then((apiKeys) => console.log(apiKeys.result))
.catch((error) => messageHandler(error));

console.log("\n3. Listing accessible databases for Key_ID = " + keyid);
await client
.listAccessibleDatabases(keyid)
.then((databases) => {
console.log(databases.result);
})
.catch((error) => messageHandler(error));

console.log("\n4. Listing accessible streams for Key_ID = " + keyid);
await client
.listAccessibleStreams(keyid, fabric, (full = false))
.then((streams) => {
console.log(streams.result);
})
.catch((error) => messageHandler(error));

console.log("\n5. Listing collections");
await client
.getCollections()
.then((collections) => {
console.log(collections);
})
.catch((error) => messageHandler(error));

console.log("\n6. Creating collection");
await client
.createCollection(collectionName)
.then((collection) => {
console.log("Collection created successfully");
})
.catch((error) => messageHandler(error));

console.log("\n7. Setting collection access level");
await client
.setCollectionAccessLevel(keyid, fabric, collectionName, "rw")
.then((collectionAccessLevel) => {
console.log(collectionAccessLevel);
})
.catch((error) => messageHandler(error));

console.log("\n8. Creating stream " + streamName);
await client
.createStream(streamName)
.then((stream) => console.log(stream))
.catch((error) => messageHandler(error));

console.log(
"\n9. Setting stream " + streamName + " access level to read only"
);
await client
.setStreamAccessLevel(keyid, fabric, "c8globals." + streamName, "ro")
.then((streamAccessLevel) => console.log(streamAccessLevel))
.catch((error) => messageHandler(error));

console.log(
"\n10. Setting database " + fabric + " access level to read write for Key_ID " +
keyid
);
await client
.setDatabaseAccessLevel(keyid, fabric, "rw")
.then((databaseAccessLevel) => console.log(databaseAccessLevel))
.catch((error) => messageHandler(error));

console.log("\n11. Getting collection" + collectionName + " access levels");
await client
.getCollectionAccessLevel(keyid, fabric, collectionName)
.then((collectionAccessLevel) => console.log(collectionAccessLevel))
.catch((error) => messageHandler(error));

console.log("\n12. Getting stream" + streamName + " access levels");
await client
.getStreamAccessLevel(keyid, fabric, "c8globals." + streamName)
.then((streamAccessLevel) => console.log(streamAccessLevel))
.catch((error) => messageHandler(error));

console.log("\n13. Getting database " + fabric + " access levels");
await client
.getDatabaseAccessLevel(keyid, fabric)
.then((databaseAccessLevel) => console.log(databaseAccessLevel))
.catch((error) => messageHandler(error));

console.log("\n14. Deleting database access level for Key_ID = " + keyid);
await client
.clearDatabaseAccessLevel(keyid, fabric)
.then((databaseAccessLevel) => console.log(databaseAccessLevel))
.catch((error) => messageHandler(error));

console.log("\n15. Deleting stream access level for Key_ID = " + keyid);
await client
.clearStreamAccessLevel(keyid, fabric, "c8globals." + streamName)
.then((streamAccessLevel) => console.log(streamAccessLevel))
.catch((error) => messageHandler(error));

console.log("\n16. Deleting collection access level for Key_ID = " + keyid);
await client
.clearCollectionAccessLevel(keyid, fabric, collectionName)
.then((collectionAccessLevel) => console.log(collectionAccessLevel))
.catch((error) => messageHandler(error));

console.log("\n17. Deleting " + keyid);
await client
.removeApiKey(keyid)
.then((removeApiKey) => console.log(removeApiKey))
.catch((error) => messageHandler(error));

console.log("\n18. Deleting stream " + streamName);
await client
.deleteStream("c8globals." + streamName)
.then((stream) => console.log(stream))
.catch((error) => messageHandler(error));

console.log("\n19. Deleting collection " + collectionName);
await client
.deleteCollection(collectionName)
.then((collection) => console.log(collection))
.catch((error) => messageHandler(error));
}
main()
.then()
.catch((error) => console.log(error));