Skip to main content

src/examples@custom_transport.erl

-module(examples@custom_transport).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/custom_transport.gleam").
-export([main/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(
    " Plug a custom transport into the client. Useful for tests, offline\n"
    " playback of recorded fixtures, JS targets, request logging, retries, etc.\n"
    "\n"
    " Run with:\n"
    "   gleam run -m examples/custom_transport\n"
).

-file("src/examples/custom_transport.gleam", 15).
?DOC(" A fake transport that returns a canned profile no matter what is asked.\n").
-spec canned_transport(gleam@http@request:request(binary())) -> {ok,
        gleam@http@response:response(binary())} |
    {error, binary()}.
canned_transport(_) ->
    Body = <<"{
      \"did\": \"did:plc:fixture\",
      \"handle\": \"fixture.test\",
      \"displayName\": \"Fixture\"
    }"/utf8>>,
    {ok, {response, 200, [], Body}}.

-file("src/examples/custom_transport.gleam", 27).
-spec main() -> nil.
main() ->
    Client = begin
        _pipe = rocksky:new(),
        rocksky:with_send(_pipe, fun canned_transport/1)
    end,
    Profile@1 = case begin
        _pipe@1 = rocksky@actor:get_profile(<<"anything"/utf8>>),
        rocksky:send(_pipe@1, Client)
    end of
        {ok, Profile} -> Profile;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"examples/custom_transport"/utf8>>,
                        function => <<"main"/utf8>>,
                        line => 32,
                        value => _assert_fail,
                        start => 871,
                        'end' => 962,
                        pattern_start => 882,
                        pattern_end => 893})
    end,
    gleam_stdlib:println(
        <<"handle: "/utf8,
            (gleam@option:unwrap(erlang:element(4, Profile@1), <<"?"/utf8>>))/binary>>
    ).