Skip to content

Commit 1cab802

Browse files
committed
feat: add Idempotency-Hit header
1 parent 8338362 commit 1cab802

31 files changed

+756
-64
lines changed

docs/api/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,12 @@ Idempotency-Key: string
972972

973973
<h3 id="add-metadata-to-an-account-responseschema">Response Schema</h3>
974974

975+
### Response Headers
976+
977+
|Status|Header|Type|Format|Description|
978+
|---|---|---|---|---|
979+
|204|Idempotency-Hit|string||Indicates that the request was processed using an idempotency key that was already used|
980+
975981
<aside class="warning">
976982
To perform this operation, you must be authenticated by means of one of the following methods:
977983
Authorization ( Scopes: ledger:write )
@@ -1025,6 +1031,12 @@ Delete metadata by key
10251031

10261032
<h3 id="delete-metadata-by-key-responseschema">Response Schema</h3>
10271033

1034+
### Response Headers
1035+
1036+
|Status|Header|Type|Format|Description|
1037+
|---|---|---|---|---|
1038+
|204|Idempotency-Hit|string||Indicates that the request was processed using an idempotency key that was already used|
1039+
10281040
<aside class="warning">
10291041
To perform this operation, you must be authenticated by means of one of the following methods:
10301042
Authorization ( Scopes: ledger:write )
@@ -1476,6 +1488,12 @@ Idempotency-Key: string
14761488
|200|[OK](https://tools.ietf.org/html/rfc7231#section-6.3.1)|OK|[V2CreateTransactionResponse](#schemav2createtransactionresponse)|
14771489
|default|Default|Error|[V2ErrorResponse](#schemav2errorresponse)|
14781490

1491+
### Response Headers
1492+
1493+
|Status|Header|Type|Format|Description|
1494+
|---|---|---|---|---|
1495+
|200|Idempotency-Hit|string||Indicates that the request was processed using an idempotency key that was already used|
1496+
14791497
<aside class="warning">
14801498
To perform this operation, you must be authenticated by means of one of the following methods:
14811499
Authorization ( Scopes: ledger:write )
@@ -1666,6 +1684,12 @@ Idempotency-Key: string
16661684

16671685
<h3 id="set-the-metadata-of-a-transaction-by-its-id-responseschema">Response Schema</h3>
16681686

1687+
### Response Headers
1688+
1689+
|Status|Header|Type|Format|Description|
1690+
|---|---|---|---|---|
1691+
|204|Idempotency-Hit|string||Indicates that the request was processed using an idempotency key that was already used|
1692+
16691693
<aside class="warning">
16701694
To perform this operation, you must be authenticated by means of one of the following methods:
16711695
Authorization ( Scopes: ledger:write )
@@ -1682,6 +1706,7 @@ POST http://localhost:8080/v2/{ledger}/transactions/{id}/revert HTTP/1.1
16821706
Host: localhost:8080
16831707
Content-Type: application/json
16841708
Accept: application/json
1709+
Idempotency-Key: string
16851710
16861711
```
16871712

@@ -1707,6 +1732,7 @@ Accept: application/json
17071732
|force|query|boolean|false|Force revert|
17081733
|atEffectiveDate|query|boolean|false|Revert transaction at effective date of the original tx|
17091734
|dryRun|query|boolean|false|Set the dryRun mode. dry run mode doesn't add the logs to the database or publish a message to the message broker.|
1735+
|Idempotency-Key|header|string|false|Use an idempotency key|
17101736
|body|body|[V2RevertTransactionRequest](#schemav2reverttransactionrequest)|false|none|
17111737

17121738
> Example responses
@@ -1809,6 +1835,12 @@ Accept: application/json
18091835
|201|[Created](https://tools.ietf.org/html/rfc7231#section-6.3.2)|OK|[V2CreateTransactionResponse](#schemav2createtransactionresponse)|
18101836
|default|Default|Error|[V2ErrorResponse](#schemav2errorresponse)|
18111837

1838+
### Response Headers
1839+
1840+
|Status|Header|Type|Format|Description|
1841+
|---|---|---|---|---|
1842+
|201|Idempotency-Hit|string||Indicates that the request was processed using an idempotency key that was already used|
1843+
18121844
<aside class="warning">
18131845
To perform this operation, you must be authenticated by means of one of the following methods:
18141846
Authorization ( Scopes: ledger:write )

internal/api/v2/controllers_accounts_add_metadata.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ func addAccountMetadata(w http.ResponseWriter, r *http.Request) {
2323
}
2424

2525
common.WithBody(w, r, func(m metadata.Metadata) {
26-
_, _, err = l.SaveAccountMetadata(r.Context(), getCommandParameters(r, ledger.SaveAccountMetadata{
26+
_, idempotencyHit, err := l.SaveAccountMetadata(r.Context(), getCommandParameters(r, ledger.SaveAccountMetadata{
2727
Address: address,
2828
Metadata: m,
2929
}))
3030
if err != nil {
3131
common.HandleCommonWriteErrors(w, r, err)
3232
return
3333
}
34+
if idempotencyHit {
35+
w.Header().Set("Idempotency-Hit", "true")
36+
}
3437

3538
api.NoContent(w)
3639
})

internal/api/v2/controllers_accounts_delete_metadata.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ func deleteAccountMetadata(w http.ResponseWriter, r *http.Request) {
1919
return
2020
}
2121

22-
if _, _, err := common.LedgerFromContext(r.Context()).
22+
_, idempotencyHit, err := common.LedgerFromContext(r.Context()).
2323
DeleteAccountMetadata(
2424
r.Context(),
2525
getCommandParameters(r, ledger.DeleteAccountMetadata{
2626
Address: address,
2727
Key: chi.URLParam(r, "key"),
2828
}),
29-
); err != nil {
29+
)
30+
if err != nil {
31+
3032
common.HandleCommonWriteErrors(w, r, err)
3133
return
3234
}
35+
if idempotencyHit {
36+
w.Header().Set("Idempotency-Hit", "true")
37+
}
3338

3439
api.NoContent(w)
3540
}

internal/api/v2/controllers_transactions_add_metadata.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) {
2424
return
2525
}
2626

27-
if _, _, err := l.SaveTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.SaveTransactionMetadata{
27+
_, idempotencyHit, err := l.SaveTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.SaveTransactionMetadata{
2828
TransactionID: txID,
2929
Metadata: m,
30-
})); err != nil {
30+
}))
31+
if err != nil {
3132
switch {
3233
case errors.Is(err, ledgercontroller.ErrNotFound):
3334
api.NotFound(w, err)
@@ -36,6 +37,9 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) {
3637
}
3738
return
3839
}
40+
if idempotencyHit {
41+
w.Header().Set("Idempotency-Hit", "true")
42+
}
3943

4044
api.NoContent(w)
4145
})

internal/api/v2/controllers_transactions_create.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
3737
return
3838
}
3939

40-
_, res, _, err := l.CreateTransaction(r.Context(), getCommandParameters(r, *createTransaction))
40+
_, res, idempotencyHit, err := l.CreateTransaction(r.Context(), getCommandParameters(r, *createTransaction))
4141
if err != nil {
4242
switch {
4343
case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}):
@@ -59,6 +59,9 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
5959
}
6060
return
6161
}
62+
if idempotencyHit {
63+
w.Header().Set("Idempotency-Hit", "true")
64+
}
6265

6366
api.Ok(w, renderTransaction(r, res.Transaction))
6467
})

internal/api/v2/controllers_transactions_delete_metadata.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) {
2323

2424
metadataKey := chi.URLParam(r, "key")
2525

26-
if _, _, err := l.DeleteTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.DeleteTransactionMetadata{
26+
_, idempotencyHit, err := l.DeleteTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.DeleteTransactionMetadata{
2727
TransactionID: txID,
2828
Key: metadataKey,
29-
})); err != nil {
29+
}))
30+
if err != nil {
3031
common.HandleCommonWriteErrors(w, r, err)
3132
return
3233
}
34+
if idempotencyHit {
35+
w.Header().Set("Idempotency-Hit", "true")
36+
}
3337

3438
api.NoContent(w)
3539
}

internal/api/v2/controllers_transactions_revert.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) {
3636
}
3737
}
3838

39-
_, ret, _, err := l.RevertTransaction(
39+
_, ret, idempotencyHit, err := l.RevertTransaction(
4040
r.Context(),
4141
getCommandParameters(r, ledgercontroller.RevertTransaction{
4242
Force: api.QueryParamBool(r, "force"),
@@ -58,6 +58,9 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) {
5858
}
5959
return
6060
}
61+
if idempotencyHit {
62+
w.Header().Set("Idempotency-Hit", "true")
63+
}
6164

6265
api.Created(w, renderTransaction(r, ret.RevertTransaction))
6366
}

internal/controller/ledger/controller_default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ func (ctrl *DefaultController) saveTransactionMetadata(ctx context.Context, stor
498498
}, nil
499499
}
500500

501-
func (ctrl *DefaultController) SaveTransactionMetadata(ctx context.Context, parameters Parameters[SaveTransactionMetadata]) (*ledger.Log, bool,error) {
501+
func (ctrl *DefaultController) SaveTransactionMetadata(ctx context.Context, parameters Parameters[SaveTransactionMetadata]) (*ledger.Log, bool, error) {
502502
log, _, idempotencyHit, err := ctrl.saveTransactionMetadataLp.forgeLog(ctx, ctrl.store, parameters, ctrl.saveTransactionMetadata)
503503
return log, idempotencyHit, err
504504
}

internal/controller/ledger/controller_with_too_many_client_handling.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *ControllerWithTooManyClientHandling) CreateTransaction(ctx context.Cont
4747
var (
4848
log *ledger.Log
4949
createdTransaction *ledger.CreatedTransaction
50-
idempotencyHit bool
50+
idempotencyHit bool
5151
err error
5252
)
5353
err = handleRetry(ctx, c.tracer, c.delayCalculator, func(ctx context.Context) error {
@@ -61,7 +61,7 @@ func (c *ControllerWithTooManyClientHandling) RevertTransaction(ctx context.Cont
6161
var (
6262
log *ledger.Log
6363
revertedTransaction *ledger.RevertedTransaction
64-
idempotencyHit bool
64+
idempotencyHit bool
6565
err error
6666
)
6767
err = handleRetry(ctx, c.tracer, c.delayCalculator, func(ctx context.Context) error {
@@ -74,9 +74,9 @@ func (c *ControllerWithTooManyClientHandling) RevertTransaction(ctx context.Cont
7474

7575
func (c *ControllerWithTooManyClientHandling) SaveTransactionMetadata(ctx context.Context, parameters Parameters[SaveTransactionMetadata]) (*ledger.Log, bool, error) {
7676
var (
77-
log *ledger.Log
77+
log *ledger.Log
7878
idempotencyHit bool
79-
err error
79+
err error
8080
)
8181
err = handleRetry(ctx, c.tracer, c.delayCalculator, func(ctx context.Context) error {
8282
log, idempotencyHit, err = c.Controller.SaveTransactionMetadata(ctx, parameters)
@@ -88,9 +88,9 @@ func (c *ControllerWithTooManyClientHandling) SaveTransactionMetadata(ctx contex
8888

8989
func (c *ControllerWithTooManyClientHandling) SaveAccountMetadata(ctx context.Context, parameters Parameters[SaveAccountMetadata]) (*ledger.Log, bool, error) {
9090
var (
91-
log *ledger.Log
91+
log *ledger.Log
9292
idempotencyHit bool
93-
err error
93+
err error
9494
)
9595
err = handleRetry(ctx, c.tracer, c.delayCalculator, func(ctx context.Context) error {
9696
log, idempotencyHit, err = c.Controller.SaveAccountMetadata(ctx, parameters)
@@ -102,9 +102,9 @@ func (c *ControllerWithTooManyClientHandling) SaveAccountMetadata(ctx context.Co
102102

103103
func (c *ControllerWithTooManyClientHandling) DeleteTransactionMetadata(ctx context.Context, parameters Parameters[DeleteTransactionMetadata]) (*ledger.Log, bool, error) {
104104
var (
105-
log *ledger.Log
105+
log *ledger.Log
106106
idempotencyHit bool
107-
err error
107+
err error
108108
)
109109
err = handleRetry(ctx, c.tracer, c.delayCalculator, func(ctx context.Context) error {
110110
log, idempotencyHit, err = c.Controller.DeleteTransactionMetadata(ctx, parameters)
@@ -116,9 +116,9 @@ func (c *ControllerWithTooManyClientHandling) DeleteTransactionMetadata(ctx cont
116116

117117
func (c *ControllerWithTooManyClientHandling) DeleteAccountMetadata(ctx context.Context, parameters Parameters[DeleteAccountMetadata]) (*ledger.Log, bool, error) {
118118
var (
119-
log *ledger.Log
119+
log *ledger.Log
120120
idempotencyHit bool
121-
err error
121+
err error
122122
)
123123
err = handleRetry(ctx, c.tracer, c.delayCalculator, func(ctx context.Context) error {
124124
log, idempotencyHit, err = c.Controller.DeleteAccountMetadata(ctx, parameters)

internal/controller/ledger/controller_with_traces.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func (c *ControllerWithTraces) CreateTransaction(ctx context.Context, parameters
367367
createdTransaction *ledger.CreatedTransaction
368368
log *ledger.Log
369369
err error
370-
idempotencyHit bool
370+
idempotencyHit bool
371371
)
372372
_, err = tracing.TraceWithMetric(
373373
ctx,
@@ -391,7 +391,7 @@ func (c *ControllerWithTraces) RevertTransaction(ctx context.Context, parameters
391391
revertedTransaction *ledger.RevertedTransaction
392392
log *ledger.Log
393393
err error
394-
idempotencyHit bool
394+
idempotencyHit bool
395395
)
396396
_, err = tracing.TraceWithMetric(
397397
ctx,
@@ -413,8 +413,8 @@ func (c *ControllerWithTraces) RevertTransaction(ctx context.Context, parameters
413413
func (c *ControllerWithTraces) SaveTransactionMetadata(ctx context.Context, parameters Parameters[SaveTransactionMetadata]) (*ledger.Log, bool, error) {
414414
var (
415415
idempotencyHit bool
416-
err error
417-
log *ledger.Log
416+
err error
417+
log *ledger.Log
418418
)
419419
_, err = tracing.TraceWithMetric(
420420
ctx,
@@ -435,8 +435,8 @@ func (c *ControllerWithTraces) SaveTransactionMetadata(ctx context.Context, para
435435
func (c *ControllerWithTraces) SaveAccountMetadata(ctx context.Context, parameters Parameters[SaveAccountMetadata]) (*ledger.Log, bool, error) {
436436
var (
437437
idempotencyHit bool
438-
err error
439-
log *ledger.Log
438+
err error
439+
log *ledger.Log
440440
)
441441
_, err = tracing.TraceWithMetric(
442442
ctx,
@@ -458,8 +458,8 @@ func (c *ControllerWithTraces) SaveAccountMetadata(ctx context.Context, paramete
458458
func (c *ControllerWithTraces) DeleteTransactionMetadata(ctx context.Context, parameters Parameters[DeleteTransactionMetadata]) (*ledger.Log, bool, error) {
459459
var (
460460
idempotencyHit bool
461-
err error
462-
log *ledger.Log
461+
err error
462+
log *ledger.Log
463463
)
464464
_, err = tracing.TraceWithMetric(
465465
ctx,
@@ -481,8 +481,8 @@ func (c *ControllerWithTraces) DeleteTransactionMetadata(ctx context.Context, pa
481481
func (c *ControllerWithTraces) DeleteAccountMetadata(ctx context.Context, parameters Parameters[DeleteAccountMetadata]) (*ledger.Log, bool, error) {
482482
var (
483483
idempotencyHit bool
484-
err error
485-
log *ledger.Log
484+
err error
485+
log *ledger.Log
486486
)
487487
_, err = tracing.TraceWithMetric(
488488
ctx,

0 commit comments

Comments
 (0)