From 2fc4f66070e51c141472c988cc5022b479853094 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogov Date: Mon, 5 Jul 2021 16:54:21 +0300 Subject: [PATCH] deps: Update dmt_client (#550) * deps: Update dmt_client * fix: Fix opts passing * fix: Fix capi_domain * test: Fix tests for new ordering * refactor: Fix get_objects_by_type signature * refactor: Fix copypaste --- apps/capi/src/capi_domain.erl | 82 ++++++++----------- apps/capi/src/capi_handler_categories.erl | 2 +- apps/capi/src/capi_handler_countries.erl | 2 +- apps/capi/src/capi_handler_trade_blocs.erl | 4 +- .../test/capi_base_api_token_tests_SUITE.erl | 8 +- rebar.lock | 2 +- 6 files changed, 41 insertions(+), 59 deletions(-) diff --git a/apps/capi/src/capi_domain.erl b/apps/capi/src/capi_domain.erl index 3262190..98e5fa2 100644 --- a/apps/capi/src/capi_domain.erl +++ b/apps/capi/src/capi_domain.erl @@ -3,7 +3,6 @@ -include_lib("damsel/include/dmsl_domain_thrift.hrl"). -include_lib("damsel/include/dmsl_domain_config_thrift.hrl"). --export([get_categories/1]). -export([get_payment_institutions/1]). -export([get/2]). -export([get_objects_by_type/2]). @@ -12,65 +11,48 @@ -type ref() :: dmsl_domain_thrift:'Reference'(). -type data() :: _. --type category() :: #domain_CategoryObject{}. -type payment_institution() :: #domain_PaymentInstitutionObject{}. --spec get_categories(context()) -> {ok, [category()]}. -get_categories(Context) -> - get_objects_by_type(Context, 'category'). - --spec get_objects_by_type(context(), Type :: atom()) -> {ok, [dmsl_domain_thrift:'DomainObject'()]}. -get_objects_by_type(Context, Type) -> - #'Snapshot'{domain = Domain} = get_shapshot(Context), - Objects = maps:fold( - fun - ({Variant, _}, {Variant, Object}, Acc) when Variant =:= Type -> - [Object | Acc]; - (_, _, Acc) -> - Acc - end, - [], - Domain - ), - {ok, Objects}. - -spec get_payment_institutions(context()) -> {ok, [payment_institution()]}. get_payment_institutions(Context) -> - % All this mess was done to reduce requests to dominant. - % TODO rewrite this with dmt_client, cache, unicorns and rainbows. - #'Snapshot'{domain = Domain} = get_shapshot(Context), - Ref = {globals, #domain_GlobalsRef{}}, - {ok, {globals, #domain_GlobalsObject{data = Globals}}} = dmt_domain:get_object(Ref, Domain), - {ok, get_payment_institutions(Globals, Domain)}. + Opts = #{woody_context => Context}, -get_payment_institutions(#domain_Globals{payment_institutions = PaymentInstitutionRefs}, Domain) when - PaymentInstitutionRefs /= undefined --> - lists:map( - fun(Ref) -> - {ok, {payment_institution, Object}} = dmt_domain:get_object({payment_institution, Ref}, Domain), - Object + #'VersionedObject'{ + version = Version, + object = {globals, #domain_GlobalsObject{data = Globals}} + } = dmt_client:checkout_versioned_object(latest, globals(), Opts), + + PaymentInstitutionRefs = + case Globals#domain_Globals.payment_institutions of + undefined -> []; + List -> List end, - ordsets:to_list(PaymentInstitutionRefs) - ); -get_payment_institutions(#domain_Globals{payment_institutions = undefined}, _) -> - []. + + PaymentInstitutions = + lists:map( + fun(Ref) -> + {payment_institution, Object} = dmt_client:checkout_object(Version, {payment_institution, Ref}, Opts), + Object + end, + PaymentInstitutionRefs + ), + + {ok, PaymentInstitutions}. -spec get(ref(), context()) -> {ok, data()} | {error, not_found}. get(Ref, Context) -> - #'Snapshot'{domain = Domain} = get_shapshot(Context), - case dmt_domain:get_object(Ref, Domain) of - {ok, {_Type, C}} -> - {ok, C}; - error -> + try + {_Type, Object} = dmt_client:checkout_object(latest, Ref, #{woody_context => Context}), + {ok, Object} + catch + throw:#'ObjectNotFound'{} -> {error, not_found} end. -get_shapshot(Context) -> - get_shapshot(head(), Context). +globals() -> + {globals, #domain_GlobalsRef{}}. -get_shapshot(Reference, _Context) -> - dmt_client:checkout(Reference). - -head() -> - {'head', #'Head'{}}. +-spec get_objects_by_type(Type :: atom(), context()) -> {ok, [dmsl_domain_thrift:'DomainObject'()]}. +get_objects_by_type(Type, Context) -> + Objects = dmt_client:checkout_objects_by_type(latest, Type, #{woody_context => Context}), + {ok, Objects}. diff --git a/apps/capi/src/capi_handler_categories.erl b/apps/capi/src/capi_handler_categories.erl index a2dd53b..7528e32 100644 --- a/apps/capi/src/capi_handler_categories.erl +++ b/apps/capi/src/capi_handler_categories.erl @@ -19,7 +19,7 @@ prepare(OperationID = 'GetCategories', Req, Context = #{woody_context := WoodyCo {ok, capi_auth:authorize_operation(Prototypes, Context, Req)} end, Process = fun() -> - Categories = capi_utils:unwrap(capi_domain:get_categories(WoodyContext)), + Categories = capi_utils:unwrap(capi_domain:get_objects_by_type(category, WoodyContext)), {ok, {200, #{}, [decode_category(C) || C <- Categories]}} end, {ok, #{authorize => Authorize, process => Process}}; diff --git a/apps/capi/src/capi_handler_countries.erl b/apps/capi/src/capi_handler_countries.erl index a670a4a..4cddde5 100644 --- a/apps/capi/src/capi_handler_countries.erl +++ b/apps/capi/src/capi_handler_countries.erl @@ -22,7 +22,7 @@ prepare(_OperationID, _Req, _Context) -> {error, noimpl}. process_request('GetCountries', _Req, #{woody_context := WoodyContext}) -> - Countries = unwrap(capi_domain:get_objects_by_type(WoodyContext, country)), + Countries = unwrap(capi_domain:get_objects_by_type(country, WoodyContext)), {ok, {200, #{}, lists:map(fun decode_country_object/1, Countries)}}; process_request('GetCountryByID', Req, #{woody_context := WoodyContext}) -> CountryCode = capi_coder_utils:encode_country_code(maps:get(countryID, Req)), diff --git a/apps/capi/src/capi_handler_trade_blocs.erl b/apps/capi/src/capi_handler_trade_blocs.erl index 4fd4280..482009f 100644 --- a/apps/capi/src/capi_handler_trade_blocs.erl +++ b/apps/capi/src/capi_handler_trade_blocs.erl @@ -23,8 +23,8 @@ prepare(_OperationID, _Req, _Context) -> {error, noimpl}. process_request('GetTradeBlocs', _Req, #{woody_context := WoodyContext}) -> - Countries = unwrap(capi_domain:get_objects_by_type(WoodyContext, trade_bloc)), - {ok, {200, #{}, lists:map(fun decode_trade_bloc_object/1, Countries)}}; + TradeBlocs = unwrap(capi_domain:get_objects_by_type(trade_bloc, WoodyContext)), + {ok, {200, #{}, lists:map(fun decode_trade_bloc_object/1, TradeBlocs)}}; process_request('GetTradeBlocByID', Req, #{woody_context := WoodyContext}) -> Ref = {trade_bloc, #domain_TradeBlocRef{id = maps:get('tradeBlocID', Req)}}, case capi_domain:get(Ref, WoodyContext) of diff --git a/apps/capi/test/capi_base_api_token_tests_SUITE.erl b/apps/capi/test/capi_base_api_token_tests_SUITE.erl index 951be53..ff263d9 100644 --- a/apps/capi/test/capi_base_api_token_tests_SUITE.erl +++ b/apps/capi/test/capi_base_api_token_tests_SUITE.erl @@ -2382,14 +2382,14 @@ get_country_by_id_not_found_test(Config) -> get_countries_test(Config) -> ?assertEqual( {ok, [ - #{ - <<"id">> => <<"RUS">>, - <<"name">> => <<"Russia">> - }, #{ <<"id">> => <<"DEU">>, <<"name">> => <<"Germany">>, <<"tradeBlocs">> => [<<"EEA">>] + }, + #{ + <<"id">> => <<"RUS">>, + <<"name">> => <<"Russia">> } ]}, capi_client_countries:get_countries(?config(context, Config)) diff --git a/rebar.lock b/rebar.lock index 576a2fa..ebacaaa 100644 --- a/rebar.lock +++ b/rebar.lock @@ -43,7 +43,7 @@ 0}, {<<"dmt_client">>, {git,"https://github.com/rbkmoney/dmt_client.git", - {ref,"37f376e239a2182cbb2a7a052797e99955edbaad"}}, + {ref,"53924f4de479d6a5e05199b5d25931237150d72d"}}, 0}, {<<"dmt_core">>, {git,"https://github.com/rbkmoney/dmt_core.git",