Skip to content

Commit

Permalink
Use knm_locality (2600hz#1915)
Browse files Browse the repository at this point in the history
* knm-locality: use knm_locality for metadata requests to phonebook

* knm-locality: move get_prefix to knm_locality

* knm-locality: fix Dialyzer error

* knm-locality: updated specs/types
  • Loading branch information
fenollp authored and jamesaimonetti committed May 16, 2016
1 parent c4edf1d commit 292deb9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 93 deletions.
84 changes: 19 additions & 65 deletions applications/crossbar/src/modules_v2/cb_phone_numbers_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@

-define(DEFAULT_COUNTRY, <<"US">>).
-define(KEY_PHONEBOOK_FREE_URL, <<"phonebook_url">>).
-define(KEY_PHONEBOOK_PAID_URL, <<"phonebook_url_premium">>).
-define(PREFIX, <<"prefix">>).
-define(LOCALITY, <<"locality">>).
-define(CHECK, <<"check">>).
Expand Down Expand Up @@ -520,13 +519,13 @@ find_prefix(Context) ->
'undefined' -> cb_context:add_system_error('bad_identifier', Context);
City ->
case get_prefix(City) of
{'ok', Data} ->
{'ok', JObj} ->
cb_context:set_resp_data(
cb_context:set_resp_status(Context, 'success')
,Data
,JObj
);
{'error', Error} ->
lager:error("error while prefix for city: ~p : ~p", [City, Error]),
lager:error("error while prefix for city ~s: ~p", [City, Error]),
cb_context:set_resp_data(
cb_context:set_resp_status(Context, 'error')
,Error
Expand Down Expand Up @@ -601,20 +600,11 @@ get_prefix(City) ->
'undefined' ->
{'error', <<"Unable to acquire numbers missing carrier url">>};
Url ->
Country = kapps_config:get_string(?PHONE_NUMBERS_CONFIG_CAT, <<"default_country">>, ?DEFAULT_COUNTRY),
ReqParam = kz_util:uri_encode(City),
case kz_http:get(lists:flatten([Url, "/", Country, "/city?pattern=", ReqParam])) of
{'ok', 200, _Headers, Body} ->
JObj = kz_json:decode(Body),
case kz_json:get_value(<<"data">>, JObj) of
'undefined' -> {'error', JObj};
Data -> {'ok', Data}
end;
{'ok', _Status, _Headers, Body} ->
{'error', kz_json:decode(Body)};
{'error', _Reason}=E ->
E
end
Country = kapps_config:get_string(?PHONE_NUMBERS_CONFIG_CAT
,<<"default_country">>
,?DEFAULT_COUNTRY
),
knm_locality:prefix(Url, Country, City)
end.

%%--------------------------------------------------------------------
Expand All @@ -625,21 +615,20 @@ get_prefix(City) ->
-spec fetch_locality(cb_context:context()) -> cb_context:context().
fetch_locality(Context) ->
Numbers = cb_context:req_value(Context, ?COLLECTION_NUMBERS),
Url = get_url(cb_context:req_value(Context, <<"quality">>)),
case fetch_locality(Numbers, Url) of
case knm_locality:fetch(Numbers) of
{'ok', Localities} ->
cb_context:set_resp_data(
cb_context:set_resp_status(Context, 'success')
,Localities
);
{'error', E} ->
crossbar_util:response('error', E, 500, Context)
{'error', 'lookup_failed'} ->
Msg = <<"number locality lookup failed">>,
crossbar_util:response('error', Msg, 500, Context);
{'error', 'missing_url'} ->
Msg = <<"could not get locality url">>,
crossbar_util:response('error', Msg, 500, Context)
end.

-spec get_url(any()) -> ne_binary().
get_url(<<"high">>) -> ?KEY_PHONEBOOK_PAID_URL;
get_url(_) -> ?KEY_PHONEBOOK_FREE_URL.

%% @private
-spec maybe_update_locality(cb_context:context()) -> cb_context:context().
maybe_update_locality(Context) ->
Expand All @@ -656,7 +645,7 @@ maybe_update_locality(Context) ->
cb_context:context().
update_locality(Context, []) -> Context;
update_locality(Context, Numbers) ->
case fetch_locality(Numbers, ?KEY_PHONEBOOK_FREE_URL) of
case knm_locality:fetch(Numbers) of
{'ok', Localities} ->
_ = kz_util:spawn(fun update_phone_numbers_locality/2, [Context, Localities]),
update_context_locality(Context, Localities);
Expand Down Expand Up @@ -711,41 +700,6 @@ update_phone_numbers_locality_fold(Key, Value, JObj) ->
_Else -> JObj
end.

%% @private
-spec fetch_locality(ne_binaries(), ne_binary()) -> {'ok', kz_json:object()} |
{'error', any()}.
fetch_locality(Numbers, PhonebookUrlType) ->
case kapps_config:get_string(?PHONE_NUMBERS_CONFIG_CAT, PhonebookUrlType) of
'undefined' ->
lager:error("could not get locality url ~s", [PhonebookUrlType]),
{'error', <<"could not get locality url">>};
URL ->
ReqBody = kz_json:set_value(<<"data">>, Numbers, kz_json:new()),
URI = lists:flatten([URL, $/, "locality", $/, "metadata"]),
Headers = [{'content_type', "application/json"}],
case kz_http:post(URI, Headers, kz_json:encode(ReqBody)) of
{'ok', 200, _Headers, Body} ->
handle_phonebook_resp(kz_json:decode(Body));
{'ok', _Status, _, _Body} ->
lager:error("number locality lookup failed: ~p ~p", [_Status, _Body]),
{'error', <<"number locality lookup failed">>};
{'error', _Reason} ->
lager:error("number locality lookup failed: ~p", [_Reason]),
{'error', <<"number locality lookup failed">>}
end
end.

%% @private
-spec handle_phonebook_resp(kz_json:object()) -> 'ok'.
handle_phonebook_resp(Resp) ->
case kz_json:get_value(<<"status">>, Resp) of
<<"success">> ->
{'ok', kz_json:get_value(<<"data">>, Resp, kz_json:new())};
_E ->
lager:error("number locality lookup failed, status: ~p", [_E]),
{'error', <<"number locality lookup failed">>}
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
Expand Down Expand Up @@ -871,7 +825,7 @@ reply_number_not_found(Context) ->
%% @end
%%--------------------------------------------------------------------
-type process_result() :: {kz_services:services(), kz_json:object()}.
-spec collection_process(cb_context:context(), ne_binary()) -> process_result().
-spec collection_process(cb_context:context(), ne_binary()) -> {ne_binary(), kz_services:services(), kz_json:object()}.
collection_process(Context, Action) ->
ReqData = cb_context:req_data(Context),
Numbers = kz_json:get_value(<<"numbers">>, ReqData),
Expand Down Expand Up @@ -904,7 +858,7 @@ collection_process(Context, Action, Numbers) ->

%% @private
-spec numbers_action(cb_context:context(), ne_binary(), ne_binaries()) ->
knm_number_return().
knm_numbers:numbers_return().
numbers_action(Context, ?ACTIVATE, Numbers) ->
Options = [{'auth_by', cb_context:auth_account_id(Context)}
,{'dry_run', not cb_context:accepting_charges(Context)}
Expand Down Expand Up @@ -932,7 +886,7 @@ numbers_action(Context, ?HTTP_DELETE, Numbers) ->

-spec fold_dry_runs([kz_services:services(), ...]) -> kz_json:object().
fold_dry_runs(ServicesList) ->
F = fun (Services, _Acc) -> kz_services:dry_run(Services) end,
F = fun(Services, _Acc) -> kz_services:dry_run(Services) end,
lists:foldl(F, [], ServicesList).

%%--------------------------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions core/kazoo_number_manager/src/knm_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
-spec should_permanently_delete() -> boolean().
-spec should_permanently_delete(boolean()) -> boolean().
-ifdef(TEST).
should_permanently_delete() ->
'false'.
should_permanently_delete(Default) ->
Default.
should_permanently_delete() -> 'false'.
should_permanently_delete(Default) -> Default.
-else.
should_permanently_delete() ->
should_permanently_delete('false').
Expand Down
63 changes: 40 additions & 23 deletions core/kazoo_number_manager/src/knm_locality.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-module(knm_locality).

-export([fetch/1]).
-export([prefix/3]).

-include("knm.hrl").

Expand All @@ -17,11 +18,8 @@
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec fetch(ne_binary() | ne_binaries()) ->
{'ok', kz_json:object()} |
{'error', 'lookup_failed' | 'missing_url'}.
fetch(Number) when is_binary(Number)->
fetch([Number]);
-spec fetch(ne_binaries()) -> {'ok', kz_json:object()} |
{'error', 'lookup_failed' | 'missing_url'}.
fetch(Numbers) when is_list(Numbers)->
case knm_config:locality_url() of
'undefined' ->
Expand All @@ -32,15 +30,45 @@ fetch(Numbers) when is_list(Numbers)->
handle_resp(Resp)
end.

%%--------------------------------------------------------------------
%% @public
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec prefix(ne_binary(), ne_binary(), ne_binary()) -> {'ok', kz_json:object()} |
{'error', kz_json:object()}.
prefix(Url, Country, City) ->
ReqParam = kz_util:uri_encode(City),
Uri = lists:flatten([Url, "/", Country, "/city?pattern=", ReqParam]),
case kz_http:get(Uri) of
{'ok', 200, _Headers, Body} ->
JObj = kz_json:decode(Body),
case kz_json:get_value(<<"data">>, JObj) of
'undefined' -> {'error', JObj};
Data -> {'ok', Data}
end;
{'ok', _Status, _Headers, Body} ->
{'error', kz_json:decode(Body)};
{'error', Reason} ->
JObj = kz_json:from_list([{<<"unknown error">>, kz_util:to_binary(Reason)}]),
{'error', JObj}
end.


%%%===================================================================
%%% Internal functions
%%%===================================================================

-spec fetch_req(ne_binaries(), ne_binary()) -> kz_http:http_ret().
fetch_req(Numbers, Url) ->
ReqBody = kz_json:set_value(<<"data">>, Numbers, kz_json:new()),
ReqBody = kz_json:from_list([{<<"data">>, Numbers}]),
Uri = <<Url/binary, "/locality/metadata">>,
kz_http:post(binary:bin_to_list(Uri), [], kz_json:encode(ReqBody)).
Headers = [{'content_type', "application/json"}
],
kz_http:post(binary_to_list(Uri), Headers, kz_json:encode(ReqBody)).

-spec handle_resp(kz_http:http_ret()) ->
{'ok', kz_json:object()} |
{'error', any()}.
-spec handle_resp(kz_http:http_ret()) -> {'ok', kz_json:object()} |
{'error', 'lookup_failed'}.
handle_resp({'error', Reason}) ->
lager:error("number locality lookup failed: ~p", [Reason]),
{'error', 'lookup_failed'};
Expand All @@ -50,19 +78,8 @@ handle_resp({'ok', _Status, _, _Body}) ->
lager:error("number locality lookup failed: ~p ~p", [_Status, _Body]),
{'error', 'lookup_failed'}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec handle_resp_body(kz_json:object()) ->
{'ok', kz_json:object()} |
{'error', 'lookup_failed'}.
-spec handle_resp_body(kz_json:object()) -> {'ok', kz_json:object()} |
{'error', 'lookup_failed'}.
handle_resp_body(Resp) ->
case kz_json:get_value(<<"status">>, Resp, <<"error">>) of
<<"success">> ->
Expand Down
2 changes: 1 addition & 1 deletion core/kazoo_number_manager/src/knm_numbers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

-include("knm.hrl").

-type number_return() :: {ne_binary(), knm_number_return()}.
-type number_return() :: {ne_binary(), knm_number:knm_number_return()}.
-type numbers_return() :: [number_return()].

%%--------------------------------------------------------------------
Expand Down

0 comments on commit 292deb9

Please sign in to comment.