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.
1
import { network } from '@oliveai/ldk';
2
3
// You won't often define a Uint8Array manually, rather you would
4
// get it back as the result from a network or filesystem call
5
const uintArray = new Uint8Array([70, 111, 111]);
6
7
network.decode(uint8Array).then((stringValue) => {
8
// Would output to the console "Foo"
9
console.log(stringValue);
10
}
Copied!

encode

Converts the passed in string into a Uint8Array.
1
import { network } from '@oliveai/ldk';
2
3
const testString = 'Foo';
4
5
network.encode(testString).then((uintValue) => {
6
// Would output to the console "[70, 111, 111]"
7
console.log(uintValue.toString());
8
};
Copied!

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.
1
import { network } from '@oliveai/ldk';
2
3
// Must go to a https address or specific IP.
4
const url = 'https://some.api.com';
5
6
// Standard HTTP request methods
7
const method = 'GET';
8
9
// Optional parameter. Headers for the request are given as
10
// a Record<string, string[]> object.
11
const headers = {
12
'some-required-header': ['Foo', 'Bar'],
13
};
14
15
// Optional parameter. The encoded value of the body of the HTTP request
16
const body = new Uint8Array([70, 111, 111]);
17
18
// Optional parameter. The time in milliseconds to await a response from
19
// the API before automatically failing the request
20
const timeoutMs = 5000;
21
22
network.httpRequest({
23
url,
24
method,
25
headers,
26
body,
27
timeoutMs,
28
}).then((httpResponse) => {
29
// The response from the server returns back a statusCode, body,
30
// and headers. 200 should mean the call was successful.
31
if (httpResponse.statusCode === 200) {
32
// The body is encoded as a Uint8Array and needs to be decoded
33
// in order to interpret it
34
network.decode(httpResponse.body).then((decodedValue) => {
35
console.log(decodedValue);
36
});
37
}
38
});
Copied!

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.
1
import { network } from '@oliveai/ldk';
2
3
// Must go to a secure address or specific IP.
4
const url = 'wss://some.api.com';
5
6
// Optional parameter. Headers for the request are given as
7
// a Record<string, string[]> object.
8
const headers = {
9
'some-required-header': ['Foo', 'Bar'],
10
};
11
12
// Optional parameter. Indicates whether the web socket server
13
// uses compression or not
14
const useCompression = false;
15
16
// Optional parameter. Describes the subprotocols used by the server
17
const subprotocols = ['value1', 'value2'];
18
19
network.webSocketConnect({
20
url,
21
headers,
22
useCompression,
23
subprotocols,
24
}).then((socket) => {
25
// The return from this promise is an object of the type Socket.
26
// Sockets have several different functions that allow bidirectional
27
// communication between it and the server. All functions are also
28
// promises that can be chained.
29
socket.setCloseHandler((error, code, text) => {
30
// Configure how to handle close...
31
});
32
33
socket.setMessageHandler((error, message) => {
34
// Configure how to handle different messages received...
35
});
36
37
socket.setPongHandler((error, msg) => {
38
// Configure how to handle the message received after pinging
39
// the server...
40
});
41
42
socket.writeMessage(message).then(() => {
43
// Write a message back to the server...
44
});
45
46
socket.ping(); // Ping the server, which should respond with a pongD
47
48
socket.close(); // Close the socket connection
49
});
Copied!
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.
1
import { network } from '@oliveai/ldk';
2
3
const callApi = (minDate, maxDate) => {
4
network.httpRequest({
5
url: `https://my.test.api/test-route?min=${minDate}&max=${maxDate}`,
6
method: 'GET',
7
}).then((httpResponse) => {
8
// Check the response to see if it was successful
9
if (httpResponse.status === 200) {
10
// If successful, then we can use the data we received.
11
network.decode(httpResponse.body).then((stringValue) => {
12
const data = JSON.parse(stringValue);
13
// Do what your loop needs to do with the data it received,
14
// whether that be displaying it back to the user or doing
15
// further calculations based on the information received.
16
});
17
} else {
18
console.error('Something went wrong');
19
}
20
});
21
};
Copied!
To use the Network aptitude, use the following permissions outline in your package.json under the ldk object.
1
...
2
"ldk": {
3
"permissions": {
4
"network": {
5
"urlDomains": [
6
{
7
"value": "string"
8
},
9
...
10
]
11
},
12
...
13
}
14
},
15
...
Copied!
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

1
{
2
"ldk": {
3
"permissions": {
4
"network": {
5
"urlDomains": [
6
{
7
"value": "your.api.com"
8
},
9
{
10
"value": "127.0.0.1"
11
},
12
{
13
"value": "*.domain.com"
14
}
15
]
16
}
17
}
18
}
19
}
Copied!
Last modified 28d ago
Copy link