fix
This commit is contained in:
parent
609386cc44
commit
1f0899c85e
@ -1,27 +1,16 @@
|
||||
%%% docker_http.erl
|
||||
-module(docker_http).
|
||||
-export([request/4]).
|
||||
-export([test/0]).
|
||||
|
||||
test() ->
|
||||
try
|
||||
Path = "/api/detail?user_id=a23147f0-2faa-40fc-b1ae-fa387bc7349f&id=16322",
|
||||
catch request("GET", "/containers/json", undefined, [])
|
||||
catch _:Reason:Stack ->
|
||||
lager:debug("reason: ~p", [Reason]),
|
||||
lager:debug("stack: ~p", [Stack])
|
||||
end.
|
||||
|
||||
%% 通过 Unix Socket 调用 Docker API
|
||||
-spec request(Method :: iolist(), Path :: string(), Body :: binary() | undefined, Headers :: list()) ->
|
||||
{ok, StatusCode :: integer(), RespBody :: binary()} | {error, any()}.
|
||||
{ok, StatusCode :: integer(), RespHeaders :: proplists:proplist(), RespBody :: binary()} | {error, any()}.
|
||||
request(Method, Path, Body, Headers) when is_list(Method); is_binary(Method), is_list(Path), is_binary(Body), is_list(Headers) ->
|
||||
SocketPath = "/var/run/docker.sock",
|
||||
%% 使用 gun:open/2 + {local, Path} 方式
|
||||
case gun:open_unix(SocketPath, #{}) of
|
||||
{ok, ConnPid} ->
|
||||
{ok, _} = Res = gun:await_up(ConnPid),
|
||||
lager:debug("gun up: ~p, res: ~p", [ConnPid, Res]),
|
||||
{ok, http} = gun:await_up(ConnPid),
|
||||
%% 如果 Body 是 undefined,就用 <<>> 代替
|
||||
BodyBin = case Body of
|
||||
undefined -> <<>>;
|
||||
@ -29,7 +18,6 @@ request(Method, Path, Body, Headers) when is_list(Method); is_binary(Method), is
|
||||
end,
|
||||
%% 发送 HTTP 请求
|
||||
StreamRef = gun:request(ConnPid, Method, Path, Headers, BodyBin),
|
||||
lager:debug("stream is: ~p", [StreamRef]),
|
||||
receive_response(ConnPid, StreamRef);
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user