~/bend-docscommunity

http.bend source

http.bend on the hub · documented module

import Baseimport ./url.bend as Urltype Method is Data:  GET{}  POST{}  PUT{}  PATCH{}  DELETE{}type Hdr is Data:  Hdr{name: String, value: String}type Reply is Data:  Reply{status: U32, headers: String, body: String}def Method.show(m: Method) -> String:  match m:    case GET{}:      "GET"    case POST{}:      "POST"    case PUT{}:      "PUT"    case PATCH{}:      "PATCH"    case DELETE{}:      "DELETE"def Method.is_get(m: Method) -> Bool:  match m:    case GET{}:      True{}    case POST{}:      False{}    case PUT{}:      False{}    case PATCH{}:      False{}    case DELETE{}:      False{}def Hdr.line(h: Hdr) -> String:  match h:    case Hdr{name, value}:      name ++ ": " ++ valuedef Hdr.block(xs: List<&2, Hdr>) -> String:  match xs:    case Nil{}:      ""    case Con{h, t}:      Hdr.line(h) ++ "\n" ++ Hdr.block(t)def Hdr.json() -> Hdr:  Hdr{"Content-Type", "application/json"}def Hdr.accept() -> Hdr:  Hdr{"Accept", "application/json"}def Hdr.bearer(+token: String) -> Hdr:  Hdr{"Authorization", "Bearer " ++ token}def Hdr.org(+org: String) -> Hdr:  Hdr{"X-Org-Id", org}def Http.ok(+status: U32) -> Bool:  Bool.and(U32.is_ge(status, 200), U32.is_le(status, 299))def Http.unauthorized(+status: U32) -> Bool:  U32.is_eq(status, 401)def Http.fetch(  method: String, url: String, headers: String, body: String) -> IO(Result<&1, &1, U32 & String, U32 & String & String>):  import "./http_fetch.c"  import "./http_fetch.js"def Http.pack(r: Result<&1, &1, U32 & String, U32 & String & String>) ->  Result<&1, &1, U32 & String, Reply>:  match r:    case Fail{e}:      Fail{e}    case Done{(status, rest)}:      (headers, body) = rest      Done{Reply{status, headers, body}}def Http.send(  method: Method, url: String, headers: List<&2, Hdr>, body: String) -> IO(Result<&1, &1, U32 & String, Reply>):  do IO<Result<&1, &1, U32 & String, Reply>>:    raw : Result<&1, &1, U32 & String, U32 & String & String> <-      Http.fetch(Method.show(method), url, Hdr.block(headers), body)    IO.pure(Result<&1, &1, U32 & String, Reply>, Http.pack(raw))def Http.call(  +base: String, method: Method, +path: String, headers: List<&2, Hdr>,  body: String) -> IO(Result<&1, &1, U32 & String, Reply>):  Http.send(method, Url.join(base, path), headers, body)