This version of the module is deprecated: Merged into gaato/mttp (0.3 and later), the module an app adds. See https://github.com/gaato/mttp
gaato/http Transport over moonbitlang/async. Native, JS, and moonrun Wasm targets.
Status: experimental. Minor releases may contain breaking changes; the changelog lists each one.
moon add gaato/http
moon add gaato/http-async
moon add moonbitlang/async///|
/// Fetches a page as text.
pub async fn fetch_text(url : String) -> String {
let transport = @http_async.AsyncTransport::new(
timeout_ms=30_000,
max_body_bytes=64 * 1024 * 1024,
)
defer transport.close()
let response = transport.send(@http.Request::get(url))
guard response.is_success() else { fail("HTTP \{response.status}") }
response.text()
}| Package | What it is |
|---|---|
| gaato/http-async | AsyncTransport, AsyncClock, ClockTimer, SemaphoreGate, DeadlineTransport, with_timeout_opt |
| gaato/http-async/mock | VirtualClock, a Clock whose time moves only when a test advances it |
pub struct AsyncClock {
}impl Clock for AsyncClocktest {
let _ = @http_async.AsyncClock::new()
}pub struct AsyncTransport {
// private fields
}impl Transport for AsyncTransportasync fn send_stream(self : AsyncTransport, request : Request) -> (ResponseHead, &BodyStream) raise HttpErrortest {
let transport = @http_async.AsyncTransport::new()
transport.close()
assert_eq(transport.idle_connections(), 0)
}fn AsyncTransport::new(timeout_ms? : Int, connect_timeout_ms? : Int, write_timeout_ms? : Int, read_timeout_ms? : Int, pool_timeout_ms? : Int, max_connections? : Int, max_connections_per_origin? : Int, max_idle_per_origin? : Int, idle_max_ms? : Int, max_body_bytes? : Int, proxy? : String, trust_env? : Bool, clock? : &Clock) -> AsyncTransporttest {
let _ = @http_async.AsyncTransport::new(timeout_ms=1000)
let _ = @http_async.AsyncTransport::new(
connect_timeout_ms=5000,
read_timeout_ms=60000,
)
let _ = @http_async.AsyncTransport::new(max_connections_per_origin=8)
let _ = @http_async.AsyncTransport::new(max_idle_per_origin=0)
let _ = @http_async.AsyncTransport::new(max_body_bytes=1_048_576)
let _ = @http_async.AsyncTransport::new(proxy="http://user:pass@proxy:3128")
let _ = @http_async.AsyncTransport::new(trust_env=false)
}async fn AsyncTransport::send_stream(self : AsyncTransport, request : Request) -> (ResponseHead, &BodyStream) raise HttpErrorpub struct ClockTimer {
// private fields
}impl Timer for ClockTimerasync fn within(self : ClockTimer, milliseconds : Int, f : async () -> Unit noraise) -> Bool noraiseasync test {
let timer = @http_async.ClockTimer::new()
let done = Ref(false)
assert_true(timer.within(1000, () => done.val = true))
assert_true(done.val)
}async fn ClockTimer::within(self : ClockTimer, milliseconds : Int, f : async () -> Unit noraise) -> Bool noraisepub struct DeadlineTransport {
// private fields
}impl Transport for DeadlineTransportasync fn send_stream(self : DeadlineTransport, request : Request) -> (ResponseHead, &BodyStream) raise HttpErrorasync fn DeadlineTransport::send(self : DeadlineTransport, request : Request) -> Response raise HttpErrorasync fn DeadlineTransport::send_stream(self : DeadlineTransport, request : Request) -> (ResponseHead, &BodyStream) raise HttpErrorpub struct SemaphoreGate {
// private fields
}impl Gate for SemaphoreGateasync test {
let clock = @http_async.AsyncClock::new()
assert_eq(@http_async.with_timeout_opt(clock, 1000, () => 7), Some(7))
}Install
Download zipgaato/http Transport over moonbitlang/async. Native, JS, and moonrun Wasm targets.