mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-20 17:27:00 +01:00
Add support for REST API custom headers
ext_api_headers can be defined as a single string. Headers are separated by comma. Definition MUST NOT contain spaces. Example "X-MyHead:test,X-Token:082748"
This commit is contained in:
parent
82c42051c3
commit
db51d522e8
39
src/rest.erl
39
src/rest.erl
@ -28,12 +28,14 @@
|
|||||||
-behaviour(ejabberd_config).
|
-behaviour(ejabberd_config).
|
||||||
|
|
||||||
-export([start/1, stop/1, get/2, get/3, post/4, delete/2,
|
-export([start/1, stop/1, get/2, get/3, post/4, delete/2,
|
||||||
put/4, patch/4, request/6, with_retry/4, opt_type/1]).
|
put/4, patch/4, request/6, with_retry/4,
|
||||||
|
encode_json/1, opt_type/1]).
|
||||||
|
|
||||||
-include("logger.hrl").
|
-include("logger.hrl").
|
||||||
|
|
||||||
-define(HTTP_TIMEOUT, 10000).
|
-define(HTTP_TIMEOUT, 10000).
|
||||||
-define(CONNECT_TIMEOUT, 8000).
|
-define(CONNECT_TIMEOUT, 8000).
|
||||||
|
-define(CONTENT_TYPE, "application/json").
|
||||||
|
|
||||||
start(Host) ->
|
start(Host) ->
|
||||||
application:start(inets),
|
application:start(inets),
|
||||||
@ -58,31 +60,32 @@ with_retry(Method, Args, Retries, MaxRetries, Backoff) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
get(Server, Path) ->
|
get(Server, Path) ->
|
||||||
request(Server, get, Path, [], "application/json", <<>>).
|
request(Server, get, Path, [], ?CONTENT_TYPE, <<>>).
|
||||||
get(Server, Path, Params) ->
|
get(Server, Path, Params) ->
|
||||||
request(Server, get, Path, Params, "application/json", <<>>).
|
request(Server, get, Path, Params, ?CONTENT_TYPE, <<>>).
|
||||||
|
|
||||||
delete(Server, Path) ->
|
delete(Server, Path) ->
|
||||||
request(Server, delete, Path, [], "application/json", <<>>).
|
request(Server, delete, Path, [], ?CONTENT_TYPE, <<>>).
|
||||||
|
|
||||||
post(Server, Path, Params, Content) ->
|
post(Server, Path, Params, Content) ->
|
||||||
Data = encode_json(Content),
|
Data = encode_json(Content),
|
||||||
request(Server, post, Path, Params, "application/json", Data).
|
request(Server, post, Path, Params, ?CONTENT_TYPE, Data).
|
||||||
|
|
||||||
put(Server, Path, Params, Content) ->
|
put(Server, Path, Params, Content) ->
|
||||||
Data = encode_json(Content),
|
Data = encode_json(Content),
|
||||||
request(Server, put, Path, Params, "application/json", Data).
|
request(Server, put, Path, Params, ?CONTENT_TYPE, Data).
|
||||||
|
|
||||||
patch(Server, Path, Params, Content) ->
|
patch(Server, Path, Params, Content) ->
|
||||||
Data = encode_json(Content),
|
Data = encode_json(Content),
|
||||||
request(Server, patch, Path, Params, "application/json", Data).
|
request(Server, patch, Path, Params, ?CONTENT_TYPE, Data).
|
||||||
|
|
||||||
request(Server, Method, Path, Params, Mime, Data) ->
|
request(Server, Method, Path, Params, Mime, Data) ->
|
||||||
URI = to_list(url(Server, Path, Params)),
|
URI = to_list(url(Server, Path, Params)),
|
||||||
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
|
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
|
||||||
{timeout, ?HTTP_TIMEOUT}],
|
{timeout, ?HTTP_TIMEOUT}],
|
||||||
Hdrs = [{"connection", "keep-alive"},
|
Hdrs = [{"connection", "keep-alive"},
|
||||||
{"User-Agent", "ejabberd"}],
|
{"User-Agent", "ejabberd"}]
|
||||||
|
++ custom_headers(Server),
|
||||||
Req = if
|
Req = if
|
||||||
(Method =:= post) orelse (Method =:= patch) orelse (Method =:= put) orelse (Method =:= delete) ->
|
(Method =:= post) orelse (Method =:= patch) orelse (Method =:= put) orelse (Method =:= delete) ->
|
||||||
{URI, Hdrs, to_list(Mime), Data};
|
{URI, Hdrs, to_list(Mime), Data};
|
||||||
@ -149,7 +152,7 @@ request(Server, Method, Path, Params, Mime, Data) ->
|
|||||||
|
|
||||||
to_list(V) when is_binary(V) ->
|
to_list(V) when is_binary(V) ->
|
||||||
binary_to_list(V);
|
binary_to_list(V);
|
||||||
to_list(V) ->
|
to_list(V) when is_list(V) ->
|
||||||
V.
|
V.
|
||||||
|
|
||||||
encode_json(Content) ->
|
encode_json(Content) ->
|
||||||
@ -164,6 +167,20 @@ encode_json(Content) ->
|
|||||||
Encoded
|
Encoded
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
custom_headers(Server) ->
|
||||||
|
case ejabberd_config:get_option({ext_api_headers, Server},
|
||||||
|
<<>>) of
|
||||||
|
<<>> ->
|
||||||
|
[];
|
||||||
|
Hdrs ->
|
||||||
|
lists:foldr(fun(Hdr, Acc) ->
|
||||||
|
case binary:split(Hdr, <<":">>) of
|
||||||
|
[K, V] -> [{binary_to_list(K), binary_to_list(V)}|Acc];
|
||||||
|
_ -> Acc
|
||||||
|
end
|
||||||
|
end, [], binary:split(Hdrs, <<",">>))
|
||||||
|
end.
|
||||||
|
|
||||||
base_url(Server, Path) ->
|
base_url(Server, Path) ->
|
||||||
BPath = case iolist_to_binary(Path) of
|
BPath = case iolist_to_binary(Path) of
|
||||||
<<$/, Ok/binary>> -> Ok;
|
<<$/, Ok/binary>> -> Ok;
|
||||||
@ -209,4 +226,6 @@ opt_type(ext_api_http_pool_size) ->
|
|||||||
fun (X) when is_integer(X), X > 0 -> X end;
|
fun (X) when is_integer(X), X > 0 -> X end;
|
||||||
opt_type(ext_api_url) ->
|
opt_type(ext_api_url) ->
|
||||||
fun (X) -> iolist_to_binary(X) end;
|
fun (X) -> iolist_to_binary(X) end;
|
||||||
opt_type(_) -> [ext_api_http_pool_size, ext_api_url].
|
opt_type(ext_api_headers) ->
|
||||||
|
fun (X) -> iolist_to_binary(X) end;
|
||||||
|
opt_type(_) -> [ext_api_http_pool_size, ext_api_url, ext_api_headers].
|
||||||
|
Loading…
Reference in New Issue
Block a user