Skip to content

Commit

Permalink
Convert kapi_websockets module to use kapi_definition module (2600hz#…
Browse files Browse the repository at this point in the history
  • Loading branch information
harenson authored and icehess committed Sep 5, 2019
1 parent 7d97cce commit cc0c420
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 163 deletions.
352 changes: 232 additions & 120 deletions core/kazoo_amqp/src/api/kapi_websockets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,32 @@
%%%-----------------------------------------------------------------------------
-module(kapi_websockets).

-export([get_req/1, get_req_v/1
,get_resp/1, get_resp_v/1
,module_req/1, module_req_v/1
,module_resp/1, module_resp_v/1
-export([api_definitions/0, api_definition/1]).

,bind_q/2
-export([get_req/1
,get_req_v/1
,publish_get_req/1
,publish_get_req/2
]).
-export([get_resp/1
,get_resp_v/1
,publish_get_resp/2
,publish_get_resp/3
]).
-export([module_req/1
,module_req_v/1
,publish_module_req/1
,publish_module_req/2
]).
-export([module_resp/1
,module_resp_v/1
,publish_module_resp/2
,publish_module_resp/3
]).

-export([bind_q/2
,unbind_q/2
,declare_exchanges/0

,publish_get_req/1, publish_get_req/2
,publish_get_resp/2, publish_get_resp/3
,publish_module_req/1, publish_module_req/2
,publish_module_resp/2, publish_module_resp/3
]).

-include("kapi_websockets.hrl").
Expand All @@ -36,80 +49,228 @@

-export_type([bind_props/0]).

-spec get_req(kz_term:api_terms()) ->{'ok', iolist()} | {'error', string()}.
get_req(Prop) when is_list(Prop) ->
case get_req_v(Prop) of
'true' -> kz_api:build_message(Prop, ?WEBSOCKETS_GET_REQ_HEADERS, ?OPTIONAL_WEBSOCKETS_GET_REQ_HEADERS);
'false' -> {'error', "Proplist failed validation for websockets get_req"}
end;
get_req(JObj) ->
get_req(kz_json:to_proplist(JObj)).

%%------------------------------------------------------------------------------
%% @doc
%% @doc Get all API definitions of this module.
%% @end
%%------------------------------------------------------------------------------
-spec get_req_v(kz_term:api_terms()) -> boolean().
get_req_v(Prop) when is_list(Prop) ->
kz_api:validate(Prop, ?WEBSOCKETS_GET_REQ_HEADERS, ?WEBSOCKETS_GET_REQ_VALUES, ?WEBSOCKETS_TYPES);
get_req_v(JObj) ->
get_req_v(kz_json:to_proplist(JObj)).
-spec api_definitions() -> kapi_definition:apis().
api_definitions() ->
[get_req_definition()
,get_resp_definition()
,module_req_definition()
,module_resp_definition()
].

%%------------------------------------------------------------------------------
%% @doc
%% @doc Get API definition of the given `Name'.
%% @see api_definitions/0
%% @end
%%------------------------------------------------------------------------------
-spec get_resp(kz_term:api_terms()) -> {'ok', iolist()} | {'error', string()}.
get_resp(Prop) when is_list(Prop) ->
case get_resp_v(Prop) of
'true' -> kz_api:build_message(Prop, ?WEBSOCKETS_GET_RESP_HEADERS, ?OPTIONAL_WEBSOCKETS_GET_RESP_HEADERS);
'false' -> {'error', "Proplist failed validation for websockets get_resp"}
end;
get_resp(JObj) ->
get_resp(kz_json:to_proplist(JObj)).
-spec api_definition(kz_term:text()) -> kapi_definition:api().
api_definition(Name) when not is_binary(Name) ->
api_definition(kz_term:to_binary(Name));
api_definition(<<"get_req">>) ->
get_req_definition();
api_definition(<<"get_resp">>) ->
get_resp_definition();
api_definition(<<"module_req">>) ->
module_req_definition();
api_definition(<<"module_resp">>) ->
module_resp_definition().

-spec get_req_definition() -> kapi_definition:api().
get_req_definition() ->
EventName = <<"get_req">>,
Category = <<"websockets">>,
Setters = [{fun kapi_definition:set_name/2, EventName}
,{fun kapi_definition:set_friendly_name/2, <<"WebSockets Get Request">>}
,{fun kapi_definition:set_description/2, <<"WebSockets Request to Read">>}
,{fun kapi_definition:set_category/2, Category}
,{fun kapi_definition:set_build_fun/2, fun get_req/1}
,{fun kapi_definition:set_validate_fun/2, fun get_req_v/1}
,{fun kapi_definition:set_publish_fun/2, fun publish_get_req/1}
,{fun kapi_definition:set_binding/2, ?KEY_WEBSOCKETS_GET_REQ}
,{fun kapi_definition:set_required_headers/2, []}
,{fun kapi_definition:set_optional_headers/2, [?KEY_API_ACCOUNT_ID
,<<"Socket-ID">>
]}
,{fun kapi_definition:set_values/2
,kapi_definition:event_type_headers(Category, EventName)
}
,{fun kapi_definition:set_types/2, []}
],
kapi_definition:setters(Setters).

-spec get_resp_definition() -> kapi_definition:api().
get_resp_definition() ->
EventName = <<"get_resp">>,
Category = <<"websockets">>,
Setters = [{fun kapi_definition:set_name/2, EventName}
,{fun kapi_definition:set_friendly_name/2, <<"WebSockets Get Response">>}
,{fun kapi_definition:set_description/2, <<"WebSockets Answer to Read Request">>}
,{fun kapi_definition:set_category/2, Category}
,{fun kapi_definition:set_build_fun/2, fun get_resp/1}
,{fun kapi_definition:set_validate_fun/2, fun get_resp_v/1}
,{fun kapi_definition:set_publish_fun/2, fun publish_get_resp/2}
,{fun kapi_definition:set_required_headers/2, []}
,{fun kapi_definition:set_optional_headers/2, [<<"Data">>
]}
,{fun kapi_definition:set_values/2
,kapi_definition:event_type_headers(Category, EventName)
}
,{fun kapi_definition:set_types/2, []}
],
kapi_definition:setters(Setters).

-spec module_req_definition() -> kapi_definition:api().
module_req_definition() ->
EventName = <<"module_req">>,
Category = <<"websockets">>,
Setters = [{fun kapi_definition:set_name/2, EventName}
,{fun kapi_definition:set_friendly_name/2, <<"WebSockets Module Request">>}
,{fun kapi_definition:set_description/2, <<"WebSockets Module Request">>}
,{fun kapi_definition:set_category/2, Category}
,{fun kapi_definition:set_build_fun/2, fun module_req/1}
,{fun kapi_definition:set_validate_fun/2, fun module_req_v/1}
,{fun kapi_definition:set_publish_fun/2, fun publish_module_req/1}
,{fun kapi_definition:set_binding/2, ?MODULE_REQ_ROUTING_KEY}
,{fun kapi_definition:set_required_headers/2, [<<"Module">>
,<<"Action">>
]}
,{fun kapi_definition:set_optional_headers/2, [<<"Persist">>
]}
,{fun kapi_definition:set_values/2
,[{<<"Event-Category">>, Category}
,{<<"Event-Name">>, EventName}
,{<<"Action">>, [<<"start">>, <<"stop">>]}
]
}
,{fun kapi_definition:set_types/2
,[{<<"Persist">>, fun kz_term:is_boolean/1}
,{<<"Module">>, fun is_binary/1}
]
}
],
kapi_definition:setters(Setters).

-spec module_resp_definition() -> kapi_definition:api().
module_resp_definition() ->
EventName = <<"module_resp">>,
Category = <<"websockets">>,
Setters = [{fun kapi_definition:set_name/2, EventName}
,{fun kapi_definition:set_friendly_name/2, <<"WebSockets Module Response">>}
,{fun kapi_definition:set_description/2, <<"WebSockets Module Response">>}
,{fun kapi_definition:set_category/2, Category}
,{fun kapi_definition:set_build_fun/2, fun module_resp/1}
,{fun kapi_definition:set_validate_fun/2, fun module_resp_v/1}
,{fun kapi_definition:set_publish_fun/2, fun publish_module_resp/2}
,{fun kapi_definition:set_required_headers/2, [<<"Persisted">>
,<<"Started">>
]}
,{fun kapi_definition:set_optional_headers/2, [<<"Error">>
]}
,{fun kapi_definition:set_values/2
,kapi_definition:event_type_headers(Category, EventName)
}
,{fun kapi_definition:set_types/2
,[{<<"Persisted">>, fun kz_term:is_boolean/1}
,{<<"Started">>, fun kz_term:is_boolean/1}
,{<<"Error">>, fun is_binary/1}
]
}
],
kapi_definition:setters(Setters).

%% request to read
-spec get_req(kz_term:api_terms()) -> kz_api:api_formatter_return().
get_req(Req) ->
kapi_definition:build_message(Req, get_req_definition()).

-spec get_req_v(kz_term:api_terms()) -> boolean().
get_req_v(Req) ->
kapi_definition:validate(Req, get_req_definition()).

-spec publish_get_req(kz_term:api_terms()) -> 'ok'.
publish_get_req(JObj) ->
publish_get_req(JObj, ?DEFAULT_CONTENT_TYPE).

-spec publish_get_req(kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_get_req(Api, ContentType) ->
Definition = get_req_definition(),
{'ok', Payload} = kz_api:prepare_api_payload(Api
,kapi_definition:values(Definition)
,kapi_definition:build_fun(Definition)
),
kz_amqp_util:sysconf_publish(kapi_definition:binding(Definition), Payload, ContentType).

%% answer to a read request
-spec get_resp(kz_term:api_terms()) -> kz_api:api_formatter_return().
get_resp(Req) ->
kapi_definition:build_message(Req, get_resp_definition()).

%%------------------------------------------------------------------------------
%% @doc
%% @end
%%------------------------------------------------------------------------------
-spec get_resp_v(kz_term:api_terms()) -> boolean().
get_resp_v(Prop) when is_list(Prop) ->
kz_api:validate(Prop, ?WEBSOCKETS_GET_RESP_HEADERS, ?WEBSOCKETS_GET_RESP_VALUES, ?WEBSOCKETS_TYPES);
get_resp_v(JObj) ->
get_resp_v(kz_json:to_proplist(JObj)).


-spec module_req(kz_term:api_terms()) -> {'ok', iolist()} |
{'error', string()}.
module_req(Prop) when is_list(Prop) ->
case module_req_v(Prop) of
'false' -> {'error', "Proplist failed validation for module_req"};
'true' -> kz_api:build_message(Prop, ?MODULE_REQ_HEADERS, ?OPTIONAL_MODULE_REQ_HEADERS)
end;
module_req(JObj) ->
module_req(kz_json:to_proplist(JObj)).
get_resp_v(Req) ->
kapi_definition:validate(Req, get_resp_definition()).

-spec publish_get_resp(kz_term:ne_binary(), kz_term:api_terms()) -> 'ok'.
publish_get_resp(RespQ, JObj) ->
publish_get_resp(RespQ, JObj, ?DEFAULT_CONTENT_TYPE).

-spec publish_get_resp(kz_term:ne_binary(), kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_get_resp(RespQ, Api, ContentType) ->
Definition = get_resp_definition(),
PrepareOptions = [{'formatter', kapi_definition:build_fun(Definition)}
,{'remove_recursive', 'false'}
],
{'ok', Payload} = kz_api:prepare_api_payload(Api
,kapi_definition:values(Definition)
,PrepareOptions
),
kz_amqp_util:targeted_publish(RespQ, Payload, ContentType).

%% Module request
-spec module_req(kz_term:api_terms()) -> kz_api:api_formatter_return().
module_req(Req) ->
kapi_definition:build_message(Req, module_req_definition()).

-spec module_req_v(kz_term:api_terms()) -> boolean().
module_req_v(Prop) when is_list(Prop) ->
kz_api:validate(Prop, ?MODULE_REQ_HEADERS, ?MODULE_REQ_VALUES, ?MODULE_REQ_TYPES);
module_req_v(JObj) ->
module_req_v(kz_json:to_proplist(JObj)).

-spec module_resp(kz_term:api_terms()) -> {'ok', iolist()} |
{'error', string()}.
module_resp(Prop) when is_list(Prop) ->
case module_resp_v(Prop) of
'false' -> {'error', "Proplist failed validation for module_resp"};
'true' -> kz_api:build_message(Prop, ?MODULE_RESP_HEADERS, ?OPTIONAL_MODULE_RESP_HEADERS)
end;
module_resp(JObj) ->
module_resp(kz_json:to_proplist(JObj)).
module_req_v(Req) ->
kapi_definition:validate(Req, module_req_definition()).

-spec publish_module_req(kz_term:api_terms()) -> 'ok'.
publish_module_req(API) ->
publish_module_req(API, ?DEFAULT_CONTENT_TYPE).

-spec publish_module_req(kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_module_req(API, ContentType) ->
Definition = module_req_definition(),
{'ok', Payload} = kz_api:prepare_api_payload(API
,kapi_definition:values(Definition)
,kapi_definition:build_fun(Definition)
),
kz_amqp_util:kapps_publish(kapi_definition:binding(Definition), Payload, ContentType).

%% Module response
-spec module_resp(kz_term:api_terms()) -> kz_api:api_formatter_return().
module_resp(Req) ->
kapi_definition:build_message(Req, module_resp_definition()).

-spec module_resp_v(kz_term:api_terms()) -> boolean().
module_resp_v(Prop) when is_list(Prop) ->
kz_api:validate(Prop, ?MODULE_RESP_HEADERS, ?MODULE_RESP_VALUES, ?MODULE_RESP_TYPES);
module_resp_v(JObj) ->
module_resp_v(kz_json:to_proplist(JObj)).
module_resp_v(Req) ->
kapi_definition:validate(Req, module_resp_definition()).

-spec publish_module_resp(kz_term:ne_binary(), kz_term:api_terms()) -> 'ok'.
publish_module_resp(ServerId, API) ->
publish_module_resp(ServerId, API, ?DEFAULT_CONTENT_TYPE).

-spec publish_module_resp(kz_term:ne_binary(), kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_module_resp(ServerId, API, ContentType) ->
Definition = module_resp_definition(),
{'ok', Payload} = kz_api:prepare_api_payload(API
,kapi_definition:values(Definition)
,kapi_definition:build_fun(Definition)
),
kz_amqp_util:targeted_publish(ServerId, Payload, ContentType).

-spec bind_q(kz_term:ne_binary(), bind_props()) -> 'ok'.
bind_q(Queue, Props) ->
Expand Down Expand Up @@ -141,52 +302,3 @@ declare_exchanges() ->
kz_amqp_util:kapps_exchange(),
kz_amqp_util:targeted_exchange(),
kz_amqp_util:sysconf_exchange().

-spec publish_module_req(kz_term:api_terms()) -> 'ok'.
publish_module_req(API) ->
publish_module_req(API, ?DEFAULT_CONTENT_TYPE).

-spec publish_module_req(kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_module_req(API, ContentType) ->
{'ok', Payload} = kz_api:prepare_api_payload(API, ?MODULE_REQ_VALUES, fun module_req/1),
kz_amqp_util:kapps_publish(?MODULE_REQ_ROUTING_KEY, Payload, ContentType).

-spec publish_module_resp(kz_term:ne_binary(), kz_term:api_terms()) -> 'ok'.
publish_module_resp(ServerId, API) ->
publish_module_resp(ServerId, API, ?DEFAULT_CONTENT_TYPE).

-spec publish_module_resp(kz_term:ne_binary(), kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_module_resp(ServerId, API, ContentType) ->
{'ok', Payload} = kz_api:prepare_api_payload(API, ?MODULE_RESP_VALUES, fun module_resp/1),
kz_amqp_util:targeted_publish(ServerId, Payload, ContentType).

%%------------------------------------------------------------------------------
%% @doc
%% @end
%%------------------------------------------------------------------------------

-spec publish_get_req(kz_term:api_terms()) -> 'ok'.
publish_get_req(JObj) ->
publish_get_req(JObj, ?DEFAULT_CONTENT_TYPE).

-spec publish_get_req(kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_get_req(Api, ContentType) ->
{'ok', Payload} = kz_api:prepare_api_payload(Api, ?WEBSOCKETS_GET_REQ_VALUES, fun get_req/1),
kz_amqp_util:sysconf_publish(?KEY_WEBSOCKETS_GET_REQ, Payload, ContentType).

%%------------------------------------------------------------------------------
%% @doc
%% @end
%%------------------------------------------------------------------------------

-spec publish_get_resp(kz_term:ne_binary(), kz_term:api_terms()) -> 'ok'.
publish_get_resp(RespQ, JObj) ->
publish_get_resp(RespQ, JObj, ?DEFAULT_CONTENT_TYPE).

-spec publish_get_resp(kz_term:ne_binary(), kz_term:api_terms(), kz_term:ne_binary()) -> 'ok'.
publish_get_resp(RespQ, Api, ContentType) ->
PrepareOptions = [{'formatter', fun get_resp/1}
,{'remove_recursive', 'false'}
],
{'ok', Payload} = kz_api:prepare_api_payload(Api, ?WEBSOCKETS_GET_RESP_VALUES, PrepareOptions),
kz_amqp_util:targeted_publish(RespQ, Payload, ContentType).
Loading

0 comments on commit cc0c420

Please sign in to comment.