Internet documentation

Introductions

The Internet API is a completely asynchronous HTTP and web socket client that makes heavy use of events.

The internet api exists in the global space as "internet" or "_G.internet"

Lookup table

Includes the functions in the base api for sending requests, and websocket send and close functions provided by the 'WebsocketOpened' event.

GET(url, headers = {}) Sends a http GET request, and mints an ID to track the response
POST(url, headers = {}, body = "") Sends a http POST request, and mints an ID to track the response
CreateWebsocket(uri, headers = {}) Clears all event queues
hasAccess() Checks if the internet API is enabled in the mod config
isReady() Checks if the api is ready to receive outgoing messages

'WebsocketOpened' functions

send(body, binaryMode) Attempts to send a message to the opened websocket
close() Closes the websocket with error code 1000

Functions in detail

GET(url, headers = {})

Adds the given HTTP GET request to the outgoing queue, and returns the id of the inbound response

Parameters: url[String, url format], headers[Table of string pairs] default value {} Returns: Integer

POST(url, headers = {}, body = "")

Adds the given HTTP POST request to the outgoing queue, and returns the id of the inbound response

Parameters: url[String, url format], headers[Table of string pairs] default value {}, body[String or Bytes] Returns: Integer

CreateWebsocket(uri, headers = {})

Attempts to start the life cycle of a web socket, and mints an id for all further correspondence (note only 5 sockets can be active at once by default)
Will register an "WebsocketOpened" once complete, there may be a delay between the function finishing execution and the event being registered
While the HTTP request functions provide almost all errors through 'HttpResponse' events, CreateWebsocket is more likely to throw errors directly

Parameters: url[String], headers[Table of string pairs] default value {} Returns: Integer

hasAccess()

Returns if internet connectivity is enabled in the mod configuration

Parameters: None Returns: Boolean

isReady()

Returns if the computer is ready to receive outgoing requests, including websocket messages

Parameters: None Returns: Boolean

'WebsocketOpened' functions

send(body, binaryMode)

Sends the attached message to the opened websocket, with a toggle for binary or plain text modes
Will send 'WebsocketSendSuccess' or 'WebsocketSendFailure' event depending on if the operation was successful

Parameters: body[String or Bytes], binaryMode[Boolean] Returns: None

close()

Returns if the computer is ready to receive outgoing requests, including websocket messages

Parameters: None Returns: None

Event table

A large part of the internet API, especially Websockets, is dependent on events in the Network category

HttpResponse [Network]

HttpResponse[String] id[Int] status code[Int] status message[String] >
Headers[Table of Strings] Body[String/Bytes or nil]
Documents the HTTP response to a request, exactly one response event is guaranteed to be generated for any http request that don't complete exceptionally (throw an error).
The id is the same number as it returned by the request generating function, and is randomly generated.
The status code and message will generally follow HTTP status code protocol, however some linix TCP error codes have been used in some client exceptions (Documented below)

WebsocketOpened [Network]

WebsocketOpened[String] id[Int] Send Function[Function] Close Function[Function]
The event that signals a Websocket has been successfully created, shares the random ID as the function that requested the Websocket and provides methods for interacting with the open Websocket (Documented above)
May also cover networking errors encountered while trying to connect to the webserver.

WebsocketClosed [Network]

WebsocketClosed[String] id[Int] status code[Int] reason[String]
The event that signals a Websocket has been closed, shares the random ID as the function that requested the Websocket and contained the associated error code and reason

WebsocketMessage [Network]

WebsocketMessage[String] id[Int] Body[String/Bytes] binary[Boolean]
The event that signals a Websocket has received a message from the server, shares the random ID as the function that requested the Websocket and describes if the message was sent as binary or plain UTF-8

WebsocketSendSuccess [Network]

WebsocketSendSuccess[String] id[Int]
The event that signals a Websocket message has been successfully sent, shares the random ID as the function that requested the Websocket

WebsocketSendFailure [Network]

WebsocketSendFailure[String] id[Int] reason[String]
The event that signals a Websocket message has failed to be sent, shares the random ID as the function that requested the Websocket

Status Codes

Examples of common error codes, does not contain ever possible error code except for the TCP ones, those are inclusive to what API fires

Code Name Explanation
# --- PROPRIETARY TCP LINIX CODES, ONLY FIRED BY API FOR HTTP REQUESTS
105 ENOBUFSNO Computer not ready to receive requests
110 ETIMEDOUT Client couldn't connect in timeout period
111 ECONNREFUSED Internet access not enabled in mod config
# --- NORMAL COMMON HTTP CODES
200 OK No issue
400 TIMEOUT Client or server timed out
400 BAD REQUEST Request malformed in some way
404 NOT FOUND Target resource not found
403 FORBIDDEN You don't have access to the resource your requesting
# --- WEBSOCKET ERROR CODES
1000 NORMAL CLOSURE Socket connection has reached the end of its usefulness
1001 GOING AWAY The client/server is going away
1002 PROTOCOL ERROR Message doesn't follow expected guidelines/format
1003 UNSUPPORTED DATA Data received the server won't except, usually has to do with binary modes
1006 ABNORMAL CLOSURE Client lost connection suddenly