Current section

Files

Jump to
nimiq_rpc src nimiq_rpc.erl
Raw

src/nimiq_rpc.erl

-module(nimiq_rpc).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/nimiq_rpc.gleam").
-export([client/1, client_with_auth/3]).
-file("src/nimiq_rpc.gleam", 16).
-spec send_request(
binary(),
gleam@option:option({binary(), binary()}),
binary()
) -> {ok, gleam@http@response:response(binary())} |
{error, gleam@httpc:http_error()}.
send_request(Url, Authentication, Payload) ->
Req@1 = case gleam@http@request:to(Url) of
{ok, Req} -> Req;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"nimiq_rpc"/utf8>>,
function => <<"send_request"/utf8>>,
line => 22,
value => _assert_fail,
start => 583,
'end' => 619,
pattern_start => 594,
pattern_end => 601})
end,
Req@2 = begin
_pipe = Req@1,
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_header(
_pipe@1,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_body(_pipe@2, Payload)
end,
Req@3 = case Authentication of
{some, {Username, Password}} ->
Auth = <<<<Username/binary, ":"/utf8>>/binary, Password/binary>>,
_pipe@3 = Req@2,
gleam@http@request:set_header(
_pipe@3,
<<"authorization"/utf8>>,
<<"Basic "/utf8,
(begin
_pipe@4 = gleam_stdlib:identity(Auth),
gleam_stdlib:base64_encode(_pipe@4, true)
end)/binary>>
);
none ->
Req@2
end,
gleam@httpc:send(Req@3).
-file("src/nimiq_rpc.gleam", 49).
-spec start_client(binary(), gleam@option:option({binary(), binary()})) -> gleam@erlang@process:subject(nimiq_rpc@internal@fiber@src@fiber@backend:message()).
start_client(Url, Authentication) ->
State = begin
_pipe = nimiq_rpc@internal@fiber@src@fiber:new(),
nimiq_rpc@internal@fiber@src@fiber@backend:build_state(_pipe)
end,
Actor@1 = case begin
_pipe@1 = gleam@otp@actor:new(State),
_pipe@9 = gleam@otp@actor:on_message(
_pipe@1,
fun(State@1, Msg) ->
Response_subject = gleam@erlang@process:new_subject(),
Next = begin
_pipe@2 = State@1,
nimiq_rpc@internal@fiber@src@fiber@backend:fiber_message(
_pipe@2,
Msg,
fun(Payload) ->
_pipe@5 = send_request(
Url,
Authentication,
begin
_pipe@3 = Payload,
_pipe@4 = nimiq_rpc@internal@fiber@src@fiber@message:to_json(
_pipe@3
),
gleam@json:to_string(_pipe@4)
end
),
gleam@result:map(
_pipe@5,
fun(Res) -> _pipe@6 = Response_subject,
gleam@erlang@process:send(
_pipe@6,
erlang:element(4, Res)
) end
)
end
)
end,
case Next of
{continue, Next_state, _} ->
case begin
_pipe@7 = Response_subject,
gleam@erlang@process:'receive'(_pipe@7, 0)
end of
{ok, Response} ->
_ = begin
_pipe@8 = Next_state,
nimiq_rpc@internal@fiber@src@fiber@backend:handle_text(
_pipe@8,
Response
)
end,
nil;
_ ->
nil
end,
gleam@otp@actor:continue(Next_state);
{stop, Next@1} ->
Next@1
end
end
),
gleam@otp@actor:start(_pipe@9)
end of
{ok, {started, _, Actor}} -> Actor;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"nimiq_rpc"/utf8>>,
function => <<"start_client"/utf8>>,
line => 55,
value => _assert_fail,
start => 1313,
'end' => 2182,
pattern_start => 1324,
pattern_end => 1351})
end,
Actor@1.
-file("src/nimiq_rpc.gleam", 89).
-spec client(binary()) -> gleam@erlang@process:subject(nimiq_rpc@internal@fiber@src@fiber@backend:message()).
client(Url) ->
start_client(Url, none).
-file("src/nimiq_rpc.gleam", 93).
-spec client_with_auth(binary(), binary(), binary()) -> gleam@erlang@process:subject(nimiq_rpc@internal@fiber@src@fiber@backend:message()).
client_with_auth(Url, Username, Password) ->
start_client(Url, {some, {Username, Password}}).