add test code
This commit is contained in:
parent
8f121402b2
commit
3a907417d6
@ -17,24 +17,14 @@ init(Req0, Opts) ->
|
|||||||
Path = binary_to_list(cowboy_req:path(Req0)),
|
Path = binary_to_list(cowboy_req:path(Req0)),
|
||||||
|
|
||||||
{ok, ReqBody, Req1} = parse_body(Req0),
|
{ok, ReqBody, Req1} = parse_body(Req0),
|
||||||
Sign = cowboy_req:header(<<"sign">>, Req1, <<>>),
|
|
||||||
BodySign = njau_bot_signer:sign(ReqBody),
|
|
||||||
case BodySign =:= string:lowercase(Sign) of
|
|
||||||
true ->
|
|
||||||
{ok, StatusCode, Resp} = handle_request(Method, Path, ReqBody),
|
{ok, StatusCode, Resp} = handle_request(Method, Path, ReqBody),
|
||||||
lager:debug("[http_protocol] request path: ~p, post_params: ~p, response: ~ts",
|
lager:debug("[http_protocol] request path: ~p, post_params: ~p, response: ~ts",
|
||||||
[Path, ReqBody, Resp]),
|
[Path, ReqBody, Resp]),
|
||||||
Req2 = cowboy_req:reply(StatusCode, #{
|
Req2 = cowboy_req:reply(StatusCode, #{
|
||||||
<<"Content-Type">> => <<"application/json">>
|
<<"Content-Type">> => <<"application/json">>
|
||||||
}, Resp, Req1),
|
}, Resp, Req1),
|
||||||
{ok, Req2, Opts};
|
{ok, Req2, Opts}.
|
||||||
false ->
|
|
||||||
lager:debug("[api_handler] invalid sign: ~p, body sign: ~p, request body: ``~ts``", [Sign, BodySign, ReqBody]),
|
|
||||||
Req2 = cowboy_req:reply(500, #{
|
|
||||||
<<"Content-Type">> => <<"text/html;charset=utf-8">>
|
|
||||||
}, <<"Internal Server Error">>, Req1),
|
|
||||||
{ok, Req2, Opts}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% 场地信息
|
%% 场地信息
|
||||||
handle_request("POST", "/api/device_info/storeInfo", ReqBody) when is_binary(ReqBody) ->
|
handle_request("POST", "/api/device_info/storeInfo", ReqBody) when is_binary(ReqBody) ->
|
||||||
@ -81,9 +71,8 @@ handle_request("POST", "/api/device_info/order", ReqBody) ->
|
|||||||
|
|
||||||
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||||
|
|
||||||
handle_request(_, Path, _) ->
|
handle_request(_, _Path, _) ->
|
||||||
Path1 = list_to_binary(Path),
|
{ok, 200, json_reply(true, <<"OK">>)}.
|
||||||
{ok, 200, json_reply(false, <<"url: ", Path1/binary, " not found">>)}.
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
%% helper methods
|
%% helper methods
|
||||||
|
|||||||
118
apps/njau_bot/src/http_handler/api_handler1.erl
Normal file
118
apps/njau_bot/src/http_handler/api_handler1.erl
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
%%% @author licheng5
|
||||||
|
%%% @copyright (C) 2020, <COMPANY>
|
||||||
|
%%% @doc
|
||||||
|
%%%
|
||||||
|
%%% @end
|
||||||
|
%%% Created : 26. 4月 2020 3:36 下午
|
||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
-module(api_handler1).
|
||||||
|
-author("licheng5").
|
||||||
|
|
||||||
|
%% API
|
||||||
|
-export([init/2]).
|
||||||
|
|
||||||
|
init(Req0, Opts) ->
|
||||||
|
Method = binary_to_list(cowboy_req:method(Req0)),
|
||||||
|
Path = binary_to_list(cowboy_req:path(Req0)),
|
||||||
|
|
||||||
|
{ok, ReqBody, Req1} = parse_body(Req0),
|
||||||
|
Sign = cowboy_req:header(<<"sign">>, Req1, <<>>),
|
||||||
|
BodySign = njau_bot_signer:sign(ReqBody),
|
||||||
|
case BodySign =:= string:lowercase(Sign) of
|
||||||
|
true ->
|
||||||
|
{ok, StatusCode, Resp} = handle_request(Method, Path, ReqBody),
|
||||||
|
lager:debug("[http_protocol] request path: ~p, post_params: ~p, response: ~ts",
|
||||||
|
[Path, ReqBody, Resp]),
|
||||||
|
Req2 = cowboy_req:reply(StatusCode, #{
|
||||||
|
<<"Content-Type">> => <<"application/json">>
|
||||||
|
}, Resp, Req1),
|
||||||
|
{ok, Req2, Opts};
|
||||||
|
false ->
|
||||||
|
lager:debug("[api_handler] invalid sign: ~p, body sign: ~p, request body: ``~ts``", [Sign, BodySign, ReqBody]),
|
||||||
|
Req2 = cowboy_req:reply(500, #{
|
||||||
|
<<"Content-Type">> => <<"text/html;charset=utf-8">>
|
||||||
|
}, <<"Internal Server Error">>, Req1),
|
||||||
|
{ok, Req2, Opts}
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% 场地信息
|
||||||
|
handle_request("POST", "/api/device_info/storeInfo", ReqBody) when is_binary(ReqBody) ->
|
||||||
|
#{<<"storeInfos">> := StoreInfos} = jiffy:decode(ReqBody, [return_maps]),
|
||||||
|
lager:debug("[api_handler] get storeInfo: ~p", [StoreInfos]),
|
||||||
|
|
||||||
|
njau_bot_logger:write(ReqBody),
|
||||||
|
|
||||||
|
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||||
|
|
||||||
|
%% 设备信息
|
||||||
|
handle_request("POST", "/api/device_info/equip", ReqBody) ->
|
||||||
|
#{<<"equips">> := Equips} = jiffy:decode(ReqBody, [return_maps]),
|
||||||
|
lager:debug("[api_handler] get equips: ~p", [Equips]),
|
||||||
|
|
||||||
|
njau_bot_logger:write(ReqBody),
|
||||||
|
|
||||||
|
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||||
|
|
||||||
|
%% 设备离线,在线状态
|
||||||
|
handle_request("POST", "/api/device_info/online", ReqBody) ->
|
||||||
|
#{<<"equipStatus">> := Equips} = jiffy:decode(ReqBody, [return_maps]),
|
||||||
|
lager:debug("[api_handler] get online: ~p", [Equips]),
|
||||||
|
|
||||||
|
njau_bot_logger:write(ReqBody),
|
||||||
|
|
||||||
|
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||||
|
|
||||||
|
%% 设备运行状态
|
||||||
|
handle_request("POST", "/api/device_info/runStatus", ReqBody) ->
|
||||||
|
#{<<"equipRunStatus">> := EquipRunStatus} = jiffy:decode(ReqBody, [return_maps]),
|
||||||
|
lager:debug("[api_handler] get equipRunStatus: ~p", [EquipRunStatus]),
|
||||||
|
|
||||||
|
njau_bot_logger:write(ReqBody),
|
||||||
|
|
||||||
|
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||||
|
|
||||||
|
%% 订单信息推送
|
||||||
|
handle_request("POST", "/api/device_info/order", ReqBody) ->
|
||||||
|
%#{<<"equipRunStatus">> := EquipRunStatus} = jiffy:decode(ReqBody, [return_maps]),
|
||||||
|
lager:debug("[api_handler] get order: ~p", [ReqBody]),
|
||||||
|
|
||||||
|
njau_bot_logger:write(ReqBody),
|
||||||
|
|
||||||
|
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||||
|
|
||||||
|
handle_request(_, Path, _) ->
|
||||||
|
Path1 = list_to_binary(Path),
|
||||||
|
{ok, 200, json_reply(false, <<"url: ", Path1/binary, " not found">>)}.
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
%% helper methods
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
|
parse_body(Req0) ->
|
||||||
|
ContentType = cowboy_req:header(<<"content-type">>, Req0),
|
||||||
|
case ContentType of
|
||||||
|
<<"application/json", _/binary>> ->
|
||||||
|
{ok, Body, Req1} = read_body(Req0),
|
||||||
|
{ok, Body, Req1};
|
||||||
|
_ ->
|
||||||
|
{ok, #{}, Req0}
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% 读取请求体
|
||||||
|
read_body(Req) ->
|
||||||
|
read_body(Req, <<>>).
|
||||||
|
read_body(Req, AccData) ->
|
||||||
|
case cowboy_req:read_body(Req) of
|
||||||
|
{ok, Data, Req1} ->
|
||||||
|
{ok, <<AccData/binary, Data/binary>>, Req1};
|
||||||
|
{more, Data, Req1} ->
|
||||||
|
read_body(Req1, <<AccData/binary, Data/binary>>)
|
||||||
|
end.
|
||||||
|
|
||||||
|
json_reply(Result, Message) when is_boolean(Result), is_binary(Message) ->
|
||||||
|
Json = jiffy:encode(#{
|
||||||
|
<<"result">> => Result,
|
||||||
|
<<"message">> => Message
|
||||||
|
}, [force_utf8]),
|
||||||
|
iolist_to_binary(Json).
|
||||||
Loading…
x
Reference in New Issue
Block a user