%%%------------------------------------------------------------------- %%% @author cheese %%% @copyright (C) 2015, %%% @doc %%% %%% @end %%% Created : 23. Nov 2015 14:28 %%%------------------------------------------------------------------- -module(diint_start). -author("cheese"). -include_lib("kernel/include/logger.hrl"). %% 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} -> ?LOG_INFO("Failed get ~s from start. ~w", [Key, Reason]), {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) -> case os:getenv("ERLANG_ENV") of "Prod" -> send_to_start(Request, "data/cert/start_crt.pem", "data/cert/start_key.pem"); "Stage" -> send_to_start(Request, "data/cert/start_crt_stage.pem", "data/cert/start_key_stage.pem"); _ -> send_to_start(Request, "data/cert/start_crt.pem", "data/cert/start_key.pem") end. send_to_start(Request, Certfile, Keyfile) -> Url = "https://start-secondary.diint.it.loc:8084/DIINTStart/GetLite.aspx", ?LOG_INFO("http post ~s~n ", [Url]), ?LOG_INFO("=> ~s~n ", [Request]), 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}} -> ?LOG_INFO("<= (~w): ~w Bytes~n ", [HttpStatus, length(Response)]), case HttpStatus of 200 -> {ok, Response}; Other -> {error, {Other, Response}} end; ErrResponse -> ?LOG_INFO("<= (error): ~p~n ", [ErrResponse]), {error, ErrResponse} end; _ -> {error, {file_is_absent, Keyfile}} end; _ -> {error, {file_is_absent, Certfile}} end.