%%%-------------------------------------------------------------------
%%% @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, Reason}
            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, Reason}
            end;

        {error, Reason} ->
            lager:warning("[iot_http_client] url: ~p, get error: ~p", [Url, Reason]),
            {error, Reason}
    end.