|
@@ -113,15 +113,17 @@ create(ChainID, AuthenticatorName,
|
|
|
request_timeout := RequestTimeout} = Config) ->
|
|
request_timeout := RequestTimeout} = Config) ->
|
|
|
#{path := Path,
|
|
#{path := Path,
|
|
|
query := Query} = URIMap = parse_url(URL),
|
|
query := Query} = URIMap = parse_url(URL),
|
|
|
- BaseURL = generate_base_url(URIMap),
|
|
|
|
|
State = #{method => Method,
|
|
State = #{method => Method,
|
|
|
path => Path,
|
|
path => Path,
|
|
|
base_query => cow_qs:parse_qs(list_to_binary(Query)),
|
|
base_query => cow_qs:parse_qs(list_to_binary(Query)),
|
|
|
- headers => maps:to_list(Headers),
|
|
|
|
|
- form_data => FormData,
|
|
|
|
|
|
|
+ headers => normalize_headers(Headers),
|
|
|
|
|
+ form_data => maps:to_list(FormData),
|
|
|
request_timeout => RequestTimeout},
|
|
request_timeout => RequestTimeout},
|
|
|
ResourceID = <<ChainID/binary, "/", AuthenticatorName/binary>>,
|
|
ResourceID = <<ChainID/binary, "/", AuthenticatorName/binary>>,
|
|
|
- case emqx_resource:create_local(ResourceID, emqx_connector_http, Config#{base_url => BaseURL}) of
|
|
|
|
|
|
|
+ case emqx_resource:create_local(ResourceID,
|
|
|
|
|
+ emqx_connector_http,
|
|
|
|
|
+ Config#{base_url => maps:remove(query, URIMap),
|
|
|
|
|
+ pool_type => random}) of
|
|
|
{ok, _} ->
|
|
{ok, _} ->
|
|
|
{ok, State#{resource_id => ResourceID}};
|
|
{ok, State#{resource_id => ResourceID}};
|
|
|
{error, already_created} ->
|
|
{error, already_created} ->
|
|
@@ -214,10 +216,8 @@ parse_url(URL) ->
|
|
|
URIMap
|
|
URIMap
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
-generate_base_url(#{scheme := Scheme,
|
|
|
|
|
- host := Host,
|
|
|
|
|
- port := Port}) ->
|
|
|
|
|
- iolist_to_binary(io_lib:format("~p://~s:~p", [Scheme, Host, Port])).
|
|
|
|
|
|
|
+normalize_headers(Headers) ->
|
|
|
|
|
+ [{atom_to_binary(K), V} || {K, V} <- maps:to_list(Headers)].
|
|
|
|
|
|
|
|
generate_request(Credential, #{method := Method,
|
|
generate_request(Credential, #{method := Method,
|
|
|
path := Path,
|
|
path := Path,
|
|
@@ -251,6 +251,8 @@ replace_placeholder(<<"${mqtt-username}">>, Credential) ->
|
|
|
maps:get(username, Credential, undefined);
|
|
maps:get(username, Credential, undefined);
|
|
|
replace_placeholder(<<"${mqtt-clientid}">>, Credential) ->
|
|
replace_placeholder(<<"${mqtt-clientid}">>, Credential) ->
|
|
|
maps:get(clientid, Credential, undefined);
|
|
maps:get(clientid, Credential, undefined);
|
|
|
|
|
+replace_placeholder(<<"${mqtt-password}">>, Credential) ->
|
|
|
|
|
+ maps:get(password, Credential, undefined);
|
|
|
replace_placeholder(<<"${ip-address}">>, Credential) ->
|
|
replace_placeholder(<<"${ip-address}">>, Credential) ->
|
|
|
maps:get(peerhost, Credential, undefined);
|
|
maps:get(peerhost, Credential, undefined);
|
|
|
replace_placeholder(<<"${cert-subject}">>, Credential) ->
|
|
replace_placeholder(<<"${cert-subject}">>, Credential) ->
|
|
@@ -274,9 +276,9 @@ qs([], Acc) ->
|
|
|
qs([{K, V} | More], Acc) ->
|
|
qs([{K, V} | More], Acc) ->
|
|
|
qs(More, [["&", emqx_http_lib:uri_encode(K), "=", emqx_http_lib:uri_encode(V)] | Acc]).
|
|
qs(More, [["&", emqx_http_lib:uri_encode(K), "=", emqx_http_lib:uri_encode(V)] | Acc]).
|
|
|
|
|
|
|
|
-serialize_body('application/json', FormData) ->
|
|
|
|
|
|
|
+serialize_body(<<"application/json">>, FormData) ->
|
|
|
emqx_json:encode(FormData);
|
|
emqx_json:encode(FormData);
|
|
|
-serialize_body('application/x-www-form-urlencoded', FormData) ->
|
|
|
|
|
|
|
+serialize_body(<<"application/x-www-form-urlencoded">>, FormData) ->
|
|
|
qs(FormData).
|
|
qs(FormData).
|
|
|
|
|
|
|
|
safely_parse_body(ContentType, Body) ->
|
|
safely_parse_body(ContentType, Body) ->
|