Compare commits

..

4 Commits

View File

@ -38,14 +38,14 @@
-module(hz_grids). -module(hz_grids).
-vsn("0.7.0"). -vsn("0.7.0").
-export([url/2, parse/1, req/2, req/3]). -export([url/2, url/3, url/4, parse/1, req/2, req/3]).
-spec url(Instruction, HTTP) -> Result -spec url(Instruction, HTTP) -> Result
when Instruction :: spend | transfer | sign, when Instruction :: spend | transfer | sign,
HTTP :: uri_string:uri_string(), HTTP :: uri_string:uri_string(),
GRIDS :: uri_string:uri_string(), Result :: {ok, GRIDS} | uri_string:uri_error(),
Result :: {ok, GRIDS} | uri_string:uri_error(). GRIDS :: uri_string:uri_string().
%% @doc %% @doc
%% Takes %% Takes
@ -66,6 +66,63 @@ url2(Instruction, URL = #{path := Path}) ->
{ok, uri_string:recompose(GRIDS)}. {ok, uri_string:recompose(GRIDS)}.
-spec url(Instruction, Recipient, Amount) -> GRIDS
when Instruction :: {spend, Network} | {transfer, Node},
Network :: string(),
Node :: {inet:ip_address() | inet:hostname(), inet:port_number()}
| uri_string:uri_string(),
Recipient :: string(),
Amount :: non_neg_integer(),
GRIDS :: uri_string:uri_string().
%% @doc
%% Forms a GRIDS URL for spends or transfers.
%% @equiv uri(Instruction, Recipient, Amount, "").
url(Instruction, Recipient, Amount) ->
url(Instruction, Recipient, Amount, "").
-spec url(Instruction, Recipient, Amount, Payload) -> GRIDS
when Instruction :: {spend, Network} | {transfer, Node},
Network :: string(),
Node :: {inet:ip_address() | inet:hostname(), inet:port_number()}
| uri_string:uri_string(), % "http://..." | "https://..."
Recipient :: string(),
Amount :: non_neg_integer() | none,
Payload :: binary(),
GRIDS :: uri_string:uri_string().
%% @doc
%% Forms a GRIDS URL for spends or transfers.
url({spend, Network}, Recipient, Amount, Payload) ->
Elements = ["grids://", Network, "/1/s/", Recipient, qwargs(Amount, Payload)],
unicode:characters_to_list(Elements);
url({transfer, Node}, Recipient, Amount, Payload) ->
Prefix =
case Node of
{H, P} -> ["grid://", h_to_s(H), ":", integer_to_list(P)];
"https://" ++ H -> ["grids://", H];
"http://" ++ H -> ["grid://", H];
<<"https://", H/binary>> -> ["grids://", H];
<<"http://", H/binary>> -> ["grid://", H]
end,
unicode:characters_to_list([Prefix, "/1/t/", Recipient, qwargs(Amount, Payload)]).
h_to_s(Host) when is_list(Host) -> Host;
h_to_s(Host) when is_binary(Host) -> Host;
h_to_s(Host) when is_tuple(Host) -> inet:ntoa(Host);
h_to_s(Host) when is_atom(Host) -> atom_to_list(Host).
qwargs(none, "") ->
[];
qwargs(Amount, "") ->
["?a=", integer_to_list(Amount)];
qwargs(none, Payload) ->
[$? | uri_string:compose_query([{"p", Payload}])];
qwargs(Amount, Payload) ->
[$? | uri_string:compose_query([{"a", integer_to_list(Amount)}, {"p", Payload}])].
-spec parse(GRIDS) -> Result -spec parse(GRIDS) -> Result
when GRIDS :: string(), when GRIDS :: string(),
Result :: {ok, Instruction} | uri_string:error(), Result :: {ok, Instruction} | uri_string:error(),