Skip to content

Commit

Permalink
Modify kapi_delegate module to use kapi_definition module (2600hz#6329)
Browse files Browse the repository at this point in the history
  • Loading branch information
harenson authored and jamesaimonetti committed Feb 26, 2020
1 parent 2cb0289 commit 690f372
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 58 deletions.
24 changes: 24 additions & 0 deletions applications/crossbar/priv/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -12996,6 +12996,30 @@
],
"type": "object"
},
"kapi.delegate.job": {
"description": "AMQP API for delegate.job",
"properties": {
"Delegate-Message": {
"type": "string"
},
"Event-Category": {
"enum": [
"delegate"
],
"type": "string"
},
"Event-Name": {
"enum": [
"job"
],
"type": "string"
}
},
"required": [
"Delegate-Message"
],
"type": "object"
},
"kapi.dialplan.answer": {
"description": "AMQP API for dialplan.answer",
"properties": {
Expand Down
26 changes: 26 additions & 0 deletions applications/crossbar/priv/couchdb/schemas/kapi.delegate.job.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"_id": "kapi.delegate.job",
"description": "AMQP API for delegate.job",
"properties": {
"Delegate-Message": {
"type": "string"
},
"Event-Category": {
"enum": [
"delegate"
],
"type": "string"
},
"Event-Name": {
"enum": [
"job"
],
"type": "string"
}
},
"required": [
"Delegate-Message"
],
"type": "object"
}
152 changes: 94 additions & 58 deletions core/kazoo_amqp/src/api/kapi_delegate.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,105 @@
%%%-----------------------------------------------------------------------------
-module(kapi_delegate).

-export([delegate/1, delegate_v/1]).
-export([api_definitions/0, api_definition/1]).

-export([delegate/1
,delegate_v/1
,publish_delegate/2
,publish_delegate/3
,publish_delegate/4
]).

-export([bind_q/2
,unbind_q/2
]).
-export([declare_exchanges/0]).
-export([publish_delegate/2, publish_delegate/3, publish_delegate/4]).

-include_lib("kz_amqp_util.hrl").

-define(APIKEY, <<"delegate">>).
-type maybe_key() :: kz_term:api_ne_binary().

-type maybe_key() :: kz_term:ne_binary() | 'undefined'.
-ifdef(TEST).
-export([build_binding/2
]).
-endif.

-define(DELEGATE_ROUTING_KEY(App, Key)
,list_to_binary([?APIKEY, "."
,kz_amqp_util:encode(App), "."
,kz_amqp_util:encode(Key)
])
).
-define(DELEGATE_ROUTING_KEY(App)
,list_to_binary([?APIKEY, ".", kz_amqp_util:encode(App)])
).
%%------------------------------------------------------------------------------
%% @doc Get all API definitions of this module.
%% @end
%%------------------------------------------------------------------------------
-spec api_definitions() -> kapi_definition:apis().
api_definitions() ->
[delegate_definition()
].

-define(DELEGATE_HEADERS, [<<"Delegate-Message">>]).
-define(OPTIONAL_DELEGATE_HEADERS, []).
-define(DELEGATE_VALUES, [{<<"Event-Category">>, <<"delegate">>}
,{<<"Event-Name">>, <<"job">>}
]).
-define(DELEGATE_TYPES, []).
%%------------------------------------------------------------------------------
%% @doc Get API definition of the given `Name'.
%% @see api_definitions/0
%% @end
%%------------------------------------------------------------------------------
-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(<<"delegate">>) ->
delegate_definition().

-spec delegate_definition() -> kapi_definition:api().
delegate_definition() ->
EventName = <<"job">>,
Category = <<"delegate">>,
Setters = [{fun kapi_definition:set_name/2, EventName}
,{fun kapi_definition:set_friendly_name/2, <<"Delegate Job">>}
,{fun kapi_definition:set_description/2
,<<"Delegate JObj from one application to another application">>
}
,{fun kapi_definition:set_category/2, Category}
,{fun kapi_definition:set_build_fun/2, fun delegate/1}
,{fun kapi_definition:set_validate_fun/2, fun delegate_v/1}
,{fun kapi_definition:set_publish_fun/2, fun publish_delegate/2}
,{fun kapi_definition:set_binding/2, fun build_binding/2}
,{fun kapi_definition:set_required_headers/2, [<<"Delegate-Message">>
]}
,{fun kapi_definition:set_optional_headers/2, []}
,{fun kapi_definition:set_values/2
,kapi_definition:event_type_headers(Category, EventName)
}
,{fun kapi_definition:set_types/2, []}
],
kapi_definition:setters(Setters).

%%------------------------------------------------------------------------------
%% @doc Resume a Callflow's flow.
%% @doc Delegate JObj from one application to another application.
%% Takes {@link kz_term:proplist()}, creates JSON string or error.
%% @end
%%------------------------------------------------------------------------------
-spec delegate(kz_term:api_terms()) -> {'ok', iolist()} | {'error', string()}.
delegate(Prop) when is_list(Prop) ->
case delegate_v(Prop) of
'true' -> kz_api:build_message(Prop, ?DELEGATE_HEADERS, ?OPTIONAL_DELEGATE_HEADERS);
'false' -> {'error', "Proplist failed validation for authn_error"}
end;
delegate(JObj) -> delegate(kz_json:to_proplist(JObj)).
-spec delegate(kz_term:api_terms()) -> kz_api:api_formatter_return().
delegate(Req) ->
kapi_definition:build_message(Req, delegate_definition()).

-spec delegate_v(kz_term:api_terms()) -> boolean().
delegate_v(Prop) when is_list(Prop) ->
kz_api:validate(Prop, ?DELEGATE_HEADERS, ?DELEGATE_VALUES, ?DELEGATE_TYPES);
delegate_v(JObj) -> delegate_v(kz_json:to_proplist(JObj)).
delegate_v(Req) ->
kapi_definition:validate(Req, delegate_definition()).

-spec publish_delegate(kz_term:ne_binary(), kz_term:api_terms()) -> 'ok'.
publish_delegate(TargetApp, API) ->
publish_delegate(TargetApp, API, 'undefined').

-spec publish_delegate(kz_term:ne_binary(), kz_term:api_terms(), maybe_key()) -> 'ok'.
publish_delegate(TargetApp, API, Key) ->
publish_delegate(TargetApp, API, Key, ?DEFAULT_CONTENT_TYPE).

-spec publish_delegate(kz_term:ne_binary(), kz_term:api_terms(), maybe_key(), binary()) -> 'ok'.
publish_delegate(<<_/binary>> = TargetApp, API, Key, ContentType) ->
Definition = delegate_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))(TargetApp, Key)
,Payload
,ContentType
).

-spec bind_q(kz_term:ne_binary(), kz_term:proplist()) -> 'ok'.
bind_q(Q, Props) ->
Expand All @@ -69,10 +120,10 @@ bind_q(Q, Props) ->
bind_q(Q, App, Key).

-spec bind_q(kz_term:ne_binary(), kz_term:ne_binary(), maybe_key()) -> 'ok'.
bind_q(Q, <<_/binary>> = App, 'undefined') ->
kz_amqp_util:bind_q_to_kapps(Q, ?DELEGATE_ROUTING_KEY(App));
bind_q(Q, <<_/binary>> = App, <<_/binary>> = Key) ->
kz_amqp_util:bind_q_to_kapps(Q, ?DELEGATE_ROUTING_KEY(App, Key)).
bind_q(Q, <<_/binary>> = App, Key) ->
kz_amqp_util:bind_q_to_kapps(Q
,(kapi_definition:binding(delegate_definition()))(App, Key)
).

-spec unbind_q(kz_term:ne_binary(), kz_term:proplist()) -> 'ok'.
unbind_q(Q, Props) ->
Expand All @@ -81,10 +132,10 @@ unbind_q(Q, Props) ->
unbind_q(Q, App, Key).

-spec unbind_q(kz_term:ne_binary(), kz_term:ne_binary(), maybe_key()) -> 'ok'.
unbind_q(Q, <<_/binary>> = App, 'undefined') ->
kz_amqp_util:unbind_q_from_kapps(Q, ?DELEGATE_ROUTING_KEY(App));
unbind_q(Q, <<_/binary>> = App, <<_/binary>> = Key) ->
kz_amqp_util:unbind_q_from_kapps(Q, ?DELEGATE_ROUTING_KEY(App, Key)).
unbind_q(Q, <<_/binary>> = App, Key) ->
kz_amqp_util:unbind_q_from_kapps(Q
,(kapi_definition:binding(delegate_definition()))(App, Key)
).

%%------------------------------------------------------------------------------
%% @doc Declare the exchanges used by this API.
Expand All @@ -94,23 +145,8 @@ unbind_q(Q, <<_/binary>> = App, <<_/binary>> = Key) ->
declare_exchanges() ->
kz_amqp_util:kapps_exchange().

%%------------------------------------------------------------------------------
%% @doc Publish the JSON string to the proper Exchange.
%% @end
%%------------------------------------------------------------------------------

-spec publish_delegate(kz_term:ne_binary(), kz_term:api_terms()) -> 'ok'.
publish_delegate(TargetApp, API) ->
publish_delegate(TargetApp, API, 'undefined').

-spec publish_delegate(kz_term:ne_binary(), kz_term:api_terms(), maybe_key()) -> 'ok'.
publish_delegate(TargetApp, API, Key) ->
publish_delegate(TargetApp, API, Key, ?DEFAULT_CONTENT_TYPE).

-spec publish_delegate(kz_term:ne_binary(), kz_term:api_terms(), maybe_key(), binary()) -> 'ok'.
publish_delegate(<<_/binary>> = TargetApp, API, 'undefined', ContentType) ->
{'ok', Payload} = kz_api:prepare_api_payload(API, ?DELEGATE_VALUES, fun delegate/1),
kz_amqp_util:kapps_publish(?DELEGATE_ROUTING_KEY(TargetApp), Payload, ContentType);
publish_delegate(<<_/binary>> = TargetApp, API, Key, ContentType) ->
{'ok', Payload} = kz_api:prepare_api_payload(API, ?DELEGATE_VALUES, fun delegate/1),
kz_amqp_util:kapps_publish(?DELEGATE_ROUTING_KEY(TargetApp, Key), Payload, ContentType).
-spec build_binding(kz_term:ne_binary(), maybe_key()) -> kz_term:ne_binary().
build_binding(App, 'undefined') ->
<<"delegate.", (kz_amqp_util:encode(App))/binary>>;
build_binding(App, Key) ->
<<"delegate.", (kz_amqp_util:encode(App))/binary, ".", (kz_amqp_util:encode(Key))/binary>>.
35 changes: 35 additions & 0 deletions core/kazoo_amqp/test/kapi_delegate_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%%%-----------------------------------------------------------------------------
%%% @copyright (C) 2013-2020, 2600Hz
%%% @doc
%%% This Source Code Form is subject to the terms of the Mozilla Public
%%% License, v. 2.0. If a copy of the MPL was not distributed with this
%%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%%
%%% @end
%%%-----------------------------------------------------------------------------
-module(kapi_delegate_tests).

-include_lib("eunit/include/eunit.hrl").

build_binding_test_() ->
Cat = <<"delegate">>,
App = kz_binary:rand_hex(4),
Key = kz_binary:rand_hex(4),

[{"Must work with 'undefined' Key"
,?_assertEqual(<<Cat/binary, ".", App/binary>>
,kapi_delegate:build_binding(App, 'undefined')
)
}
,{"Must work with binary Key"
,?_assertEqual(<<Cat/binary, ".", App/binary, ".", Key/binary>>
,kapi_delegate:build_binding(App, Key)
)
}
,{"App must be a binary"
,?_assertException('error'
,'function_clause'
,kapi_delegate:build_binding('undefined', Key)
)
}
].

0 comments on commit 690f372

Please sign in to comment.