Current section

Files

Jump to
diint_utilites_common_app src security diint_start.erl
Raw

src/security/diint_start.erl

%%%-------------------------------------------------------------------
%%% @author cheese
%%% @copyright (C) 2015, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 23. Nov 2015 14:28
%%%-------------------------------------------------------------------
-module(diint_start).
-author("cheese").
-define(LOG_FILE, "log/diint_start.log").
%% API
-export([get_data/2, send_to_start/1, get_request_param/2, get_mssql/1, get_password/1]).
get_mssql(Key) ->
get_data(Key, <<"2">>).
get_password(Key) ->
get_data(Key, <<"1">>).
get_data(Key, Type) ->
Request = <<"{\"Type\":\"", Type/binary, "\", \"Name\":\"", Key/binary, "\"}">>,
case send_to_start(Request) of
{ok, Response} ->
Json = jsx:decode(list_to_binary(Response)),
%logger:info("Json ~w~n ", [Json], ?LOG_FILE),
case get_request_param(Json, <<"IsSuccess">>) of
true ->
case get_request_param(Json, <<"Message">>) of
undefined ->
{error, {json_response_error, message_not_found}};
Data ->
{ok, Data}
end;
IsSuccess ->
{error, {json_response_error, {is_succces_value, IsSuccess}}}
end;
{error, Reason} ->
logger_Cheese:info("Failed get ~s from start. ~w", [Key, Reason], ?LOG_FILE),
{error, Reason}
end.
get_request_param([{ParamName, Value} | _OtherJson], ParamName) ->
Value;
get_request_param([{_Name, _Value} | OtherJson], ParamName) ->
get_request_param(OtherJson, ParamName);
get_request_param(_Other, _ParamName) ->
undefined.
send_to_start(Request) ->
Url = "https://start-secondary.diint.it.loc:8084/DIINTStart/GetLite.aspx",
logger_Cheese:info("http post ~s~n ", [Url], ?LOG_FILE),
logger_Cheese:info("=> ~s~n ", [Request], ?LOG_FILE),
Certfile = "data/cert/start_crt.pem",
Keyfile = "data/cert/start_key.pem",
case file_utilites:file_exist(Certfile) of
true ->
case file_utilites:file_exist(Keyfile) of
true ->
case httpc:request(post, {Url, [], "application/octet-stream", Request}, [{timeout, 10000}, {ssl, [{certfile, Certfile}, {keyfile, Keyfile}, {active, true}, binary]}], []) of
{ok, {{_, HttpStatus, _}, _, Response}} ->
logger_Cheese:info("<= (~w): ~w Bytes~n ", [HttpStatus, length(Response)], ?LOG_FILE),
case HttpStatus of
200 ->
{ok, Response};
Other ->
{error, {Other, Response}}
end;
ErrResponse ->
logger_Cheese:info("<= (error): ~p~n ", [ErrResponse], ?LOG_FILE),
{error, ErrResponse}
end;
_ ->
{error, {file_is_absent, Keyfile}}
end;
_ ->
{error, {file_is_absent, Certfile}}
end.