-module(rsvp). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([expect_ok_response/1, expect_text/1, expect_any_response/1, send/2, expect_json/2, parse_relative_uri/1, get/2, post/3]). -export_type([error/0, handler/1]). -type error() :: bad_body | {bad_url, binary()} | {http_error, gleam@http@response:response(binary())} | {json_error, gleam@json:decode_error()} | network_error | {unhandled_response, gleam@http@response:response(binary())}. -opaque handler(OOR) :: {handler, fun(({ok, gleam@http@response:response(binary())} | {error, error()}) -> OOR)}. -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 183). -spec expect_ok_response( fun(({ok, gleam@http@response:response(binary())} | {error, error()}) -> OPP) ) -> handler(OPP). expect_ok_response(Handler) -> {handler, fun(Result) -> Handler( (gleam@result:'try'( Result, fun(Response) -> case erlang:element(2, Response) of Code when (Code >= 200) andalso (Code < 300) -> {ok, Response}; Code@1 when (Code@1 >= 400) andalso (Code@1 < 600) -> {error, {http_error, Response}}; _ -> {error, {unhandled_response, Response}} end end )) ) end}. -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 112). -spec expect_json_response( fun(({ok, gleam@http@response:response(binary())} | {error, error()}) -> OPB) ) -> handler(OPB). expect_json_response(Handler) -> expect_ok_response( fun(Result) -> Handler( (gleam@result:'try'( Result, fun(Response) -> case gleam@http@response:get_header( Response, <<"content-type"/utf8>> ) of {ok, <<"application/json"/utf8>>} -> {ok, Response}; {ok, <<"application/json;"/utf8, _/binary>>} -> {ok, Response}; _ -> {error, {unhandled_response, Response}} end end )) ) end ). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 156). -spec expect_text_response( fun(({ok, gleam@http@response:response(binary())} | {error, error()}) -> OPK) ) -> handler(OPK). expect_text_response(Handler) -> expect_ok_response( fun(Result) -> Handler( (gleam@result:'try'( Result, fun(Response) -> case gleam@http@response:get_header( Response, <<"content-type"/utf8>> ) of {ok, <<"text/"/utf8, _/binary>>} -> {ok, Response}; _ -> {error, {unhandled_response, Response}} end end )) ) end ). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 148). -spec expect_text(fun(({ok, binary()} | {error, error()}) -> OPF)) -> handler(OPF). expect_text(Handler) -> expect_text_response(fun(Result) -> _pipe = Result, _pipe@1 = gleam@result:map( _pipe, fun(Response) -> erlang:element(4, Response) end ), Handler(_pipe@1) end). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 211). -spec expect_any_response( fun(({ok, gleam@http@response:response(binary())} | {error, error()}) -> OPU) ) -> handler(OPU). expect_any_response(Handler) -> {handler, Handler}. -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 284). -spec do_send(gleam@http@request:request(binary()), handler(OQH)) -> lustre@effect:effect(OQH). do_send(Request, Handler) -> lustre@effect:from( fun(Dispatch) -> gleam@erlang@process:start( fun() -> _pipe = gleam@httpc:send(Request), _pipe@1 = gleam@result:map_error( _pipe, fun(Error) -> case Error of invalid_utf8_response -> bad_body; {failed_to_connect, _, _} -> network_error end end ), _pipe@2 = (erlang:element(2, Handler))(_pipe@1), Dispatch(_pipe@2) end, true ), nil end ). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 279). -spec send(gleam@http@request:request(binary()), handler(OQD)) -> lustre@effect:effect(OQD). send(Request, Handler) -> do_send(Request, Handler). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 326). -spec reject(error(), handler(OQK)) -> lustre@effect:effect(OQK). reject(Err, Handler) -> lustre@effect:from(fun(Dispatch) -> _pipe = {error, Err}, _pipe@1 = (erlang:element(2, Handler))(_pipe), Dispatch(_pipe@1) end). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 334). -spec decode_json_body( gleam@http@response:response(binary()), gleam@dynamic@decode:decoder(OQO) ) -> {ok, OQO} | {error, error()}. decode_json_body(Response, Decoder) -> _pipe = erlang:element(4, Response), _pipe@1 = gleam@json:parse(_pipe, Decoder), gleam@result:map_error(_pipe@1, fun(Field@0) -> {json_error, Field@0} end). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 101). -spec expect_json( gleam@dynamic@decode:decoder(OOS), fun(({ok, OOS} | {error, error()}) -> OOW) ) -> handler(OOW). expect_json(Decoder, Handler) -> expect_json_response(fun(Result) -> _pipe = Result, _pipe@1 = gleam@result:then( _pipe, fun(_capture) -> decode_json_body(_capture, Decoder) end ), Handler(_pipe@1) end). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 359). -spec parse_relative_uri(binary()) -> {ok, gleam@uri:uri()} | {error, nil}. parse_relative_uri(_) -> {error, nil}. -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 343). -spec to_uri(binary()) -> {ok, gleam@uri:uri()} | {error, error()}. to_uri(Uri_string) -> _pipe = case Uri_string of <<"./"/utf8, _/binary>> -> parse_relative_uri(Uri_string); <<"/"/utf8, _/binary>> -> parse_relative_uri(Uri_string); _ -> gleam_stdlib:uri_parse(Uri_string) end, gleam@result:replace_error(_pipe, {bad_url, Uri_string}). -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 226). -spec get(binary(), handler(OPW)) -> lustre@effect:effect(OPW). get(Url, Handler) -> case to_uri(Url) of {ok, Uri} -> _pipe = gleam@http@request:from_uri(Uri), _pipe@1 = gleam@result:map( _pipe, fun(_capture) -> send(_capture, Handler) end ), _pipe@2 = gleam@result:map_error( _pipe@1, fun(_) -> reject({bad_url, Url}, Handler) end ), gleam@result:unwrap_both(_pipe@2); {error, Err} -> reject(Err, Handler) end. -file("/Users/hayleigh/dev/courier/src/rsvp.gleam", 247). -spec post(binary(), gleam@json:json(), handler(OPZ)) -> lustre@effect:effect(OPZ). post(Url, Body, Handler) -> case to_uri(Url) of {ok, Uri} -> _pipe = gleam@http@request:from_uri(Uri), _pipe@5 = gleam@result:map(_pipe, fun(Request) -> _pipe@1 = Request, _pipe@2 = gleam@http@request:set_method(_pipe@1, post), _pipe@3 = gleam@http@request:set_header( _pipe@2, <<"content-type"/utf8>>, <<"application/json"/utf8>> ), _pipe@4 = gleam@http@request:set_body( _pipe@3, gleam@json:to_string(Body) ), send(_pipe@4, Handler) end), _pipe@6 = gleam@result:map_error( _pipe@5, fun(_) -> reject({bad_url, Url}, Handler) end ), gleam@result:unwrap_both(_pipe@6); {error, Err} -> reject(Err, Handler) end.