src/client.bend source
src/client.bend on the hub · documented module
# ezhttp/src/client: fetch a URL. URL and HTTP parsing joined to the wire effect.# Answers a structured Response (status + headers + body), not a hub-style# "0\n…" string contract.import Baseimport bend-kit-bytes@0.3.2.0/bytes.bend as Bytesimport ./url.bend as Urlimport ./http.bend as Httpimport ./body.bend as Bodyimport ./wire.bend as Wireimport ./wirespec.bend as Specimport ./redirect.bend as Redirectimport ./auth.bend as Authimport ./qs.bend as Qsimport ./packed.bend as Packed# structured client result: transport/parse error, or an HTTP response: its# status, its header fields and reason phrase as text (decoded from the octets# received as UTF-8, SPEC.md HTTP-BODY-4, HTTP-CLI-11), and its body's octets# as they arrived, packed (`client.text` decodes them when asked)type Response is Type: # noqa: L001 the wire effect keeps src/client.bend out of law files (HTTP-TRUST-5) Err{why: String} Response{status: U32, headers: List<&2, Http.Header>, reason: String, bytes: Bytes.Bytes}# a body's octets decoded as UTF-8 (SPEC.md HTTP-BODY-4)def client.text(b: Bytes.Bytes) -> String: Qs.octets.text(Bytes.to_string(b))# a response read off its octets as a client Response, its head's octet text# decodeddef from.got(g: Packed.Got) -> Response: match g: case Packed.Torn{why}: Err{why} case Packed.Got{status, headers, reason, body}: Response{status, Qs.octets.headers(headers), Qs.octets.text(reason), body}# an exchange's answer as a client Response, read by the request's method# (packed.bend packed.reply: interim 1xx heads skipped, no body for HEAD, 1xx,# 204 or 304); a failed exchange keeps its reason (wirespec.bend wire.failed)def http.answer(+method: String, a: Wire.Answer) -> Response: match a: case Wire.Failed{answer}: Err{Spec.wire.failed(answer)} case Wire.Received{octets}: from.got(Packed.packed.reply(method, octets))# an exchange as wirespec.bend wrote it, with the body's octets after it:# sent and read by its own method, or the URL's reasondef http.send(ex: Spec.Exchange, tail: Bytes.Bytes) -> IO(Response): match ex: case Spec.NoUrl{why}: IO.pure(Response, Err{why}) case Spec.Exchange{secure, host, port, +method, req}: do IO<Response>: a : Wire.Answer <- Wire.wire.talk(secure, host, port, method, req, tail) IO.pure(Response, http.answer(method, a))# an exchange for a body of this shape, sent with the body's octetsdef http.loc.shaped(method: String, l: Url.Loc, headers: List<&2, Http.Header>, r: Body.Shape & Bytes.Bytes) -> IO(Response): (shape, tail) = r http.send(Spec.wire.exchange.body(method, l, headers, shape), tail)# a URL that came apart (or not) into an HTTP exchange with this bodydef http.loc(method: String, l: Url.Loc, headers: List<&2, Http.Header>, body: Body.Body) -> IO(Response): http.loc.shaped(method, l, headers, Body.body.shape(body))# an exchange as wirespec.bend wrote it, bounded by a timeout, sent and read# by its own methoddef http.send.timed(ms: U32, ex: Spec.Exchange, tail: Bytes.Bytes) -> IO(Response): match ex: case Spec.NoUrl{why}: IO.pure(Response, Err{why}) case Spec.Exchange{secure, host, port, +method, req}: do IO<Response>: a : Wire.Answer <- Wire.wire.talk.timed(secure, ms, host, port, method, req, tail) IO.pure(Response, http.answer(method, a))# a timed exchange for a body of this shapedef client.timed.shaped(method: String, url: String, headers: List<&2, Http.Header>, ms: U32, r: Body.Shape & Bytes.Bytes) -> IO(Response): (shape, tail) = r http.send.timed(ms, Spec.wire.exchange.body(method, Url.parse(url), headers, shape), tail)# a request with this method, headers and body, bounded by a timeout in# millisecondsdef client.timed(method: String, url: String, headers: List<&2, Http.Header>, body: Body.Body, ms: U32) -> IO(Response): client.timed.shaped(method, url, headers, ms, Body.body.shape(body))# GET bounded by a timeout in millisecondsdef client.get.timed(url: String, headers: List<&2, Http.Header>, ms: U32) -> IO(Response): client.timed("GET", url, headers, Body.Empty{}, ms)# the next hop after a response (redirect.bend), and the response; a failed# exchange stopsdef client.hop(left: Nat, url: String, r: Response) -> Redirect.Hop & Response: match r: case Err{why}: (Redirect.Stop{}, Err{why}) case Response{+status, +headers, reason, bytes}: (Redirect.redirect.plan(left, url, status, headers), Response{status, headers, reason, bytes})# a response, or the GET its hop asks fordef client.after(p: Redirect.Hop & Response, go: String -> IO(Response)) -> IO(Response): (hop, r) = p match hop: case Redirect.Stop{}: IO.pure(Response, r) case Redirect.Go{next}: go(next)# GET, following redirects while `left` hops remain; `body` goes with this# hop only. The answer is the last response, which is a redirect only when# the hops ran out or its Location is not one redirect.bend followsdef client.follow(+left: Nat, +url: String, +headers: List<&2, Http.Header>, body: Body.Body, +ms: U32) -> IO(Response): match left: case 0n: client.timed("GET", url, headers, body, ms) case 1n+p: do IO<Response>: r : Response <- client.timed("GET", url, headers, body, ms) client.after(client.hop(left, url, r), next => client.follow(p, next, headers, Body.Empty{}, ms))# a request once it is known whether it follows redirectsdef client.request.go(go: Bool, method: String, url: String, headers: List<&2, Http.Header>, body: Body.Body, ms: U32) -> IO(Response): match go: case True{}: client.follow(Redirect.redirect.max(), url, headers, body, ms) case False{}: client.timed(method, url, headers, body, ms)# a request with a method, headers, body and timeout; it follows redirects# when `follow` is set and the method is GET (redirect.bend# redirect.follows), and answers its first response otherwisedef client.request.opts(+method: String, url: String, headers: List<&2, Http.Header>, body: Body.Body, ms: U32, follow: Bool) -> IO(Response): client.request.go(Redirect.redirect.follows(method, follow), method, url, headers, body, ms)# GET with headers and a timeout, following redirects when `follow` is setdef client.get.opts(url: String, headers: List<&2, Http.Header>, ms: U32, follow: Bool) -> IO(Response): client.request.opts("GET", url, headers, Body.Empty{}, ms, follow)# whether a response is the failure of an exchange that ran out of timedef client.timed_out(r: Response) -> Bool: match r: case Err{why}: Spec.wire.timed_out(why) case Response{status, headers, reason, bytes}: False{}# GETdef client.get(url: String, headers: List<&2, Http.Header>) -> IO(Response): http.loc("GET", Url.parse(url), headers, Body.Empty{})# HEAD (RFC 9110 §9.3.2)def client.head(url: String, headers: List<&2, Http.Header>) -> IO(Response): http.loc("HEAD", Url.parse(url), headers, Body.Empty{})# POSTdef client.post(url: String, headers: List<&2, Http.Header>, body: Body.Body) -> IO(Response): http.loc("POST", Url.parse(url), headers, body)# PUTdef client.put(url: String, headers: List<&2, Http.Header>, body: Body.Body) -> IO(Response): http.loc("PUT", Url.parse(url), headers, body)# DELETEdef client.delete(url: String, headers: List<&2, Http.Header>, body: Body.Body) -> IO(Response): http.loc("DELETE", Url.parse(url), headers, body)# OPTIONSdef client.options(url: String, headers: List<&2, Http.Header>) -> IO(Response): http.loc("OPTIONS", Url.parse(url), headers, Body.Empty{})# Authorization: Bearer … (auth.bend)def client.bearer(token: String) -> Http.Header: Auth.auth.bearer(token)# Authorization: Basic … (auth.bend; RFC 7617, RFC 4648 Base64)def client.basic(user: String, password: String) -> Http.Header: Auth.auth.basic(user, password)