Network

The Network aptitude provides the ability make HTTP requests, set up a web socket connection, and encode and decode Uint8Arrays.

Requests/connections made using this method must be made to https resources.

Requests/connections made to http resources are only allowed for 127.0.0.1/localhost when running as a local loop.

decode

Converts the passed inUint8Array object into a string.

import { network } from '@oliveai/ldk';

// You won't often define a Uint8Array manually, rather you would
// get it back as the result from a network or filesystem call
const uintArray = new Uint8Array([70, 111, 111]);

network.decode(uint8Array).then((stringValue) => {
    // Would output to the console "Foo"
    console.log(stringValue);
}

encode

Converts the passed in string into a Uint8Array.

import { network } from '@oliveai/ldk';

const testString = 'Foo';

network.encode(testString).then((uintValue) => {
    // Would output to the console "[70, 111, 111]"
    console.log(uintValue.toString());
};

httpRequest

Generates a network request to an API. Returns the response from the API. The domain that the request is going to needs to be specified in the permissions block.

import { network } from '@oliveai/ldk';

// Must go to a https address or specific IP.
const url = 'https://some.api.com';

// Standard HTTP request methods
const method = 'GET';

// Optional parameter. Headers for the request are given as 
// a Record<string, string[]> object.
const headers = {
    'some-required-header': ['Foo', 'Bar'],
};

// Optional parameter. The encoded value of the body of the HTTP request
const body = new Uint8Array([70, 111, 111]);

// Optional parameter. The time in milliseconds to await a response from 
// the API before automatically failing the request
const timeoutMs = 5000;

network.httpRequest({
    url,
    method,
    headers,
    body,
    timeoutMs,
}).then((httpResponse) => {
    // The response from the server returns back a statusCode, body, 
    // and headers. 200 should mean the call was successful.
    if (httpResponse.statusCode === 200) {
        // The body is encoded as a Uint8Array and needs to be decoded
        // in order to interpret it
        network.decode(httpResponse.body).then((decodedValue) => {
            console.log(decodedValue);
        });
    }
});

webSocketConnect

Establishes a web socket connection to the specified server. Unless you have set up the API to be a web socket connection, httpRequest() would usually be what you want.

import { network } from '@oliveai/ldk';

// Must go to a secure address or specific IP.
const url = 'wss://some.api.com';

// Optional parameter. Headers for the request are given as 
// a Record<string, string[]> object.
const headers = {
    'some-required-header': ['Foo', 'Bar'],
};

// Optional parameter. Indicates whether the web socket server 
// uses compression or not
const useCompression = false;

// Optional parameter. Describes the subprotocols used by the server
const subprotocols = ['value1', 'value2'];

network.webSocketConnect({
    url,
    headers,
    useCompression,
    subprotocols,
}).then((socket) => {
    // The return from this promise is an object of the type Socket.
    // Sockets have several different functions that allow bidirectional
    // communication between it and the server. All functions are also
    // promises that can be chained.
    socket.setCloseHandler((error, code, text) => {
        // Configure how to handle close...
    });
    
    socket.setMessageHandler((error, message) => {
        // Configure how to handle different messages received...
    });
    
    socket.setPongHandler((error, msg) => {
        // Configure how to handle the message received after pinging 
        // the server...
    });
    
    socket.writeMessage(message).then(() => {
        // Write a message back to the server...
    });
    
    socket.ping(); // Ping the server, which should respond with a pongD
    
    socket.close(); // Close the socket connection
});

Last updated