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]).
-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) ->
_assert_subject = gleam@http@request:to(Url),
{ok, Req} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"nimiq_rpc"/utf8>>,
function => <<"send_request"/utf8>>,
line => 22})
end,
Req@1 = begin
_pipe = Req,
_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@2 = case Authentication of
{some, {Username, Password}} ->
Auth = <<<<Username/binary, ":"/utf8>>/binary, Password/binary>>,
_pipe@3 = Req@1,
gleam@http@request:set_header(
_pipe@3,
<<"authorization"/utf8>>,
<<"Basic "/utf8,
(begin
_pipe@4 = gleam_stdlib:identity(Auth),
gleam_stdlib:bit_array_base64_encode(_pipe@4, true)
end)/binary>>
);
none ->
Req@1
end,
gleam@httpc:send(Req@2).
-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,
_assert_subject = gleam@otp@actor:start(
State,
fun(Msg, State@1) ->
Response_subject = gleam@erlang@process:new_subject(),
Next = begin
_pipe@1 = State@1,
nimiq_rpc@internal@fiber@src@fiber@backend:fiber_message(
_pipe@1,
Msg,
fun(Payload) ->
_pipe@4 = send_request(
Url,
Authentication,
begin
_pipe@2 = Payload,
_pipe@3 = nimiq_rpc@internal@fiber@src@fiber@message:to_json(
_pipe@2
),
gleam@json:to_string(_pipe@3)
end
),
gleam@result:map(
_pipe@4,
fun(Res) -> _pipe@5 = Response_subject,
gleam@erlang@process:send(
_pipe@5,
erlang:element(4, Res)
) end
)
end
)
end,
case Next of
{continue, Next_state, _} ->
case begin
_pipe@6 = Response_subject,
gleam_erlang_ffi:'receive'(_pipe@6, 0)
end of
{ok, Response} ->
_ = begin
_pipe@7 = Next_state,
nimiq_rpc@internal@fiber@src@fiber@backend:handle_text(
_pipe@7,
Response
)
end,
nil;
_ ->
nil
end,
gleam@otp@actor:continue(Next_state);
{stop, Reason} ->
{stop, Reason}
end
end
),
{ok, Actor} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"nimiq_rpc"/utf8>>,
function => <<"start_client"/utf8>>,
line => 55})
end,
Actor.
-file("src/nimiq_rpc.gleam", 86).
-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", 90).
-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}}).