Net Lulib
The net lulib is a builtin lulib that provides a bunch of tools for networking.
HTTP
A simple http utility module to send and recieve http payload.
HTTP Types
-
Request:
url(string): The URL to the request sourceuri(string): The pathname and query providedmethod(string): The method (can bePOST,GET,DELETE, ...)headers(table<string, string>): The Headersbody(string | bytearray): The body content
-
Response:
host(string): The host of the requesteruri(string): The pathname and query providedmethod(string): The method (can bePOST,GET,DELETE, ...)headers(table<string, string>): The Headersbody(bytearray): The body content, always a ByteArray result.status(number): The status of the request response
-
PartialResponse:
status(number): The status of the request responseheaders(table<string, string>): The Headersbody(function() -> bytearray): Get the body.json(function() -> string): Parse the json as jsonyaml(function() -> string): Parse the json as yamlinto(function(Class) -> Class Instance): Parse the body asSerializable.into_many(function(Vec<Class>) -> Class Instance): Parse the body as multiple entries ofSerializable.
HTTP Client
You can send http requests using the reqwest crate to pull a ByteArray result.
net.http.request:(Request) -> PartialResponsenet.http.send:(url, Request?) -> PartialResponsenet.http.get:(url, Request?) -> PartialResponsenet.http.post:(url, Request?) -> PartialResponsenet.http.put:(url, Request?) -> PartialResponsenet.http.patch:(url, Request?) -> PartialResponsenet.http.delete:(url, Request?) -> PartialResponse
Example:
using { lulib.net }
() async =>
local res = net.http.get("https://example.com")
print(res.status, res.body():to_string())
end
Example With Serializable:
using { lulib.net }
local {
#id,
#name,
#username
} -> @Serializable('json') User
() async =>
-- Get one item
local res = net.http.get("https://jsonplaceholder.typicode.com/users/1")
fprint(res.into(User))
-- Get multiple items
local res = net.http.get("https://jsonplaceholder.typicode.com/users")
fprint(res.into_many(User))
end
Http Server
A simple http listener that listens async.
net.http.serve:(string, function(Request) -> Response) -> nil
net.http.serve("0.0.0.0:8000", function(req)
return Response {
body = "Hello" -- will be turned to bytes in rust
}
end)
TCP
net.tcp.connect:(addr: string) -> { read, write, close }net.tcp.listen:(addr: string) -> { accept() -> { read, write, close } }
UDP
net.tcp.bind:(addr: string) -> { send_to(addr, data), recv_from(size: number) -> data }
Websocket
net.websocket.connect:(url: string) -> { read, write, close }