Build Scramjet with Us!We're hiring

Javascript API Client

The API Client package provides a Middleware API Client to use with the Scramjet Platform. It allows access to platform resources using an access token and helps with HostClient, SequenceClient and InstanceClient creation.

Usage:

import { MiddlewareClient } from "@scramjet/middleware-api-client"; let middlewareClient = new MiddlewareClient(middlewareUrl);

Authorization

The Platform requires requests to be authorized. Access token can be easily obtained by following steps from the Get Started section. Request can be authorized by adding the Authorization header. The example below shows how to do it with the middleware-api-client.

import { MiddlewareClient } from "@scramjet/middleware-api-client"; import { ClientUtils } from "@scramjet/client-utils"; let middlewareClient = new MiddlewareClient(middlewareUrl); ClientUtils.setDefaultHeaders({ Authorization: `Bearer ${token}`, });

Users Space and HostClient

The Middleware API Client helps retrieve the list of spaces bound to the user. It has a helpful method that does just that. It's also the first step for the HostClient creation.

const spaces = await middlewareClient.getManagers(); // [ // { // "id":"org-11111111-2222-3333-4444-555555555555-manager" // } // ]

NOTE: There is usually one Space available.

Next, we have to create the HostClient. For that to be possible the Space Manager has to be requested for the list of available hosts.

const spaceClient = middlewareClient.getManagerClient(spaces[0].id); const spaceHubs = await spaceClient.getHosts(); // [ // { // "id":"sth-0", // "info":{ // "created":"2022-07-07T13:37:30.416Z", // "lastConnected":"2022-07-14T07:39:34.781Z", // "lastDisconnected":"2022-07-14T07:38:41.105Z" // }, // "healthy":true, // "isConnectionActive":true // } // ]

NOTE: There is usually one Hub available.

Having Hubs available allows for the HostClient to be created.

const hostClient = spaceClient.getHostClient(spaceHubs[0].id);

HostClient allows for listing available Instances and Sequences and for the creation of corresponding clients.

Sequence and Instance clients

For the Sequence or Instance client to be created the entity id is needed. It can be retrieved from the HostClient listing the instances and sequences. Creating HostClient in the context of Scramjet Platform was shown in the Users Space and HostClient section.

Creating SequenceClient

The SequenceClient requires the sequence id. It can be found in the list of sequences.

const sequencesList = await hostClient.listSequences(); // [ // { // "id":"11111111-2222-3333-4444-555555555555", // "config":{ // "type":"kubernetes", // "entrypointPath":"hello.js", // "version":"0.19.0", // "name":"@scramjet/hello-js", // "id":"11111111-2222-3333-4444-555555555555", // "sequenceDir":"/root/.scramjet_k8s_sequences/11111111-2222-3333-4444-555555555555", // "engines":{} // }, // "instances":[] // }, // (...) // ]

Having the Sequence id allows for the SequenceClient to be created and its methods to be called. For example start of the Sequence results in the creation of the new Instance.

const sequenceClient = hostClient.getSequenceClient(sequencesList[0].id); const instanceClient = await sequenceClient.start({ appConfig: {} });

Creating InstanceClient

The InstanceClient requires the instance id. It can be found in the list of instances.

const instancesList = await hostClient.listInstances(); // [ // { // "id":"21111111-3222-4333-5444-655555555555", // "appConfig":{}, // "sequence":"11111111-2222-3333-4444-555555555555", // "created":"2022-07-14T08:09:34.221Z", // "status":"starting" // }, // (...) // ]

Having the Instance id allows for the InstanceClient to be created and its methods to be called. For example, killing the Instance results in Instance immediate stop.

const instanceClient = hostClient.getInstanceClient(instancesList[0].id); await instanceClient.kill();

Docs

See the code documentation here:

Python API Client

The Python API Client package provides a simple API Client to use with the Scramjet Platform. It allows interaction with HostClient, SequenceClient and InstanceClient creation.

Host client

To create Host client, you need to provide url to platform, like in example below.

url = 'http://192.168.0.34:8000/api/v1' host = HostClient(url)

You can now interact with host object, e.g. to check Host version

response = asyncio.run(host.get_version()) print(f'Host version: {response}')

You can also send, start or delete Sequence with our API.

Sending Sequence

file = 'path/to/sequence.tar.gz' app_config = {} response = asyncio.run(host.send_sequence(file, app_config))

Running Sequence

To run previously sent Sequence, you need to retrieve 'id' of it from response, e.g.

import json seq_id = json.loads(response).get('id') response = asyncio.run(host.start_sequence(seq_id))

It works similarly for other endpoints.

Sequence and Instance clients

To create Sequence or Instance client, you need to provide HostClient object parameter and `id' of requested Sequence/Instance. It can be found in the list of instances/sequences.

url = 'http://192.168.0.34:8000/api/v1' host = HostClient(url) res = asyncio.run(host.list_sequences()) # [ # { # "id":"11111111-2222-3333-4444-555555555555", # "config":{ # "type":"kubernetes", # "entrypointPath":"hello.js", # "version":"0.19.0", # "name":"@scramjet/hello-js", # "id":"11111111-2222-3333-4444-555555555555", # "sequenceDir":"/root/.scramjet_k8s_sequences/11111111-2222-3333-4444-555555555555", # "engines":{} # }, # "instances":[] # }, # (...) # ] sequence_client = SequenceClient(id, host)

By analogy for instance client

url = 'http://192.168.0.34:8000/api/v1' host = HostClient(url) res = asyncio.run(host.list_instances()) # [ # { # "id":"21111111-3222-4333-5444-655555555555", # "appConfig":{}, # "sequence":"11111111-2222-3333-4444-555555555555", # "created":"2022-07-14T08:09:34.221Z", # "status":"starting" # }, # (...) # ] instance_client = InstanceClient(id, host)

Having the Instance-id or Sequence-id allows to use methods from the right clients. For example, killing the Instance results in Instance immediate stop.

instance_client = InstanceClient(id, host) asyncio.run(instance_client.kill())

Was it helpful?

Didn't find information needed?

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