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
- JavaScript
- Python
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();
# https://github.com/scramjetorg/api-client-python/tree/main/client/client
# implement pip as a subprocess:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'Scramjet-api-client'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'scramjet-middleware-client'])
from client_utils.client_utils import ClientUtils
from middleware_client.middleware_client import MiddlewareClient
from client.sequence_client import SequenceClient
from client.instance_client import InstanceClient
import asyncio
import json
api_base = "https://api.scramjet.cloud/api/v1"
token = "XXXXXXXXXXXxxxxxxxxxxxXXXXXxxxxxxXxXXXXXXxXXXXxxxxxxXXxXx"
#filename = "C:\\Users\\Sam\\Downloads\\hello_pycharm.tar.gz"
filename = "C:\\Users\\Sam\\Downloads\\python-events.tar.gz"
ClientUtils.setDefaultHeaders({'Authorization': f'Bearer {token}'})
middleware = MiddlewareClient(api_base)
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
res = asyncio.run(middleware.get_managers())
spaces = json.loads(res)
space_client = asyncio.run(middleware.get_manager_client(spaces[0]['id']))
res = asyncio.run(space_client.get_hosts())
space_hubs = json.loads(res)
host_client = asyncio.run(space_client.get_host_client(space_hubs[0]['id']))
# HostClient Methods
# list all Sequences on a Hub
lstSeq = asyncio.run(host_client.list_sequences())
lstSeq = json.loads(lstSeq)
# list all Instances
lst_inst = asyncio.run(host_client.list_instances())
lst_inst = json.loads(lst_inst)
# Send a sequence to the Hub
seqSend = asyncio.run(host_client.send_sequence(filename))
seqSend = json.loads(seqSend)
# Start a Sequence
startSeq = SequenceClient("Sequence-id", host_client)
asyncio.run(hello_seq.start())
# Get Information about a Sequence
info_seq = asyncio.run(host_client.get_sequence("Sequence-id"))
info_seq = json.loads(info_seq)
# Delete a Sequence
delSeq = asyncio.run(host_client.delete_sequence("Sequence-id"))
# Get information on Instance
info_inst = asyncio.run(host_client.get_instance_info("Instance-id"))
info_inst = json.loads(info_inst)
# Get load check information
load_chk = asyncio.run(host_client.get_load_check())
load_chk = json.loads(load_chk)
# Get version
lst_version = asyncio.run(host_client.get_version())
lst_version = json.loads(lst_version)
# SequenceClient Methods
# list all Instances of a Sequence
lstSeq_inst = SequenceClient("Sequence-id", host_client)
lstSeq_inst = asyncio.run(lstSeq_inst.list_instances())
# Get information about Instances of a Sequence
info_seq_inst = SequenceClient('Sequence-id', host_client)
info_seq_inst = asyncio.run(info_seq_inst.get_info())
#InstanceClient Methods
# Kill an Instance
kill_inst = InstanceClient("Instance-id", host_client)
kill_inst = asyncio.run(kill_inst.kill())
kill_inst = json.loads(kill_inst)
# Send Event to an Instance // Using events.tar.gz example
seqSend = asyncio.run(host_client.send_sequence(path/to/filename))
seqSend = json.loads(seqSend)
seq_client = SequenceClient(seqSend['id'], host_client)
startSeq = asyncio.run(seq_client.start())
startSeq = json.loads(startSeq)
inst_client = InstanceClient(startSeq['id'], host_client)
event_emit_inst = asyncio.run(inst_client.send_event(event_name='EventName', message='MessageSent'))
# Receive Event from an Instance // using events.tar.gz example
inst_client = InstanceClient("Sequence-id", host_client)
event_on_inst = asyncio.run(inst_client.get_event(id='EventResponse'))
# Get health
get_health = asyncio.run(inst_client.get_health())
get_health = json.loads(get_health)
# Get information
get_info = asyncio.run(inst_client.get_info())
get_info = json.loads(get_info)
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
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