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:

JavaScript
Python
Copy

_160
import { ClientUtils } from "@scramjet/client-utils";
_160
import { MiddlewareClient } from "@scramjet/middleware-api-client";
_160
import { SequenceClient, InstanceClient } from "@scramjet/api-client";
_160
import { createReadStream } from "fs";
_160
import { randomBytes } from "crypto";
_160
import { PassThrough, Readable } from "stream";
_160
_160
const mwClient = new MiddlewareClient("https://api.scramjet.cloud/api/v1");
_160
const randomStream = Readable.from(randomBytes(16));
_160
_160
// Variables to change
_160
const token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
_160
//const file = createReadStream("C:\\Users\\Sam\\Downloads\\python-events.tar.gz");
_160
const file = createReadStream("C:\\Users\\Sam\\Downloads\\hello.tar.gz");
_160
const spaceId ="org-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-manager";
_160
const hubId = "sth-0"; // sth-0
_160
const sequenceId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
_160
const instanceId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
_160
_160
ClientUtils.setDefaultHeaders({ Authorization: `Bearer ${token}` });
_160
const spaceClient = mwClient.getManagerClient(spaceId);
_160
const hostClient = spaceClient.getHostClient(hubId);
_160
const sequenceClient = new SequenceClient(sequenceId, hostClient);
_160
_160
async function main() {
_160
_160
// HostClient Methods
_160
// List all Sequences with all available information
_160
console.log(await hostClient.listSequences());
_160
_160
// List all Instances with all available information
_160
console.log(await hostClient.listInstances());
_160
_160
// List both Instances and Sequences
_160
console.log(await hostClient.listEntities());
_160
_160
// List of audit stream properties
_160
(await hostClient.getAuditStream()).pipe(process.stdout);
_160
_160
// Get all the informations about logs
_160
(await hostClient.getLogStream()).pipe(process.stdout);
_160
_160
// Send a prepacked Sequence to the Hub
_160
const seqSend = await hostClient.sendSequence(file);
_160
console.log("Sequence-id: " + seqSend['_id']);
_160
_160
// Get information about a given Sequence
_160
console.log(await hostClient.getSequence(sequenceId));
_160
_160
// Delete a Sequence
_160
const delSeq = await hostClient.deleteSequence(sequenceId);
_160
console.log("Sequence Deleted...");
_160
_160
// Display information about a given Instance
_160
console.log(await hostClient.getInstanceInfo(instanceId));
_160
_160
// Return host load check
_160
console.log(await hostClient.getLoadCheck());
_160
_160
// Display details about the host version running
_160
console.log(await hostClient.getVersion());
_160
_160
// Display status and check if connected
_160
console.log(await hostClient.getStatus());
_160
_160
// Display information on the configurations
_160
console.log(await hostClient.getConfig());
_160
_160
// Sends data to a Topic name, if it doesn't exist it is being automaticaly created
_160
await hostClient.sendNamedData("TopicName", randomStream)
_160
.catch((error) => console.log(error));
_160
_160
// Get data from a given Topic
_160
(await hostClient.getNamedData("TopicName")).pipe(process.stdout);
_160
_160
// Get all Topics
_160
console.log(await hostClient.getTopics());
_160
_160
// Get information on the Instance Client
_160
console.log(hostClient.getInstanceClient("Instance-id"));
_160
_160
// Get information on the Sequence Client
_160
console.log(hostClient.getSequenceClient("Sequence-id"));
_160
_160
_160
//SequenceClient Methods
_160
// List all Sequences
_160
const lstSeq = await hostClient.listSequences();
_160
console.log(lstSeq.length);
_160
_160
// Start a Sequence
_160
const startSeq = await new SequenceClient(seqSend['_id'], hostClient).start();
_160
console.log("Instance-id: " + startSeq['_id']);
_160
_160
// List all Instances of a Sequence
_160
const sequenceClient = new SequenceClient(seqSend['_id'], hostClient); // 'Sequence-id'
_160
console.log(await sequenceClient.listInstances());
_160
_160
// Get information about a Sequence
_160
console.log(await sequenceClient.getInfo());
_160
_160
// Overwrite a Sequence
_160
const ow = new Promise((res, rej) => {
_160
sequenceClient.overwrite(file)
_160
.then(res("overwritten"))
_160
.catch(rej);
_160
});
_160
console.log(await ow);
_160
_160
_160
// InstanceClient Methods
_160
// Initialize the Instance using Instance-id and host_client
_160
const instanceClient = InstanceClient.from(startSeq['_id'], hostClient);
_160
_160
// Stop an Instance after a given number of miliseconds as parameter, and specify if the instance should be kept alive
_160
await instanceClient.stop(1000, true)
_160
.then(console.log("Stopped successfully"))
_160
.catch(console.log);
_160
_160
// Kill an Instance
_160
await instanceClient.kill({ removeImmiedietly:false })
_160
.then(console.log);
_160
_160
// Send Event to an Instance // Using events.tar.gz example
_160
(await instanceClient.getStream("log")).pipe(process.stdout);
_160
instanceClient.sendEvent("test-event", "MessageSent")
_160
.then(console.log);
_160
_160
// Get next Event from an Instance // using events.tar.gz example
_160
instanceClient.getNextEvent("test-response").then(console.log).catch(console.log);
_160
_160
// Receive Event from an Instance // using events.tar.gz example
_160
instanceClient.getEvent("test-response").then(console.log).catch(console.log);
_160
_160
// Get Event data from an Instance as a stream // using events.tar.gz example
_160
(instanceClient.getEventStream("test-response")).then((res) => res.pipe(process.stdout));
_160
_160
// Get all informations about an Instance
_160
console.log(await instanceClient.getInfo());
_160
_160
// Send stream to an Instance input/stdin and String/Readable
_160
// Create readable variable to be send as a stream using hello.tar.gz example
_160
const readable = Readable.from("String").pipe(new PassThrough(), { end:true }); // end:false for more input
_160
(await instanceClient.sendStream("input", readable)
_160
.then(console.log("Successfully Sent"))
_160
.catch((error) => console.log(error)));
_160
_160
// Send stream to an Instance through input method
_160
(await instanceClient.sendInput(Readable.from("String")
_160
.pipe(new PassThrough(), { end:true }))); // end:false for more input
_160
_160
// Send stream to an Instance through stdin method
_160
await instanceClient.sendStdin("String");
_160
_160
// Output data from an Instance
_160
(await instanceClient.getStream("output")).pipe(process.stdout);
_160
}
_160
_160
_160
const sequence = await main();

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 on Scramjet's Github repository.

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 on Scramjet's Github repository.

HostClient Methods

_36
listSequences
_36
// Returns a list of all of the Sequences on the Host
_36
listInstances
_36
// Returns a list of all of the Instances on the Host
_36
listEntities
_36
// Returns the list of all entities on the Host
_36
getAuditStream
_36
// Returns Host audit stream
_36
getLogStream
_36
// Returns the log stream of the Host
_36
sendSequence
_36
// Uploads the Sequence to the Host
_36
getSequence
_36
// Returns the details of a Sequence
_36
deleteSequence
_36
// Deletes a Sequence of a given Sequence-id
_36
getInstanceInfo
_36
// Returns the details of an Instance
_36
getLoadCheck
_36
// Returns the load-check of the Host
_36
getVersion
_36
// Returns the current version of the Host
_36
getStatus
_36
// Returns the status of a Host
_36
getConfig
_36
// Returns the public configuration of a Host
_36
sendNamedData
_36
// Sends data to a Topic name
_36
getNamedData
_36
// Returns the stream from a given Topic
_36
getTopics
_36
// Returns the list of all Topic names available on this Hub
_36
getInstanceClient
_36
// Creates InstanceClient based on the current HostClient and the Instance-id
_36
getSequenceClient
_36
// 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 on Scramjet's Github repository.

SequenceClient Methods

_10
start
_10
// Starts a Sequence with a particular Sequence-id
_10
listInstances
_10
// Returns a list of all Instances created from Sequence
_10
getInstance
_10
// Returns Instance Client for given Instance-id
_10
getInfo
_10
// Returns details of a Sequence
_10
overwrite
_10
// 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 on Scramjet's Github repository.

InstanceClient Methods

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


NOTE: For this code sample please install the following dependencies
For JavaScript methods npm install @scramjet/client-utils, @scramjet/middleware-api-client, fs, crypto, stream, promise
For Python methods pip install Scramjet-api-client, asyncio, json

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 on Scramjet's Github repository.

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 on Scramjet's Github repository.

HostClient Methods

_36
listSequences
_36
// Returns a list of all of the Sequences on the Host
_36
listInstances
_36
// Returns a list of all of the Instances on the Host
_36
listEntities
_36
// Returns the list of all entities on the Host
_36
getAuditStream
_36
// Returns Host audit stream
_36
getLogStream
_36
// Returns the log stream of the Host
_36
sendSequence
_36
// Uploads the Sequence to the Host
_36
getSequence
_36
// Returns the details of a Sequence
_36
deleteSequence
_36
// Deletes a Sequence of a given Sequence-id
_36
getInstanceInfo
_36
// Returns the details of an Instance
_36
getLoadCheck
_36
// Returns the load-check of the Host
_36
getVersion
_36
// Returns the current version of the Host
_36
getStatus
_36
// Returns the status of a Host
_36
getConfig
_36
// Returns the public configuration of a Host
_36
sendNamedData
_36
// Sends data to a Topic name
_36
getNamedData
_36
// Returns the stream from a given Topic
_36
getTopics
_36
// Returns the list of all Topic names available on this Hub
_36
getInstanceClient
_36
// Creates InstanceClient based on the current HostClient and the Instance-id
_36
getSequenceClient
_36
// 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 on Scramjet's Github repository.

SequenceClient Methods

_10
start
_10
// Starts a Sequence with a particular Sequence-id
_10
listInstances
_10
// Returns a list of all Instances created from Sequence
_10
getInstance
_10
// Returns Instance Client for given Instance-id
_10
getInfo
_10
// Returns details of a Sequence
_10
overwrite
_10
// 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 on Scramjet's Github repository.

InstanceClient Methods

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


NOTE: For this code sample please install the following dependencies
For JavaScript methods npm install @scramjet/client-utils, @scramjet/middleware-api-client, fs, crypto, stream, promise
For Python methods pip install Scramjet-api-client, asyncio, json

JavaScript
Python
CopyExpandClose

_160
import { ClientUtils } from "@scramjet/client-utils";
_160
import { MiddlewareClient } from "@scramjet/middleware-api-client";
_160
import { SequenceClient, InstanceClient } from "@scramjet/api-client";
_160
import { createReadStream } from "fs";
_160
import { randomBytes } from "crypto";
_160
import { PassThrough, Readable } from "stream";
_160
_160
const mwClient = new MiddlewareClient("https://api.scramjet.cloud/api/v1");
_160
const randomStream = Readable.from(randomBytes(16));
_160
_160
// Variables to change
_160
const token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
_160
//const file = createReadStream("C:\\Users\\Sam\\Downloads\\python-events.tar.gz");
_160
const file = createReadStream("C:\\Users\\Sam\\Downloads\\hello.tar.gz");
_160
const spaceId ="org-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-manager";
_160
const hubId = "sth-0"; // sth-0
_160
const sequenceId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
_160
const instanceId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
_160
_160
ClientUtils.setDefaultHeaders({ Authorization: `Bearer ${token}` });
_160
const spaceClient = mwClient.getManagerClient(spaceId);
_160
const hostClient = spaceClient.getHostClient(hubId);
_160
const sequenceClient = new SequenceClient(sequenceId, hostClient);
_160
_160
async function main() {
_160
_160
// HostClient Methods
_160
// List all Sequences with all available information
_160
console.log(await hostClient.listSequences());
_160
_160
// List all Instances with all available information
_160
console.log(await hostClient.listInstances());
_160
_160
// List both Instances and Sequences
_160
console.log(await hostClient.listEntities());
_160
_160
// List of audit stream properties
_160
(await hostClient.getAuditStream()).pipe(process.stdout);
_160
_160
// Get all the informations about logs
_160
(await hostClient.getLogStream()).pipe(process.stdout);
_160
_160
// Send a prepacked Sequence to the Hub
_160
const seqSend = await hostClient.sendSequence(file);
_160
console.log("Sequence-id: " + seqSend['_id']);
_160
_160
// Get information about a given Sequence
_160
console.log(await hostClient.getSequence(sequenceId));
_160
_160
// Delete a Sequence
_160
const delSeq = await hostClient.deleteSequence(sequenceId);
_160
console.log("Sequence Deleted...");
_160
_160
// Display information about a given Instance
_160
console.log(await hostClient.getInstanceInfo(instanceId));
_160
_160
// Return host load check
_160
console.log(await hostClient.getLoadCheck());
_160
_160
// Display details about the host version running
_160
console.log(await hostClient.getVersion());
_160
_160
// Display status and check if connected
_160
console.log(await hostClient.getStatus());
_160
_160
// Display information on the configurations
_160
console.log(await hostClient.getConfig());
_160
_160
// Sends data to a Topic name, if it doesn't exist it is being automaticaly created
_160
await hostClient.sendNamedData("TopicName", randomStream)
_160
.catch((error) => console.log(error));
_160
_160
// Get data from a given Topic
_160
(await hostClient.getNamedData("TopicName")).pipe(process.stdout);
_160
_160
// Get all Topics
_160
console.log(await hostClient.getTopics());
_160
_160
// Get information on the Instance Client
_160
console.log(hostClient.getInstanceClient("Instance-id"));
_160
_160
// Get information on the Sequence Client
_160
console.log(hostClient.getSequenceClient("Sequence-id"));
_160
_160
_160
//SequenceClient Methods
_160
// List all Sequences
_160
const lstSeq = await hostClient.listSequences();
_160
console.log(lstSeq.length);
_160
_160
// Start a Sequence
_160
const startSeq = await new SequenceClient(seqSend['_id'], hostClient).start();
_160
console.log("Instance-id: " + startSeq['_id']);
_160
_160
// List all Instances of a Sequence
_160
const sequenceClient = new SequenceClient(seqSend['_id'], hostClient); // 'Sequence-id'
_160
console.log(await sequenceClient.listInstances());
_160
_160
// Get information about a Sequence
_160
console.log(await sequenceClient.getInfo());
_160
_160
// Overwrite a Sequence
_160
const ow = new Promise((res, rej) => {
_160
sequenceClient.overwrite(file)
_160
.then(res("overwritten"))
_160
.catch(rej);
_160
});
_160
console.log(await ow);
_160
_160
_160
// InstanceClient Methods
_160
// Initialize the Instance using Instance-id and host_client
_160
const instanceClient = InstanceClient.from(startSeq['_id'], hostClient);
_160
_160
// Stop an Instance after a given number of miliseconds as parameter, and specify if the instance should be kept alive
_160
await instanceClient.stop(1000, true)
_160
.then(console.log("Stopped successfully"))
_160
.catch(console.log);
_160
_160
// Kill an Instance
_160
await instanceClient.kill({ removeImmiedietly:false })
_160
.then(console.log);
_160
_160
// Send Event to an Instance // Using events.tar.gz example
_160
(await instanceClient.getStream("log")).pipe(process.stdout);
_160
instanceClient.sendEvent("test-event", "MessageSent")
_160
.then(console.log);
_160
_160
// Get next Event from an Instance // using events.tar.gz example
_160
instanceClient.getNextEvent("test-response").then(console.log).catch(console.log);
_160
_160
// Receive Event from an Instance // using events.tar.gz example
_160
instanceClient.getEvent("test-response").then(console.log).catch(console.log);
_160
_160
// Get Event data from an Instance as a stream // using events.tar.gz example
_160
(instanceClient.getEventStream("test-response")).then((res) => res.pipe(process.stdout));
_160
_160
// Get all informations about an Instance
_160
console.log(await instanceClient.getInfo());
_160
_160
// Send stream to an Instance input/stdin and String/Readable
_160
// Create readable variable to be send as a stream using hello.tar.gz example
_160
const readable = Readable.from("String").pipe(new PassThrough(), { end:true }); // end:false for more input
_160
(await instanceClient.sendStream("input", readable)
_160
.then(console.log("Successfully Sent"))
_160
.catch((error) => console.log(error)));
_160
_160
// Send stream to an Instance through input method
_160
(await instanceClient.sendInput(Readable.from("String")
_160
.pipe(new PassThrough(), { end:true }))); // end:false for more input
_160
_160
// Send stream to an Instance through stdin method
_160
await instanceClient.sendStdin("String");
_160
_160
// Output data from an Instance
_160
(await instanceClient.getStream("output")).pipe(process.stdout);
_160
}
_160
_160
_160
const sequence = await main();

Was it helpful?

Didn't find information needed?

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