main.bend source
main.bend on the hub · documented module
import Baseimport ./client.bend as Apiimport ./http.bend as H# Hub: import 0xa2d32c1d530e8aafaf6450baaef38f01/main.bend as Maindef env_or.fin( r: Result<&1, &1, U32 & String, String>, fallback: String) -> IO(String): match r: case Fail{e}: IO.pure(String, fallback) case Done{value}: IO.pure(String, value)def env_or(name: String, fallback: String) -> IO(String): do IO<String>: r : Result<&1, &1, U32 & String, String> <- IO.get_env(name) env_or.fin(r, fallback)def load_client.go(+base: String, token: String, org: String) -> Api.Client: Api.Client.with_org(Api.Client.with_token(Api.Client.new(base), token), org)def load_client() -> IO(Api.Client): do IO<Api.Client>: base : String <- env_or("NAVANEVESHT_API_BASE", Api.Client.default_base()) token : String <- env_or("NAVANEVESHT_TOKEN", "") org : String <- env_or("NAVANEVESHT_ORG_ID", "") IO.pure(Api.Client, load_client.go(base, token, org))def print_reply(r: Result<&1, &1, U32 & String, H.Reply>) -> IO(Unit): IO.print(Api.Api.show_reply(r))def example_health(+c: Api.Client) -> IO(Unit): do IO<Unit>: IO.print("GET " ++ Api.Client.url(c, Api.Health{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.health(c) print_reply(r)def example_plans(+c: Api.Client) -> IO(Unit): do IO<Unit>: IO.print("GET " ++ Api.Client.url(c, Api.Plans{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.plans(c) print_reply(r)def example_login(+c: Api.Client, email: String, password: String) -> IO(Unit): do IO<Unit>: IO.print("POST " ++ Api.Client.url(c, Api.Login{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.login(c, email, password) print_reply(r)def example_register( +c: Api.Client, email: String, password: String, name: String) -> IO(Unit): do IO<Unit>: IO.print("POST " ++ Api.Client.url(c, Api.Register{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.register(c, email, password, name) print_reply(r)def example_refresh(+c: Api.Client, token: String) -> IO(Unit): do IO<Unit>: IO.print("POST " ++ Api.Client.url(c, Api.Refresh{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.refresh(c, token) print_reply(r)def example_logout(+c: Api.Client) -> IO(Unit): do IO<Unit>: IO.print("POST " ++ Api.Client.url(c, Api.Logout{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.logout(c) print_reply(r)def example_me(+c: Api.Client) -> IO(Unit): do IO<Unit>: IO.print("GET " ++ Api.Client.url(c, Api.Me{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.me(c) print_reply(r)def example_wallet(+c: Api.Client) -> IO(Unit): do IO<Unit>: IO.print("GET " ++ Api.Client.url(c, Api.Wallet{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.wallet(c) print_reply(r)def example_temporary(+c: Api.Client) -> IO(Unit): do IO<Unit>: IO.print("POST " ++ Api.Client.url(c, Api.TemporaryKey{})) r : Result<&1, &1, U32 & String, H.Reply> <- Api.Api.temporary_key(c) print_reply(r)def usage() -> String: "navanevesht Bend client\n" ++ " health | plans | me | wallet | logout | temporary\n" ++ " login EMAIL PASSWORD\n" ++ " register EMAIL PASSWORD [DISPLAY_NAME]\n" ++ " refresh REFRESH_TOKEN\n" ++ "env: NAVANEVESHT_API_BASE NAVANEVESHT_TOKEN NAVANEVESHT_ORG_ID"def with_login(rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match rest: case Nil{}: IO.print(usage()) case Con{a, t}: match t: case Nil{}: IO.print(usage()) case Con{b, u}: example_login(c, a, b)def with_refresh(rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match rest: case Nil{}: IO.print(usage()) case Con{a, t}: example_refresh(c, a)def with_three(rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match rest: case Nil{}: IO.print(usage()) case Con{a, t}: match t: case Nil{}: IO.print(usage()) case Con{b, u}: match u: case Nil{}: example_register(c, a, b, "") case Con{n, v}: example_register(c, a, b, n)def dispatch.refresh.if( is_refresh: Bool, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_refresh: case True{}: with_refresh(rest, c) case False{}: IO.print(usage())def dispatch.refresh(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.refresh.if(String.eq(cmd, "refresh"), rest, c)def dispatch.register.if( is_register: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_register: case True{}: with_three(rest, c) case False{}: dispatch.refresh(cmd, rest, c)def dispatch.register(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.register.if(String.eq(cmd, "register"), cmd, rest, c)def dispatch.login.if( is_login: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_login: case True{}: with_login(rest, c) case False{}: dispatch.register(cmd, rest, c)def dispatch.login(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.login.if(String.eq(cmd, "login"), cmd, rest, c)def dispatch.temporary.if( is_temporary: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_temporary: case True{}: example_temporary(c) case False{}: dispatch.login(cmd, rest, c)def dispatch.temporary(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.temporary.if(String.eq(cmd, "temporary"), cmd, rest, c)def dispatch.logout.if( is_logout: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_logout: case True{}: example_logout(c) case False{}: dispatch.temporary(cmd, rest, c)def dispatch.logout(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.logout.if(String.eq(cmd, "logout"), cmd, rest, c)def dispatch.wallet.if( is_wallet: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_wallet: case True{}: example_wallet(c) case False{}: dispatch.logout(cmd, rest, c)def dispatch.wallet(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.wallet.if(String.eq(cmd, "wallet"), cmd, rest, c)def dispatch.me.if( is_me: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_me: case True{}: example_me(c) case False{}: dispatch.wallet(cmd, rest, c)def dispatch.me(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.me.if(String.eq(cmd, "me"), cmd, rest, c)def dispatch.plans.if( is_plans: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_plans: case True{}: example_plans(c) case False{}: dispatch.me(cmd, rest, c)def dispatch.plans(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.plans.if(String.eq(cmd, "plans"), cmd, rest, c)def dispatch.health.if( is_health: Bool, +cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): match is_health: case True{}: example_health(c) case False{}: dispatch.plans(cmd, rest, c)def dispatch.health(+cmd: String, rest: List<&1, String>, +c: Api.Client) -> IO(Unit): dispatch.health.if(String.eq(cmd, "health"), cmd, rest, c)def dispatch(xs: List<&1, String>, +c: Api.Client) -> IO(Unit): match xs: case Nil{}: IO.print(usage()) case Con{cmd, rest}: dispatch.health(cmd, rest, c)def main() -> IO(Unit): do IO<Unit>: c : Api.Client <- load_client() xs : List<&1, String> <- IO.args() dispatch(xs, c)