From 29ae8477ede89b3a13eea20f56b29ee5798a59a1 Mon Sep 17 00:00:00 2001 From: Andrey Mayorov Date: Thu, 14 Jun 2018 22:51:58 +0300 Subject: [PATCH] [WIP] A couple of renames / fixes --- apps/ff_withdraw/src/ff_adapter.erl | 12 +++ ...t_client.erl => ff_adapter_withdrawal.erl} | 86 +++++++++++-------- apps/ff_withdraw/src/ff_adpt.erl | 15 ---- apps/ff_withdraw/src/ff_adpt_withdrawal.erl | 22 ----- ....erl => ff_withdrawal_session_machine.erl} | 45 +++++----- apps/fistful/src/ff_woody_client.erl | 4 +- 6 files changed, 86 insertions(+), 98 deletions(-) create mode 100644 apps/ff_withdraw/src/ff_adapter.erl rename apps/ff_withdraw/src/{ff_adpt_client.erl => ff_adapter_withdrawal.erl} (65%) delete mode 100644 apps/ff_withdraw/src/ff_adpt.erl delete mode 100644 apps/ff_withdraw/src/ff_adpt_withdrawal.erl rename apps/ff_withdraw/src/{ff_adpt_session_machine.erl => ff_withdrawal_session_machine.erl} (78%) diff --git a/apps/ff_withdraw/src/ff_adapter.erl b/apps/ff_withdraw/src/ff_adapter.erl new file mode 100644 index 0000000..02f04b5 --- /dev/null +++ b/apps/ff_withdraw/src/ff_adapter.erl @@ -0,0 +1,12 @@ +%%% Withdrawal adapter generic +-module(ff_adapter). + +%% +%% Types +%% + +-type adapter() :: ff_woody_client:client(). +-type state() :: any(). + +-export_type([adapter/0]). +-export_type([state/0]). diff --git a/apps/ff_withdraw/src/ff_adpt_client.erl b/apps/ff_withdraw/src/ff_adapter_withdrawal.erl similarity index 65% rename from apps/ff_withdraw/src/ff_adpt_client.erl rename to apps/ff_withdraw/src/ff_adapter_withdrawal.erl index f24de3c..6a5fde8 100644 --- a/apps/ff_withdraw/src/ff_adpt_client.erl +++ b/apps/ff_withdraw/src/ff_adapter_withdrawal.erl @@ -5,7 +5,8 @@ -include_lib("dmsl/include/dmsl_withdrawals_provider_adapter_thrift.hrl"). %% API --export([process_withdrawal/5]). + +-export([process_withdrawal/4]). %% %% Internal types @@ -14,16 +15,27 @@ -type id() :: machinery:id(). -type identity_id() :: id(). --type withdrawal() :: ff_adpt_withdrawal:withdrawal(). --type destination() :: ff_adpt_withdrawal:destination(). --type cash() :: ff_adpt_withdrawal:cash(). +-type destination() :: ff_destination:destination(). +-type identity() :: ff_identity:identity(). +-type cash() :: ff_transfer:body(). --type adapter() :: ff_wthadpt:adapter(). +-type withdrawal() :: #{ + id => binary(), + destination => destination(), + cash => cash(), + sender => identity() | undefined, + receiver => identity() | undefined +}. + +-export_type([withdrawal/0]). + +-type adapter() :: ff_adapter:adapter(). -type intent() :: {finish, status()} | {sleep, timer()}. --type status() :: {success, trx_info()} | {failure, ff_adpt:failure()}. +-type status() :: {success, trx_info()} | {failure, failure()}. -type timer() :: dmsl_base_thrift:'Timer'(). -type trx_info() :: dmsl_domain_thrift:'TransactionInfo'(). --type adapter_state() :: ff_adpt:adapter_state(). +-type failure() :: dmsl_domain_thrift:'Failure'(). +-type adapter_state() :: ff_adapter:state(). -type process_result() :: {ok, intent(), adapter_state()} | {ok, intent()}. -type domain_withdrawal() :: dmsl_withdrawals_provider_adapter_thrift:'Withdrawal'(). @@ -31,22 +43,22 @@ -type domain_currency() :: dmsl_domain_thrift:'Currency'(). -type domain_destination() :: dmsl_withdrawals_provider_adapter_thrift:'Destination'(). -type domain_identity() :: dmsl_withdrawals_provider_adapter_thrift:'Identity'(). - --type backend() :: machinery:backend(_). +-type domain_internal_state() :: dmsl_withdrawals_provider_adapter_thrift:'InternalState'(). %% %% API %% --spec process_withdrawal(Adapter, Withdrawal, ASt, AOpt, Be) -> process_result() when - Adapter :: adapter(), - Withdrawal :: withdrawal(), - ASt :: adapter_state(), - AOpt :: map(), - Be :: backend(). -process_withdrawal(Adapter, Withdrawal, ASt, AOpt, Be) -> - DomainWithdrawal = build_and_encode_withdrawal(Withdrawal, Be), - {ok, Result} = call(Adapter, 'ProcessWithdrawal', [DomainWithdrawal, ASt, AOpt]), +-spec process_withdrawal(Adapter, Withdrawal, ASt, AOpt) -> + process_result() when + Adapter :: adapter(), + Withdrawal :: withdrawal(), + ASt :: adapter_state(), + AOpt :: map(). + +process_withdrawal(Adapter, Withdrawal, ASt, AOpt) -> + DomainWithdrawal = encode_withdrawal(Withdrawal), + {ok, Result} = call(Adapter, 'ProcessWithdrawal', [DomainWithdrawal, encode_adapter_state(ASt), AOpt]), decode_result(Result). %% @@ -59,28 +71,27 @@ call(Adapter, Function, Args) -> %% Encoders --spec build_and_encode_withdrawal(Withdrawal, Be) -> domain_withdrawal() when - Withdrawal :: withdrawal(), - Be :: backend(). -build_and_encode_withdrawal(Withdrawal, Be) -> +-spec encode_withdrawal(Withdrawal) -> domain_withdrawal() when + Withdrawal :: withdrawal(). +encode_withdrawal(Withdrawal) -> #{ - id := WId, + id := ID, cash := Cash, destination := Dest, sender := Sender, receiver := Receiver } = Withdrawal, #wthadpt_Withdrawal{ - id = WId, + id = ID, body = encode_body(Cash), destination = encode_destination(Dest), - sender = fetch_and_encode_identity(Sender, Be), - receiver = fetch_and_encode_identity(Receiver, Be) + sender = encode_identity(Sender), + receiver = encode_identity(Receiver) }. -spec encode_body(cash()) -> domain_cash(). -encode_body({Amount, CurrencyId}) -> - Currency = ff_currency:get(CurrencyId), +encode_body({Amount, CurrencyID}) -> + Currency = ff_currency:get(CurrencyID), DomainCurrency = encode_currency(Currency), #wthadpt_Cash{amount = Amount, currency = DomainCurrency}. @@ -114,18 +125,23 @@ encode_destination(Destination) -> masked_pan = MaskedPan }}. --spec fetch_and_encode_identity - (identity_id(), backend()) -> domain_identity(); - (undefined, backend()) -> undefined. -fetch_and_encode_identity(undefined, _Be) -> +-spec encode_identity + (identity_id()) -> domain_identity(); + (undefined) -> undefined. +encode_identity(undefined) -> undefined; -fetch_and_encode_identity(IdentityId, _Be) -> - % {ok, Identity} = ff_identity:get(IdentityId, Be), +encode_identity(IdentityID) -> % TODO: Add documents and contract fields #wthdm_Identity{ - id = IdentityId + id = IdentityID }. +-spec encode_adapter_state(adapter_state()) -> domain_internal_state(). +encode_adapter_state(undefined) -> + {nl, #msgpack_Nil{}}; +encode_adapter_state(ASt) -> + ASt. + -spec decode_result(dmsl_withdrawals_provider_adapter_thrift:'ProcessResult'()) -> process_result(). decode_result(#wthadpt_ProcessResult{intent = Intent, next_state = undefined}) -> {ok, decode_intent(Intent)}; diff --git a/apps/ff_withdraw/src/ff_adpt.erl b/apps/ff_withdraw/src/ff_adpt.erl deleted file mode 100644 index bc5734b..0000000 --- a/apps/ff_withdraw/src/ff_adpt.erl +++ /dev/null @@ -1,15 +0,0 @@ -%%% Withdrawal adapter generic --module(ff_adpt). - -%% -%% Types -%% - --type adapter() :: atom(). --type adapter_state() :: any(). - --type withdrawal() :: ff_adpt_withdrawal:withdrawal(). - --export_type([adapter/0]). --export_type([withdrawal/0]). --export_type([adapter_state/0]). diff --git a/apps/ff_withdraw/src/ff_adpt_withdrawal.erl b/apps/ff_withdraw/src/ff_adpt_withdrawal.erl deleted file mode 100644 index 7c4933f..0000000 --- a/apps/ff_withdraw/src/ff_adpt_withdrawal.erl +++ /dev/null @@ -1,22 +0,0 @@ --module(ff_adpt_withdrawal). - -%% -%% Types -%% - --type destination() :: ff_destination:destination(). --type identity() :: ff_identity:identity(). --type cash() :: ff_transfer:body(). - --type withdrawal() :: #{ - id => binary(), - destination => destination(), - cash => cash(), - sender => identity() | undefined, - receiver => identity() | undefined -}. - --export_type([destination/0]). --export_type([identity/0]). --export_type([cash/0]). --export_type([withdrawal/0]). diff --git a/apps/ff_withdraw/src/ff_adpt_session_machine.erl b/apps/ff_withdraw/src/ff_withdrawal_session_machine.erl similarity index 78% rename from apps/ff_withdraw/src/ff_adpt_session_machine.erl rename to apps/ff_withdraw/src/ff_withdrawal_session_machine.erl index 75eb8e5..10eb273 100644 --- a/apps/ff_withdraw/src/ff_adpt_session_machine.erl +++ b/apps/ff_withdraw/src/ff_withdrawal_session_machine.erl @@ -1,10 +1,10 @@ %%% %%% Withdrawal session machine %%% --module(ff_adpt_session_machine). +-module(ff_withdrawal_session_machine). -behaviour(machinery). --define(NS, adpt_session). +-define(NS, 'withdrawal/session'). %% API -export([create/4]). @@ -28,16 +28,14 @@ -type session_result() :: {success, trx_info()} | {failed, ff_adpt:failure()}. --type session_status() :: new - | active +-type session_status() :: active | {finished, session_result()}. -type ev() :: {created, session()} - | started | {next_state, ff_adpt:adapter_state()} | {finished, session_result()}. --type adapter() :: {ff_adpt:adapter(), map()}. +-type adapter() :: {ff_adapter:adapter(), Opts :: #{binary() => binary()}}. %% %% Internal types @@ -49,7 +47,7 @@ -type auxst() :: #{}. --type withdrawal() :: ff_adpt_withdrawal:withdrawal(). +-type withdrawal() :: ff_adapter_withdrawal:withdrawal(). -type machine() :: machinery:machine(ev(), auxst()). -type result() :: machinery:result(ev(), auxst()). -type handler_opts() :: machinery:handler_opts(). @@ -58,7 +56,7 @@ -type st() :: #{ session => session(), - adapter_state => ff_adpt:adapter_state() + adapter_state => ff_adapter:state() }. %% Pipeline @@ -69,23 +67,22 @@ %% API %% --spec create(Id, Adapter, Withdrawal, Be) -> {ok, session()} | Error when - Id :: id(), +-spec create(ID, Adapter, Withdrawal, Be) -> ok | Error when + ID :: id(), Adapter :: adapter(), Withdrawal :: withdrawal(), Be :: backend(), Error :: {error, exists}. -create(Id, Adapter, Withdrawal, Be) -> - Session = create_session(Id, Adapter, Withdrawal), +create(ID, Adapter, Withdrawal, Be) -> + Session = create_session(ID, Adapter, Withdrawal), do(fun () -> - ok = unwrap(machinery:start(?NS, Id, Session, Be)), - unwrap(get(Id, Be)) + unwrap(machinery:start(?NS, ID, Session, Be)) end). --spec get(id(), backend()) -> {ok, session()} | {error, any()}. -get(Id, Be) -> +-spec get(id(), backend()) -> {ok, session()} | {error, notfound}. +get(ID, Be) -> do(fun () -> - session(collapse(unwrap(machinery:get(?NS, Id, Be)))) + session(collapse(unwrap(machinery:get(?NS, ID, Be)))) end). %% @@ -97,7 +94,7 @@ get(Id, Be) -> init(Session, #{}, _, _Opts) -> #{ events => emit_ts_event({created, Session}), - action => timer_action({timeout, 0}) + action => continue }. -spec process_timeout(machine(), handler_args(), handler_opts()) -> @@ -121,8 +118,8 @@ process_session(#{session := #{status := active} = Session} = St) -> adapter := {Adapter, AdapterOpts}, withdrawal := Withdrawal } = Session, - ASt = maps:get(adapter_state, St, []), - case ff_adpt_client:process_withdrawal(Adapter, Withdrawal, ASt, AdapterOpts) of + ASt = maps:get(adapter_state, St, undefined), + case ff_adapter_withdrawal:process_withdrawal(Adapter, Withdrawal, ASt, AdapterOpts) of {ok, Intent, NextState} -> process_intent(Intent, NextState); {ok, Intent} -> @@ -147,12 +144,12 @@ process_intent({sleep, Timer}) -> %% -spec create_session(id(), adapter(), ff_adpt:withdrawal()) -> session(). -create_session(Id, Adapter, Withdrawal) -> +create_session(ID, Adapter, Withdrawal) -> #{ - id => Id, + id => ID, withdrawal => Withdrawal, adapter => Adapter, - status => new + status => active }. -spec set_session_status(session_status(), session()) -> session(). @@ -181,8 +178,6 @@ merge_event({_ID, _Ts, {ev, _Ts, EvBody}}, St) -> -spec merge_event_body(ev(), st()) -> st(). merge_event_body({created, Session}, St) -> St#{session => Session}; -merge_event_body(started, #{session := Session} = St) -> - St#{session => set_session_status(active, Session)}; merge_event_body({next_state, AdapterState}, St) -> St#{adapter_state => AdapterState}; merge_event_body({finished, Result}, #{session := Session} = St) -> diff --git a/apps/fistful/src/ff_woody_client.erl b/apps/fistful/src/ff_woody_client.erl index e7e6e54..d8bc0f3 100644 --- a/apps/fistful/src/ff_woody_client.erl +++ b/apps/fistful/src/ff_woody_client.erl @@ -56,8 +56,10 @@ new(Url) when is_binary(Url); is_list(Url) -> {ok, woody:result()} | {exception, woody_error:business_error()}. -call(ServiceID, Request) -> +call(ServiceID, Request) when is_atom(ServiceID) -> Client = get_service_client(ServiceID), + woody_client:call(Request, Client, ff_woody_ctx:get()); +call(Client, Request) when is_map(Client) -> woody_client:call(Request, Client, ff_woody_ctx:get()). %%