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.
API
Example
Permissions

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
});
Developers will often have their own set of APIs that they will want to access with information that has been entered by a user of Olive Helps. Let's say that you are one such developer, and you have an API that returns information based on a date range that the user enters in through a Whisper.
import { network } from '@oliveai/ldk';
const callApi = (minDate, maxDate) => {
network.httpRequest({
url: `https://my.test.api/test-route?min=${minDate}&max=${maxDate}`,
method: 'GET',
}).then((httpResponse) => {
// Check the response to see if it was successful
if (httpResponse.status === 200) {
// If successful, then we can use the data we received.
network.decode(httpResponse.body).then((stringValue) => {
const data = JSON.parse(stringValue);
// Do what your loop needs to do with the data it received,
// whether that be displaying it back to the user or doing
// further calculations based on the information received.
});
} else {
console.error('Something went wrong');
}
});
};
To use the Network Aptitude, use the following permissions outline in your package.json under the ldk object.
Please see our Permissions page for more information.
...
"ldk": {
"permissions": {
"network": {
"urlDomains": [
{
"value": "string"
},
...
]
},
...
}
},
...
Each value should only include the domain (no scheme or path), and the only supported wildcard is if it starts with *.. If you do not specify a domain your Loop attempts to access, Olive Helps will reject the request. This is done to ensure a user feels confident that a Loop isn't sending or getting information to something unexpected.
Updating the network permissions requires uninstalling/reinstalling the local loop for the permission changes to take effect. This will be fixed in a future release.

Examples

{
"ldk": {
"permissions": {
"network": {
"urlDomains": [
{
"value": "your.api.com"
},
{
"value": "127.0.0.1"
},
{
"value": "*.domain.com"
}
]
}
}
}
}
Copy link