HTTP
net.fetch(request) and net.download(request, path) work off the caller’s thread and are async. Each host your plugin talks to has to be in your permissions, and so does every host a redirect sends it to:
{ "permissions": { "net": ["example.com"] }}"*" allows any host. A host you didn’t list fails with NotDeclared.
A request has a url, a method, and optionally headers, a body for post, put and patch, max_bytes and timeout_ms. A request that hears nothing for timeout_ms, 30 seconds unless it says, fails with Timeout. It counts from the last bytes that arrived, so a big download on a slow connection still finishes. fetch settles with the status, headers and body for any status, so check it. It keeps up to 64 MiB unless max_bytes says otherwise, and fails with TooBig past that. It follows up to 5 redirects.
download streams the body to a file instead, and settles with its size. A relative path is in your plugin’s folder for this game. Anywhere outside your plugin’s folders needs "files": true too. The file is only replaced once the whole body is in, and a status that isn’t a success fails with Status. While it runs, net.downloading(path) says how many bytes are in, out of how many, for drawing progress.
import { Wraith } from "wraith";
const net = Wraith.net();const got = await net.fetch({ url: "https://example.com/list.json", method: "get" });if (got.status === 200) { // got.body is the bytes}
await net.download({ url: "https://example.com/big.gz", method: "get" }, "big.gz");// Each frame, while it runsconst { received, total, active } = net.downloading("big.gz");local Wraith = require("wraith").Wraith
task.spawn(function() local net = Wraith.net() local got = net:fetch({ url = "https://example.com/list.json", method = "get" }):await() if got.status == 200 then -- got.body is the bytes end net:download({ url = "https://example.com/big.gz", method = "get" }, "big.gz"):await()end)var net = Net.Instance;var got = await net.FetchAsync(new Net.Request { Url = "https://example.com/list.json", Method = Net.Method.Get });// got.Status, got.Headers and got.Body
await net.DownloadAsync(new Net.Request { Url = "https://example.com/big.gz", Method = Net.Method.Get }, "big.gz");var progress = net.Downloading("big.gz");const net = self.ctx.wraith.net();const got = try sdk.tasks.wait(net.fetch(.{ .url = .init("https://example.com/list.json"), .method = .get }));defer got.body.release();// got.status, got.headers.get() and got.body.bytes()
_ = try sdk.tasks.wait(net.download(.{ .url = .init("https://example.com/big.gz"), .method = .get }, "big.gz"));const progress = net.downloading("big.gz");wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
