lustre_http
Types
pub type HttpError {
Unauthorized
NotFound
InternalServerError(String)
OtherError(Int, String)
}
Constructors
-
Unauthorized
-
NotFound
-
InternalServerError(String)
-
OtherError(Int, String)
pub type HttpOrJsonError {
H(HttpError)
J(json.DecodeError)
}
Constructors
-
H(HttpError)
-
J(json.DecodeError)
pub type JsonResult(t) =
Result(t, HttpOrJsonError)
pub type StringResult =
Result(String, HttpError)
Functions
pub fn get_as_json(url: String, to_msg: fn(
Result(String, HttpOrJsonError),
) -> a, decoder: fn(Dynamic) ->
Result(String, List(DecodeError))) -> Effect(a)
Will automatically try to decode the JSON for you. The error-type is a bit complicated. In a future version will probably (a) force “text/json” in the headers (b) not decode for you.
pub fn get_as_text(url: String, to_msg: fn(
Result(String, HttpError),
) -> a) -> Effect(a)
Usage
import lustre_http as http
type Msg {
SomeInteraction
TextReceived(StringResult)
}
pub fn update(model, msg) {
case msg {
SomeInteraction -> #(model, http.get_as_text("the_text", TextReceived))
TextReceived(Ok(text)) -> #(apply(text, model), effect.none())
TextReceived(Error(e)) -> #(indicate_problem(e, model), effect.none())
}
}
pub fn post_text(url: String, body: String, to_msg: fn(
Result(String, HttpError),
) -> a) -> Effect(a)
Future versions will force headers in both the request and the response so you can post json and receive text, post text and receive json, etc.