Skip to main content

SDK

SDK is a software development kit that enhances the usability of an API by providing a Middleware for the API Client to be used with the Scramjet Platform. SDK allows a user the access to the platform's resources by using an access token and assists in the creation of a HostClient, a SequenceClient and an InstanceClient. Scramjet Cloud Platform (SCP) requires authorization through Access Token which is added to the Bearer. Access Token can be easily obtained through Generate Token button found at the user's Web Panel Settings.

1. Create a MiddlewareClient:

The Scramjet Cloud Platform requests a level of authorization in the form of access Tokens which can be used in conjunction with MiddlewareClient in order to be granted access. More information can be found at Scramjet's MiddlewareClient Github repository.

MiddlewareClient Methods
import { ClientUtils } from "@scramjet/client-utils";
import { MiddlewareClient } from "@scramjet/middleware-api-client";
import { SequenceClient, InstanceClient } from "@scramjet/api-client";
import { createReadStream } from "fs";
import { randomBytes } from "crypto";
import { PassThrough, Readable } from "stream";

const mwClient = new MiddlewareClient("https://api.scramjet.cloud/api/v1");
const randomStream = Readable.from(randomBytes(16));

// Variables to change
const token = "XXXXXXXXXXXxxxxxxxxxxxXXXXXxxxxxxXxXXXXXXxXXXXxxxxxxXXxXx";
//const file = createReadStream("C:\\Users\\Sam\\Downloads\\python-events.tar.gz");
const file = createReadStream("C:\\Users\\Sam\\Downloads\\hello.tar.gz");
const spaceId ="org-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-manager";
const hubId = "sth-0"; // sth-0
const sequenceId = "xl1200x0-883l-883n-883c-1200v0o1200n";
const instanceId = "117fxlrs-fxdl-fxbr-vrsc-hd1130vrscdx";

ClientUtils.setDefaultHeaders({ Authorization: `Bearer ${token}` });
const spaceClient = mwClient.getManagerClient(spaceId);
const hostClient = spaceClient.getHostClient(hubId);
const sequenceClient = new SequenceClient(sequenceId, hostClient);

async function main() {

// HostClient Methods
// List all Sequences with all available information
console.log(await hostClient.listSequences());

// List all Instances with all available information
console.log(await hostClient.listInstances());

// List both Instances and Sequences
console.log(await hostClient.listEntities());

// List of audit stream properties
(await hostClient.getAuditStream()).pipe(process.stdout);

// Get all the information about logs
(await hostClient.getLogStream()).pipe(process.stdout);

// Send a prepacked Sequence to the Hub
const seqSend = await hostClient.sendSequence(file);
console.log("Sequence-id: " + seqSend['_id']);

// Get information about a given Sequence
console.log(await hostClient.getSequence(sequenceId));

// Delete a Sequence
const delSeq = await hostClient.deleteSequence(sequenceId);
console.log("Sequence Deleted...");

// Display information about a given Instance
console.log(await hostClient.getInstanceInfo(instanceId));

// Return host load check
console.log(await hostClient.getLoadCheck());

// Display details about the host version running
console.log(await hostClient.getVersion());

// Display status and check if connected
console.log(await hostClient.getStatus());

// Display information on the configurations
console.log(await hostClient.getConfig());

// Sends data to a Topic name, if it doesn't exist it is being automatically created
await hostClient.sendNamedData("TopicName", randomStream)
.catch((error) => console.log(error));

// Get data from a given Topic
(await hostClient.getNamedData("TopicName")).pipe(process.stdout);

// Get all Topics
console.log(await hostClient.getTopics());

// Get information on the Instance Client
console.log(hostClient.getInstanceClient("Instance-id"));

// Get information on the Sequence Client
console.log(hostClient.getSequenceClient("Sequence-id"));


//SequenceClient Methods
// List all Sequences
const lstSeq = await hostClient.listSequences();
console.log(lstSeq.length);

// Start a Sequence
const startSeq = await new SequenceClient(seqSend['_id'], hostClient).start();
console.log("Instance-id: " + startSeq['_id']);

// List all Instances of a Sequence
const sequenceClient = new SequenceClient(seqSend['_id'], hostClient); // 'Sequence-id'
console.log(await sequenceClient.listInstances());

// Get information about a Sequence
console.log(await sequenceClient.getInfo());

// Overwrite a Sequence
const ow = new Promise((res, rej) => {
sequenceClient.overwrite(file)
.then(res("overwritten"))
.catch(rej);
});
console.log(await ow);


// InstanceClient Methods
// Initialize the Instance using Instance-id and host_client
const instanceClient = InstanceClient.from(startSeq['_id'], hostClient);

// Stop an Instance after a given number of milliseconds as parameter, and specify if the instance should be kept alive
await instanceClient.stop(1000, true)
.then(console.log("Stopped successfully"))
.catch(console.log);

// Kill an Instance
await instanceClient.kill({ removeImmiedietly:false })
.then(console.log);

// Send Event to an Instance // Using events.tar.gz example
(await instanceClient.getStream("log")).pipe(process.stdout);
instanceClient.sendEvent("test-event", "MessageSent")
.then(console.log);

// Get next Event from an Instance // using events.tar.gz example
instanceClient.getNextEvent("test-response").then(console.log).catch(console.log);

// Receive Event from an Instance // using events.tar.gz example
instanceClient.getEvent("test-response").then(console.log).catch(console.log);

// Get Event data from an Instance as a stream // using events.tar.gz example
(instanceClient.getEventStream("test-response")).then((res) => res.pipe(process.stdout));

// Get all information about an Instance
console.log(await instanceClient.getInfo());

// Send stream to an Instance input/stdin and String/Readable
// Create readable variable to be send as a stream using hello.tar.gz example
const readable = Readable.from("String").pipe(new PassThrough(), { end:true }); // end:false for more input
(await instanceClient.sendStream("input", readable)
.then(console.log("Successfully Sent"))
.catch((error) => console.log(error)));

// Send stream to an Instance through input method
(await instanceClient.sendInput(Readable.from("String")
.pipe(new PassThrough(), { end:true }))); // end:false for more input

// Send stream to an Instance through stdin method
await instanceClient.sendStdin("String");

// Output data from an Instance
(await instanceClient.getStream("output")).pipe(process.stdout);
}


const sequence = await main();

2. Create a HostClient:

Firstly a user must retrieve an ID attribute of each Space <Space-id> in a list of Spaces bound to the user in order to create a Hub which is also the HostClient. Then a user is able to create a HostClient where the list of available hosts is provided by the Space Manager. Additional information on the Methods used by HostClient can be found at Scramjet's HostClient Github repository.

HostClient Methods
listSequences
// Returns a list of all of the Sequences on the Host
listInstances
// Returns a list of all of the Instances on the Host
listEntities
// Returns the list of all entities on the Host
getAuditStream
// Returns Host audit stream
getLogStream
// Returns the log stream of the Host
sendSequence
// Uploads the Sequence to the Host
getSequence
// Returns the details of a Sequence
deleteSequence
// Deletes a Sequence of a given Sequence-id
getInstanceInfo
// Returns the details of an Instance
getLoadCheck
// Returns the load-check of the Host
getVersion
// Returns the current version of the Host
getStatus
// Returns the status of a Host
getConfig
// Returns the public configuration of a Host
sendNamedData
// Sends data to a Topic name
getNamedData
// Returns the stream from a given Topic
getTopics
// Returns the list of all Topic names available on this Hub
getInstanceClient
// Creates InstanceClient based on the current HostClient and the Instance-id
getSequenceClient
// Returns information on the Sequence Client

3. Create a SequenceClient:

In order for a user to create a new SequenceClient an attribute ID of a Sequence <Sequence-id> must be selected from the HostClient. listSequences() will display all the Sequences found on a Hub. The Start method will create a new SequenceClient under the designated HostClient. Additional information on the Methods used by SequenceClient can be found at Scramjet's SequenceClient Github repository.

SequenceClient Methods
start
// Starts a Sequence with a particular Sequence-id
listInstances
// Returns a list of all Instances created from Sequence
getInstance
// Returns Instance Client for given Instance-id
getInfo
// Returns details of a Sequence
overwrite
// Overwrites the existing Sequence

4. Create an InstanceClient:

For a user to be able to create a new InstanceClient first a Sequence must be started and an <Instance-id> will be obtained by the HostClient. listInstances() method will display all running Sequences. Additional information on the Methods used by InstanceClient can be found at Scramjet's InstanceClient Github repository.

InstanceClient Methods
stop
// Sends stop command to an Instance
kill
// Sends kill command to an Instance
sendEvent
// Sends an event to an Instance
getNextEvent
// Waits and returns next event sent by an Instance
getEvent
// Returns last data from an Event given in eventName.
// Waits for an Event if it was never fired
getEventStream
// Fetches Event data as a stream
getHealth
// Returns an Instance's health
getInfo
// Returns information on an Instance
sendStream
// Sends a stream to an Instance input/stdin and String/Readable
sendInput
// Pipes given stream to an Instance input/stdin with a specific type
sendStdin
// Pipes a stream to an Instance stdin
getStream
// Returns readable stream from an Instance.
// Stream can be one of type InstanceOutputStream

tip

For this code sample please install the following dependencies

  • JavaScript methods npm install @scramjet/client-utils, @scramjet/middleware-api-client, fs, crypto, stream, promise
  • Python methods pip install Scramjet-api-client, asyncio, json

Was it helpful?

Didn't find information needed?

Join our Scramjet Community on Discord, where you can get help from our engineers directly.