fix http client

This commit is contained in:
安礼成 2023-03-03 11:59:19 +08:00
parent b276134de1
commit 486d870b42
6 changed files with 63 additions and 3 deletions

View File

@ -18,5 +18,6 @@
handle_request("GET", "/api/booking", _, _Params) ->
{ok, 200, iot_util:json_data(<<"success">>)};
handle_request("POST", "/api/booking", _, _Params) ->
handle_request("POST", _, _, Params) ->
lager:debug("body is: ~p", [Params]),
{ok, 200, iot_util:json_data(<<"success">>)}.

View File

@ -13,8 +13,11 @@
parse_trans,
hackney,
poolboy,
mnesia,
crypto,
public_key,
ssl,
kernel,
stdlib
]},

View File

@ -36,6 +36,7 @@ start_http_server() ->
Dispatcher = cowboy_router:compile([
{'_', [
{"/host/[...]", http_protocol, [http_host_handler]},
{"/api/[...]", http_protocol, [http_api_handler]},
{"/router/[...]", http_protocol, [http_router_handler]}
]}
]),

View File

@ -0,0 +1,40 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 03. 3 2023 11:48
%%%-------------------------------------------------------------------
-module(iot_http_client).
-author("licheng5").
%% API
-export([post/2]).
post(Url, Body) when is_list(Url), is_binary(Body) ->
case hackney:request(post, Url, [], Body) of
{ok, 200, _, ClientRef} ->
case hackney:body(ClientRef) of
{ok, RespBody} ->
lager:debug("[iot_http_client] url: ~p, response is: ~p", [Url, RespBody]),
ok;
{error, Reason} ->
lager:warning("[iot_http_client] url: ~p, get error: ~p", [Url, Reason]),
{error, failed}
end;
{ok, HttpCode, _, ClientRef} ->
case hackney:body(ClientRef) of
{ok, RespBody} ->
lager:debug("[iot_http_client] url: ~p, http_code: ~p, response is: ~p", [Url, HttpCode, RespBody]),
ok;
{error, Reason} ->
lager:warning("[iot_http_client] url: ~p, http_code: ~p, get error: ~p", [Url, HttpCode, Reason]),
{error, failed}
end;
{error, Reason} ->
lager:warning("[iot_http_client] url: ~p, get error: ~p", [Url, Reason]),
{error, failed}
end.

View File

@ -12,6 +12,7 @@
%% API
-export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/0]).
-export([start_router/1]).
-export([rsa_encode/1]).
insert_hosts() ->
@ -81,6 +82,16 @@ insert_routers() ->
router_model:add_router(R)
end, lists:seq(1, 100)).
start_router(Id0) when is_integer(Id0) ->
R = #router{
router_id = Id0,
name = <<"计费电表"/utf8>>,
rule = <<"测试规则"/utf8>>,
endpoint = #http_endpoint{url = <<"http://127.0.0.1:8080/data">>},
status = 1
},
router_model:add_router(R),
iot_router_sup:start_new_router(R).
rsa_encode(Data) when is_binary(Data) ->
%%

View File

@ -47,8 +47,12 @@ init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
%%
{ok, Routers} = router_model:get_all_valid_routers(),
Specs = lists:map(fun generate_router_spec/1, Routers),
Specs = case router_model:get_all_valid_routers() of
{ok, Routers} ->
lists:map(fun generate_router_spec/1, Routers);
error ->
[]
end,
{ok, {SupFlags, Specs}}.