add test code

This commit is contained in:
anlicheng 2024-10-20 12:24:51 +08:00
parent 8f121402b2
commit 3a907417d6
2 changed files with 128 additions and 21 deletions

View File

@ -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, <<>>), {ok, StatusCode, Resp} = handle_request(Method, Path, ReqBody),
BodySign = njau_bot_signer:sign(ReqBody), lager:debug("[http_protocol] request path: ~p, post_params: ~p, response: ~ts",
case BodySign =:= string:lowercase(Sign) of [Path, ReqBody, Resp]),
true -> Req2 = cowboy_req:reply(StatusCode, #{
{ok, StatusCode, Resp} = handle_request(Method, Path, ReqBody), <<"Content-Type">> => <<"application/json">>
lager:debug("[http_protocol] request path: ~p, post_params: ~p, response: ~ts", }, Resp, Req1),
[Path, ReqBody, Resp]), {ok, Req2, Opts}.
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) -> 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

View 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).