From 3a907417d64e9e2dab4c4579cd12f2d93f100106 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Sun, 20 Oct 2024 12:24:51 +0800 Subject: [PATCH] add test code --- .../njau_bot/src/http_handler/api_handler.erl | 31 ++--- .../src/http_handler/api_handler1.erl | 118 ++++++++++++++++++ 2 files changed, 128 insertions(+), 21 deletions(-) create mode 100644 apps/njau_bot/src/http_handler/api_handler1.erl diff --git a/apps/njau_bot/src/http_handler/api_handler.erl b/apps/njau_bot/src/http_handler/api_handler.erl index c63df0c..50bcd44 100644 --- a/apps/njau_bot/src/http_handler/api_handler.erl +++ b/apps/njau_bot/src/http_handler/api_handler.erl @@ -17,24 +17,14 @@ init(Req0, Opts) -> 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. + {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}. + %% 场地信息 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>>)}; -handle_request(_, Path, _) -> - Path1 = list_to_binary(Path), - {ok, 200, json_reply(false, <<"url: ", Path1/binary, " not found">>)}. +handle_request(_, _Path, _) -> + {ok, 200, json_reply(true, <<"OK">>)}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% helper methods diff --git a/apps/njau_bot/src/http_handler/api_handler1.erl b/apps/njau_bot/src/http_handler/api_handler1.erl new file mode 100644 index 0000000..2e88a96 --- /dev/null +++ b/apps/njau_bot/src/http_handler/api_handler1.erl @@ -0,0 +1,118 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2020, +%%% @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, <>, Req1}; + {more, Data, Req1} -> + read_body(Req1, <>) + 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). \ No newline at end of file