refactor: support the retry option
This commit is contained in:
parent
cf5f1fd78c
commit
e5a673376f
|
@ -1 +1,20 @@
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
|
||||||
|
%%
|
||||||
|
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
%% you may not use this file except in compliance with the License.
|
||||||
|
%% You may obtain a copy of the License at
|
||||||
|
%%
|
||||||
|
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
%%
|
||||||
|
%% Unless required by applicable law or agreed to in writing, software
|
||||||
|
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
%% See the License for the specific language governing permissions and
|
||||||
|
%% limitations under the License.
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-define(APP, emqx_auth_http).
|
-define(APP, emqx_auth_http).
|
||||||
|
|
||||||
|
%% equals to the default value of ehttpc
|
||||||
|
-define(DEFAULT_RETRY_TIMES, 2).
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
-logger_header("[ACL http]").
|
-logger_header("[ACL http]").
|
||||||
|
|
||||||
-import(emqx_auth_http_cli,
|
-import(emqx_auth_http_cli,
|
||||||
[ request/6
|
[ request/7
|
||||||
, feedvar/2
|
, feedvar/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -56,13 +56,15 @@ description() -> "ACL with HTTP API".
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
check_acl_request(#{pool_name := PoolName,
|
check_acl_request(Params =
|
||||||
|
#{pool_name := PoolName,
|
||||||
path := Path,
|
path := Path,
|
||||||
method := Method,
|
method := Method,
|
||||||
headers := Headers,
|
headers := Headers,
|
||||||
params := Params,
|
params := Params,
|
||||||
timeout := Timeout}, ClientInfo) ->
|
timeout := Timeout}, ClientInfo) ->
|
||||||
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout).
|
Retry = maps:get(retry_times, Params, ?DEFAULT_RETRY_TIMES),
|
||||||
|
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry).
|
||||||
|
|
||||||
access(subscribe) -> 1;
|
access(subscribe) -> 1;
|
||||||
access(publish) -> 2.
|
access(publish) -> 2.
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
-logger_header("[Auth http]").
|
-logger_header("[Auth http]").
|
||||||
|
|
||||||
-import(emqx_auth_http_cli,
|
-import(emqx_auth_http_cli,
|
||||||
[ request/6
|
[ request/7
|
||||||
, feedvar/2
|
, feedvar/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -63,24 +63,28 @@ description() -> "Authentication by HTTP API".
|
||||||
%% Requests
|
%% Requests
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
authenticate(#{pool_name := PoolName,
|
authenticate(Params =
|
||||||
|
#{pool_name := PoolName,
|
||||||
path := Path,
|
path := Path,
|
||||||
method := Method,
|
method := Method,
|
||||||
headers := Headers,
|
headers := Headers,
|
||||||
params := Params,
|
params := Params,
|
||||||
timeout := Timeout}, ClientInfo) ->
|
timeout := Timeout}, ClientInfo) ->
|
||||||
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout).
|
Retry = maps:get(retry_times, Params, ?DEFAULT_RETRY_TIMES),
|
||||||
|
request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry).
|
||||||
|
|
||||||
-spec(is_superuser(maybe(map()), emqx_types:client()) -> boolean()).
|
-spec(is_superuser(maybe(map()), emqx_types:client()) -> boolean()).
|
||||||
is_superuser(undefined, _ClientInfo) ->
|
is_superuser(undefined, _ClientInfo) ->
|
||||||
false;
|
false;
|
||||||
is_superuser(#{pool_name := PoolName,
|
is_superuser(Params =
|
||||||
|
#{pool_name := PoolName,
|
||||||
path := Path,
|
path := Path,
|
||||||
method := Method,
|
method := Method,
|
||||||
headers := Headers,
|
headers := Headers,
|
||||||
params := Params,
|
params := Params,
|
||||||
timeout := Timeout}, ClientInfo) ->
|
timeout := Timeout}, ClientInfo) ->
|
||||||
case request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout) of
|
Retry = maps:get(retry_times, Params, ?DEFAULT_RETRY_TIMES),
|
||||||
|
case request(PoolName, Method, Path, Headers, feedvar(Params, ClientInfo), Timeout, Retry) of
|
||||||
{ok, 200, _Body} -> true;
|
{ok, 200, _Body} -> true;
|
||||||
{ok, _Code, _Body} -> false;
|
{ok, _Code, _Body} -> false;
|
||||||
{error, Error} -> ?LOG(error, "Request superuser path ~s, error: ~p", [Path, Error]),
|
{error, Error} -> ?LOG(error, "Request superuser path ~s, error: ~p", [Path, Error]),
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
-include("emqx_auth_http.hrl").
|
-include("emqx_auth_http.hrl").
|
||||||
|
|
||||||
-export([ request/6
|
-export([ request/6
|
||||||
|
, request/7
|
||||||
, feedvar/2
|
, feedvar/2
|
||||||
, feedvar/3
|
, feedvar/3
|
||||||
]).
|
]).
|
||||||
|
@ -27,18 +28,21 @@
|
||||||
%% HTTP Request
|
%% HTTP Request
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
request(PoolName, get, Path, Headers, Params, Timeout) ->
|
request(PoolName, Method, Path, Headers, Params, Timeout) ->
|
||||||
NewPath = Path ++ "?" ++ binary_to_list(cow_qs:qs(bin_kw(Params))),
|
request(PoolName, Method, Path, Headers, Params, ?DEFAULT_RETRY_TIMES).
|
||||||
reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout));
|
|
||||||
|
|
||||||
request(PoolName, post, Path, Headers, Params, Timeout) ->
|
request(PoolName, get, Path, Headers, Params, Timeout, Retry) ->
|
||||||
|
NewPath = Path ++ "?" ++ binary_to_list(cow_qs:qs(bin_kw(Params))),
|
||||||
|
reply(ehttpc:request(PoolName, get, {NewPath, Headers}, Timeout, Retry));
|
||||||
|
|
||||||
|
request(PoolName, post, Path, Headers, Params, Timeout, Retry) ->
|
||||||
Body = case proplists:get_value(<<"content-type">>, Headers) of
|
Body = case proplists:get_value(<<"content-type">>, Headers) of
|
||||||
"application/x-www-form-urlencoded" ->
|
"application/x-www-form-urlencoded" ->
|
||||||
cow_qs:qs(bin_kw(Params));
|
cow_qs:qs(bin_kw(Params));
|
||||||
"application/json" ->
|
"application/json" ->
|
||||||
emqx_json:encode(bin_kw(Params))
|
emqx_json:encode(bin_kw(Params))
|
||||||
end,
|
end,
|
||||||
reply(ehttpc:request(PoolName, post, {Path, Headers, Body}, Timeout)).
|
reply(ehttpc:request(PoolName, post, {Path, Headers, Body}, Timeout, Retry)).
|
||||||
|
|
||||||
reply({ok, StatusCode, _Headers}) ->
|
reply({ok, StatusCode, _Headers}) ->
|
||||||
{ok, StatusCode, <<>>};
|
{ok, StatusCode, <<>>};
|
||||||
|
|
Loading…
Reference in New Issue