mirror of
https://github.com/valitydev/limiter.git
synced 2024-11-06 00:55:22 +00:00
TD-427: Add plan id wrap with hash (#16)
* added plan id wrap with hash * removed used mock * fixed * encoded
This commit is contained in:
parent
edbdccb67d
commit
daf099b471
@ -57,19 +57,19 @@ plan(PlanID, Batches, LimitContext) ->
|
||||
|
||||
-spec hold(plan_id(), batch(), lim_context()) -> ok | {error, invalid_request_error()}.
|
||||
hold(PlanID, Batch, LimitContext) ->
|
||||
do('Hold', construct_plan_change(PlanID, Batch), LimitContext).
|
||||
do('Hold', construct_plan_change(wrap_plan_id(PlanID), Batch), LimitContext).
|
||||
|
||||
-spec commit(plan_id(), [batch()], lim_context()) -> ok | {error, invalid_request_error()}.
|
||||
commit(PlanID, Batches, LimitContext) ->
|
||||
do('CommitPlan', construct_plan(PlanID, Batches), LimitContext).
|
||||
do('CommitPlan', construct_plan(wrap_plan_id(PlanID), Batches), LimitContext).
|
||||
|
||||
-spec rollback(plan_id(), [batch()], lim_context()) -> ok | {error, invalid_request_error()}.
|
||||
rollback(PlanID, Batches, LimitContext) ->
|
||||
do('RollbackPlan', construct_plan(PlanID, Batches), LimitContext).
|
||||
do('RollbackPlan', construct_plan(wrap_plan_id(PlanID), Batches), LimitContext).
|
||||
|
||||
-spec get_plan(plan_id(), lim_context()) -> {ok, [batch()]} | {error, notfound}.
|
||||
get_plan(PlanID, LimitContext) ->
|
||||
case call_accounter('GetPlan', {PlanID}, LimitContext) of
|
||||
case call_accounter('GetPlan', {wrap_plan_id(PlanID)}, LimitContext) of
|
||||
{ok, #accounter_PostingPlan{batch_list = BatchList}} ->
|
||||
{ok, decode_batch_list(BatchList)};
|
||||
{exception, #accounter_PlanNotFound{}} ->
|
||||
@ -157,6 +157,16 @@ construct_prototype(CurrencyCode, Description) ->
|
||||
|
||||
%%
|
||||
|
||||
wrap_plan_id(PlanID) ->
|
||||
%% Accounter requires max 64 byte plan id
|
||||
case byte_size(PlanID) < 64 of
|
||||
true ->
|
||||
%% For backward compatibility
|
||||
PlanID;
|
||||
false ->
|
||||
base64:encode(crypto:hash(sha384, PlanID))
|
||||
end.
|
||||
|
||||
call_accounter(Function, Args, LimitContext) ->
|
||||
WoodyContext = lim_context:woody_context(LimitContext),
|
||||
lim_client_woody:call(accounter, Function, Args, WoodyContext).
|
||||
|
@ -13,6 +13,7 @@
|
||||
-export([init_per_testcase/2]).
|
||||
-export([end_per_testcase/2]).
|
||||
|
||||
-export([commit_with_long_change_id/1]).
|
||||
-export([commit_with_default_exchange/1]).
|
||||
-export([partial_commit_with_exchange/1]).
|
||||
-export([commit_with_exchange/1]).
|
||||
@ -66,6 +67,7 @@ all() ->
|
||||
groups() ->
|
||||
[
|
||||
{default, [], [
|
||||
commit_with_long_change_id,
|
||||
commit_with_default_exchange,
|
||||
partial_commit_with_exchange,
|
||||
commit_with_exchange,
|
||||
@ -163,6 +165,19 @@ end_per_testcase(_Name, C) ->
|
||||
-define(LIMIT_CHANGE(ID), ?LIMIT_CHANGE(ID, ?CHANGE_ID)).
|
||||
-define(LIMIT_CHANGE(ID, ChangeID), #limiter_LimitChange{id = ID, change_id = gen_change_id(ID, ChangeID)}).
|
||||
|
||||
-spec commit_with_long_change_id(config()) -> _.
|
||||
commit_with_long_change_id(C) ->
|
||||
ID = configure_limit(?time_range_month(), ?global(), C),
|
||||
Context = ?payproc_ctx_invoice(?cash(10, <<"RUB">>)),
|
||||
LongBinary =
|
||||
<<
|
||||
"LongBinaryLongBinaryLongBinaryLongBinaryLongBinaryLong\n"
|
||||
" BinaryLongBinaryLongBinaryLongBinaryLongBinaryLongBinary"
|
||||
>>,
|
||||
ChangeID = <<LongBinary/binary, LongBinary/binary, LongBinary/binary, LongBinary/binary, LongBinary/binary>>,
|
||||
{ok, {vector, _}} = hold_and_commit(?LIMIT_CHANGE(ID, ChangeID), Context, ?config(client, C)),
|
||||
{ok, #limiter_Limit{amount = 10}} = lim_client:get(ID, Context, ?config(client, C)).
|
||||
|
||||
-spec commit_with_default_exchange(config()) -> _.
|
||||
commit_with_default_exchange(C) ->
|
||||
Rational = #base_Rational{p = 1000000, q = 100},
|
||||
|
Loading…
Reference in New Issue
Block a user