Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions apps/capi/src/capi_domain.erl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
-module(capi_domain).

-include_lib("damsel/include/dmsl_domain_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_conf_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_conf_v2_thrift.hrl").

-export([head/0]).
-export([get_payment_institution/2]).
-export([get_payment_institutions/1]).
-export([get_country/2]).
-export([get/2]).
-export([get/3]).
-export([get_ext/3]).
Expand All @@ -15,6 +16,8 @@
-export([extract_type/1]).

-type processing_context() :: capi_handler:processing_context().
-type country_code() :: dmsl_domain_thrift:'CountryCode'().
-type country_object() :: dmsl_domain_thrift:'CountryObject'().
-type ref() :: dmsl_domain_thrift:'Reference'().
-type data() :: _.
-type revision() :: dmt_client:version().
Expand All @@ -29,7 +32,7 @@

-spec head() -> revision().
head() ->
dmt_client:get_last_version().
dmt_client:get_latest_version().

-spec get_payment_institution(payment_institution_ref(), processing_context()) ->
{ok, payment_institution()} | {error, not_found}.
Expand All @@ -46,10 +49,8 @@ get_payment_institutions(Context) ->
try
Opts = make_opts(Context),

#'domain_conf_VersionedObject'{
version = Version,
object = {globals, #domain_GlobalsObject{data = Globals}}
} = dmt_client:checkout_versioned_object(latest, globals(), Opts),
{Version, #domain_GlobalsObject{data = Globals}} =
unwrap_versioned(dmt_client:checkout_object(latest, globals(), Opts)),

PaymentInstitutionRefs =
case Globals#domain_Globals.payment_institutions of
Expand All @@ -60,17 +61,27 @@ get_payment_institutions(Context) ->
PaymentInstitutions =
lists:map(
fun(Ref) ->
{payment_institution, Object} = dmt_client:checkout_object(
Version, {payment_institution, Ref}, Opts
),
{_, Object} =
unwrap_versioned(dmt_client:checkout_object(Version, {payment_institution, Ref}, Opts)),
Object
end,
PaymentInstitutionRefs
),

{ok, PaymentInstitutions}
catch
throw:#'domain_conf_ObjectNotFound'{} ->
throw:#domain_conf_v2_ObjectNotFound{} ->
{error, not_found}
end.

-spec get_country(country_code(), processing_context() | undefined) -> {ok, country_object()} | {error, not_found}.
get_country(CountryCode, Context) ->
Ref = {country, #domain_CountryRef{id = CountryCode}},
try
get(Ref, Context)
catch
error:{woody_error, {internal, result_unexpected, _}} ->
%% NOTE Object not exists if woody fails to encode country code
{error, not_found}
end.

Expand All @@ -82,20 +93,22 @@ get(Ref, Context) ->
get(Ref, Revision, Context) ->
try
Opts = make_opts(Context),
{_Type, Object} = dmt_client:checkout_object(Revision, Ref, Opts),
{_, Object} = unwrap_versioned(dmt_client:checkout_object(Revision, Ref, Opts)),
{ok, Object}
catch
throw:#'domain_conf_ObjectNotFound'{} ->
throw:#domain_conf_v2_ObjectNotFound{} ->
{error, not_found}
end.

%% FIXME Naming. It supposed to return unwrapped `Data` from object
%% structured as `{Type, {Tag, Ref, Data}}`.
-spec get_ext(ref(), revision(), processing_context() | undefined) -> {ok, data()} | {error, not_found}.
get_ext(Ref, Revision, Context) ->
try
Opts = make_opts(Context),
{ok, extract_data(dmt_client:checkout_object(Revision, Ref, Opts))}
{ok, extract_data(unwrap_versioned(dmt_client:checkout_object(Revision, Ref, Opts)))}
catch
throw:#'domain_conf_ObjectNotFound'{} ->
throw:#domain_conf_v2_ObjectNotFound{} ->
{error, not_found}
end.

Expand Down Expand Up @@ -143,11 +156,23 @@ globals() ->
-spec get_objects_by_type(Type :: atom(), processing_context()) -> {ok, [dmsl_domain_thrift:'DomainObject'()]}.
get_objects_by_type(Type, Context) ->
Opts = make_opts(Context),
Objects = dmt_client:checkout_objects_by_type(latest, Type, Opts),
Objects = lists:map(
fun(VersionedObject) ->
{_, Object} = unwrap_versioned(VersionedObject),
Object
end,
dmt_client:checkout_objects_by_type(latest, Type, Opts)
),
{ok, Objects}.

-spec make_opts(processing_context()) -> dmt_client:opts().
make_opts(#{woody_context := WoodyContext}) ->
#{woody_context => WoodyContext};
make_opts(_) ->
#{}.

unwrap_versioned(#domain_conf_v2_VersionedObject{
info = #domain_conf_v2_VersionedObjectInfo{version = Version},
object = {_Type, Object}
}) ->
{Version, Object}.
3 changes: 1 addition & 2 deletions apps/capi/src/capi_handler_countries.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ process_request('GetCountries', _Req, Context) ->
{ok, {200, #{}, lists:map(fun decode_country_object/1, Countries)}};
process_request('GetCountryByID', Req, Context) ->
CountryCode = capi_coder_utils:encode_country_code(maps:get('countryID', Req)),
Ref = {country, #domain_CountryRef{id = CountryCode}},
case capi_domain:get(Ref, Context) of
case capi_domain:get_country(CountryCode, Context) of
{ok, CountryObject} ->
{ok, {200, #{}, decode_country_object(CountryObject)}};
{error, not_found} ->
Expand Down
56 changes: 50 additions & 6 deletions apps/capi/test/capi_ct_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("capi_dummy_data.hrl").
-include_lib("capi_token_keeper_data.hrl").
-include_lib("damsel/include/dmsl_domain_conf_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_thrift.hrl").
-include_lib("damsel/include/dmsl_base_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_conf_v2_thrift.hrl").

-export([init_suite/2]).
-export([init_suite/3]).
Expand Down Expand Up @@ -50,12 +48,44 @@ init_suite(Module, Config, CapiEnv) ->
WoodyApp = start_app(woody),
ServiceURLs = mock_services_(
[
{
'RepositoryClient',
{dmsl_domain_conf_v2_thrift, 'RepositoryClient'},
fun('CheckoutObject', {{version, ?INTEGER}, ObjectRef}) ->
case maps:get(ObjectRef, ?ALL_OBJECTS, undefined) of
undefined ->
woody_error:raise(business, #domain_conf_v2_ObjectNotFound{});
Object ->
{ok, dmt_wrap_object(Object)}
end
end
},
{
'Repository',
{dmsl_domain_conf_thrift, 'Repository'},
{dmsl_domain_conf_v2_thrift, 'Repository'},
fun
('Checkout', _) -> {ok, ?SNAPSHOT};
('PullRange', _) -> {ok, #{}}
('GetLatestVersion', _) ->
{ok, ?INTEGER};
(
'SearchFullObjects',
{#domain_conf_v2_SearchRequestParams{
query = ~b"*", version = ?INTEGER, limit = _, type = Type, continuation_token = _
}}
) ->
Result0 = lists:foldl(
fun
({{T, _}, Object}, Acc) when T =:= Type ->
[dmt_wrap_object(Object) | Acc];
(_, Acc) ->
Acc
end,
[],
maps:to_list(?ALL_OBJECTS)
),
Result1 = lists:reverse(Result0),
{ok, #domain_conf_v2_SearchFullResponse{
result = Result1, total_count = length(Result1), continuation_token = undefined
}}
end
}
],
Expand Down Expand Up @@ -321,3 +351,17 @@ add_prefix(Key, Value, {Prefix, Acc}) ->

get_blacklisted_keys_dir(Config) ->
filename:join(?config(data_dir, Config), "blacklisted_keys").

dmt_wrap_object(Object) ->
#domain_conf_v2_VersionedObject{
info = #domain_conf_v2_VersionedObjectInfo{
version = ?INTEGER,
changed_at = genlib_rfc3339:format(genlib_time:unow(), second),
changed_by = #domain_conf_v2_Author{
id = ?STRING,
name = ?STRING,
email = ?STRING
}
},
object = Object
}.
Loading
Loading