diff --git a/apps/iot/include/endpoint.hrl b/apps/iot/include/endpoint.hrl index 49a33aa..f051fb9 100644 --- a/apps/iot/include/endpoint.hrl +++ b/apps/iot/include/endpoint.hrl @@ -10,6 +10,7 @@ -record(http_endpoint, { url = <<>> :: binary(), + token = <<>> :: binary(), pool_size = 10 :: integer() }). diff --git a/apps/iot/src/endpoint/endpoint.erl b/apps/iot/src/endpoint/endpoint.erl index e3a098e..c1984e9 100644 --- a/apps/iot/src/endpoint/endpoint.erl +++ b/apps/iot/src/endpoint/endpoint.erl @@ -116,9 +116,11 @@ parse_config(<<"mqtt">>, #{<<"host">> := Host, <<"port">> := Port0, <<"client_id false -> {error, Errors} end; -parse_config(<<"http">>, #{<<"url">> := Url, <<"pool_size">> := PoolSize}) -> +parse_config(<<"http">>, C = #{<<"url">> := Url, <<"pool_size">> := PoolSize}) -> + Token = maps:get(<<"token">>, C, <<>>), {ok, #http_endpoint{ url = Url, + token = Token, pool_size = PoolSize }}; parse_config(<<"kafka">>, #{<<"sasl_config">> := #{<<"username">> := Username, <<"password">> := Password, <<"mechanism">> := Mechanism0}, <<"bootstrap_servers">> := BootstrapServers, <<"topic">> := Topic}) -> diff --git a/apps/iot/src/iot_util.erl b/apps/iot/src/iot_util.erl index fc568dd..9dbde0f 100644 --- a/apps/iot/src/iot_util.erl +++ b/apps/iot/src/iot_util.erl @@ -14,6 +14,7 @@ -export([step/3, chunks/2, rand_bytes/1, uuid/0, md5/1, parse_mapper/1]). -export([json_data/1, json_error/2, is_json/1]). -export([queue_limited_in/3, assert_call/2, assert/2]). +-export([sha256/1]). -spec is_json(Json :: term()) -> boolean(). is_json(Json) when is_binary(Json) -> @@ -112,6 +113,14 @@ assert_call(true, Fun) -> assert_call(false, _) -> ok. +-spec sha256(Str :: string() | binary()) -> string(). +sha256(Str) when is_list(Str) -> + sha256(unicode:characters_to_binary(Str)); +sha256(Bin) when is_binary(Bin) -> + HashBin = crypto:hash(sha256, Bin), + HexStr = lists:flatten([io_lib:format("~2.16.0B", [B]) || B <- binary:bin_to_list(HashBin)]), + string:lowercase(HexStr). + -spec md5(Str :: binary()) -> binary(). md5(Str) when is_binary(Str) -> list_to_binary(lists:flatten([hex(X) || <> <= erlang:md5(Str)])).