Skip to content

Commit

Permalink
feat: remove deprecated json fields and query params (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed May 12, 2023
1 parent 49ac8c6 commit 0f2bbb2
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 603 deletions.
3 changes: 0 additions & 3 deletions pkg/api/apierrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func ResponseError(w http.ResponseWriter, r *http.Request, err error) {
ErrorCode: code,
ErrorMessage: err.Error(),
Details: details,

ErrorCodeDeprecated: code,
ErrorMessageDeprecated: err.Error(),
})
if err != nil {
panic(err)
Expand Down
41 changes: 1 addition & 40 deletions pkg/api/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func (ctl *AccountController) GetAccounts(w http.ResponseWriter, r *http.Request
len(sharedapi.GetQueryMap(r.URL.Query(), "metadata")) > 0 ||
r.URL.Query().Get("balance") != "" ||
r.URL.Query().Get(QueryKeyBalanceOperator) != "" ||
r.URL.Query().Get(QueryKeyBalanceOperatorDeprecated) != "" ||
r.URL.Query().Get(QueryKeyPageSize) != "" ||
r.URL.Query().Get(QueryKeyPageSizeDeprecated) != "" {
r.URL.Query().Get(QueryKeyPageSize) != "" {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("no other query params can be set with '%s'", QueryKeyCursor)))
return
Expand Down Expand Up @@ -79,43 +77,6 @@ func (ctl *AccountController) GetAccounts(w http.ResponseWriter, r *http.Request
WithMetadataFilter(token.MetadataFilter).
WithPageSize(token.PageSize)

} else if r.URL.Query().Get(QueryKeyCursorDeprecated) != "" {
if r.URL.Query().Get("after") != "" ||
r.URL.Query().Get("address") != "" ||
len(sharedapi.GetQueryMap(r.URL.Query(), "metadata")) > 0 ||
r.URL.Query().Get("balance") != "" ||
r.URL.Query().Get(QueryKeyBalanceOperator) != "" ||
r.URL.Query().Get(QueryKeyBalanceOperatorDeprecated) != "" ||
r.URL.Query().Get(QueryKeyPageSize) != "" ||
r.URL.Query().Get(QueryKeyPageSizeDeprecated) != "" {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("no other query params can be set with '%s'", QueryKeyCursorDeprecated)))
return
}

res, err := base64.RawURLEncoding.DecodeString(r.URL.Query().Get(QueryKeyCursorDeprecated))
if err != nil {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("invalid '%s' query param", QueryKeyCursorDeprecated)))
return
}

token := sqlstorage.AccPaginationToken{}
if err := json.Unmarshal(res, &token); err != nil {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("invalid '%s' query param", QueryKeyCursorDeprecated)))
return
}

accountsQuery = accountsQuery.
WithOffset(token.Offset).
WithAfterAddress(token.AfterAddress).
WithAddressFilter(token.AddressRegexpFilter).
WithBalanceFilter(token.BalanceFilter).
WithBalanceOperatorFilter(token.BalanceOperatorFilter).
WithMetadataFilter(token.MetadataFilter).
WithPageSize(token.PageSize)

} else {
balance := r.URL.Query().Get("balance")
if balance != "" {
Expand Down
54 changes: 18 additions & 36 deletions pkg/api/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,8 @@ func TestGetAccounts(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("no other query params can be set with '%s'", controllers.QueryKeyCursor),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: fmt.Sprintf("no other query params can be set with '%s'", controllers.QueryKeyCursor),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("no other query params can be set with '%s'", controllers.QueryKeyCursor),
}, err)
})

Expand All @@ -194,10 +192,8 @@ func TestGetAccounts(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
}, err)
})

Expand All @@ -210,10 +206,8 @@ func TestGetAccounts(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
}, err)
})

Expand Down Expand Up @@ -339,10 +333,8 @@ func TestGetAccounts(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid parameter 'balance', should be a number",
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: "invalid parameter 'balance', should be a number",
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid parameter 'balance', should be a number",
}, err)
})

Expand All @@ -356,10 +348,8 @@ func TestGetAccounts(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidBalanceOperator.Error(),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: controllers.ErrInvalidBalanceOperator.Error(),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidBalanceOperator.Error(),
}, err)
})

Expand Down Expand Up @@ -391,10 +381,8 @@ func TestGetAccountsWithPageSize(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidPageSize.Error(),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: controllers.ErrInvalidPageSize.Error(),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidPageSize.Error(),
}, err)
})
t.Run("page size over maximum", func(t *testing.T) {
Expand Down Expand Up @@ -508,10 +496,8 @@ func TestGetAccount(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid account address format",
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: "invalid account address format",
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid account address format",
}, err)
})

Expand Down Expand Up @@ -560,10 +546,8 @@ func TestPostAccountMetadata(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid account address format",
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: "invalid account address format",
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid account address format",
}, err)
})

Expand All @@ -574,10 +558,8 @@ func TestPostAccountMetadata(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
assert.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid metadata format",
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: "invalid metadata format",
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid metadata format",
}, err)
})

Expand Down
33 changes: 1 addition & 32 deletions pkg/api/controllers/balance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (ctl *BalanceController) GetBalances(w http.ResponseWriter, r *http.Request
if r.URL.Query().Get(QueryKeyCursor) != "" {
if r.URL.Query().Get("after") != "" ||
r.URL.Query().Get("address") != "" ||
r.URL.Query().Get(QueryKeyPageSize) != "" ||
r.URL.Query().Get(QueryKeyPageSizeDeprecated) != "" {
r.URL.Query().Get(QueryKeyPageSize) != "" {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("no other query params can be set with '%s'", QueryKeyCursor)))
return
Expand All @@ -68,36 +67,6 @@ func (ctl *BalanceController) GetBalances(w http.ResponseWriter, r *http.Request
WithAddressFilter(token.AddressRegexpFilter).
WithPageSize(token.PageSize)

} else if r.URL.Query().Get(QueryKeyCursorDeprecated) != "" {
if r.URL.Query().Get("after") != "" ||
r.URL.Query().Get("address") != "" ||
r.URL.Query().Get(QueryKeyPageSize) != "" ||
r.URL.Query().Get(QueryKeyPageSizeDeprecated) != "" {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("no other query params can be set with '%s'", QueryKeyCursorDeprecated)))
return
}

res, err := base64.RawURLEncoding.DecodeString(r.URL.Query().Get(QueryKeyCursorDeprecated))
if err != nil {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("invalid '%s' query param", QueryKeyCursorDeprecated)))
return
}

token := sqlstorage.BalancesPaginationToken{}
if err := json.Unmarshal(res, &token); err != nil {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("invalid '%s' query param", QueryKeyCursorDeprecated)))
return
}

balancesQuery = balancesQuery.
WithOffset(token.Offset).
WithAfterAddress(token.AfterAddress).
WithAddressFilter(token.AddressRegexpFilter).
WithPageSize(token.PageSize)

} else {
pageSize, err := getPageSize(w, r)
if err != nil {
Expand Down
52 changes: 1 addition & 51 deletions pkg/api/controllers/ledger_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ func (ctl *LedgerController) GetLogs(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get(QueryKeyCursor) != "" {
if r.URL.Query().Get("after") != "" ||
r.URL.Query().Get(QueryKeyStartTime) != "" ||
r.URL.Query().Get(QueryKeyStartTimeDeprecated) != "" ||
r.URL.Query().Get(QueryKeyEndTime) != "" ||
r.URL.Query().Get(QueryKeyEndTimeDeprecated) != "" ||
r.URL.Query().Get(QueryKeyPageSize) != "" ||
r.URL.Query().Get(QueryKeyPageSizeDeprecated) != "" {
r.URL.Query().Get(QueryKeyPageSize) != "" {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("no other query params can be set with '%s'", QueryKeyCursor)))
return
Expand All @@ -98,39 +95,6 @@ func (ctl *LedgerController) GetLogs(w http.ResponseWriter, r *http.Request) {
WithEndTimeFilter(token.EndTime).
WithPageSize(token.PageSize)

} else if r.URL.Query().Get(QueryKeyCursorDeprecated) != "" {
if r.URL.Query().Get("after") != "" ||
r.URL.Query().Get(QueryKeyStartTime) != "" ||
r.URL.Query().Get(QueryKeyStartTimeDeprecated) != "" ||
r.URL.Query().Get(QueryKeyEndTime) != "" ||
r.URL.Query().Get(QueryKeyEndTimeDeprecated) != "" ||
r.URL.Query().Get(QueryKeyPageSize) != "" ||
r.URL.Query().Get(QueryKeyPageSizeDeprecated) != "" {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("no other query params can be set with '%s'", QueryKeyCursorDeprecated)))
return
}

res, err := base64.RawURLEncoding.DecodeString(r.URL.Query().Get(QueryKeyCursorDeprecated))
if err != nil {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("invalid '%s' query param", QueryKeyCursorDeprecated)))
return
}

token := sqlstorage.LogsPaginationToken{}
if err := json.Unmarshal(res, &token); err != nil {
apierrors.ResponseError(w, r, ledger.NewValidationError(
fmt.Sprintf("invalid '%s' query param", QueryKeyCursorDeprecated)))
return
}

logsQuery = logsQuery.
WithAfterID(token.AfterID).
WithStartTimeFilter(token.StartTime).
WithEndTimeFilter(token.EndTime).
WithPageSize(token.PageSize)

} else {
var err error
var afterIDParsed uint64
Expand All @@ -151,13 +115,6 @@ func (ctl *LedgerController) GetLogs(w http.ResponseWriter, r *http.Request) {
return
}
}
if r.URL.Query().Get(QueryKeyStartTimeDeprecated) != "" {
startTimeParsed, err = time.Parse(time.RFC3339, r.URL.Query().Get(QueryKeyStartTimeDeprecated))
if err != nil {
apierrors.ResponseError(w, r, ErrInvalidStartTimeDeprecated)
return
}
}

if r.URL.Query().Get(QueryKeyEndTime) != "" {
endTimeParsed, err = time.Parse(time.RFC3339, r.URL.Query().Get(QueryKeyEndTime))
Expand All @@ -166,13 +123,6 @@ func (ctl *LedgerController) GetLogs(w http.ResponseWriter, r *http.Request) {
return
}
}
if r.URL.Query().Get(QueryKeyEndTimeDeprecated) != "" {
endTimeParsed, err = time.Parse(time.RFC3339, r.URL.Query().Get(QueryKeyEndTimeDeprecated))
if err != nil {
apierrors.ResponseError(w, r, ErrInvalidEndTimeDeprecated)
return
}
}

pageSize, err := getPageSize(w, r)
if err != nil {
Expand Down
36 changes: 12 additions & 24 deletions pkg/api/controllers/ledger_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,8 @@ func TestGetLogs(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid 'after' query param",
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: "invalid 'after' query param",
ErrorCode: apierrors.ErrValidation,
ErrorMessage: "invalid 'after' query param",
}, err)
})

Expand Down Expand Up @@ -223,10 +221,8 @@ func TestGetLogs(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidStartTime.Error(),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: controllers.ErrInvalidStartTime.Error(),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidStartTime.Error(),
}, err)
})

Expand All @@ -239,10 +235,8 @@ func TestGetLogs(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidEndTime.Error(),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: controllers.ErrInvalidEndTime.Error(),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: controllers.ErrInvalidEndTime.Error(),
}, err)
})

Expand All @@ -267,10 +261,8 @@ func TestGetLogs(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("no other query params can be set with '%s'", controllers.QueryKeyCursor),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: fmt.Sprintf("no other query params can be set with '%s'", controllers.QueryKeyCursor),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("no other query params can be set with '%s'", controllers.QueryKeyCursor),
}, err)
})

Expand All @@ -283,10 +275,8 @@ func TestGetLogs(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
}, err)
})

Expand All @@ -299,10 +289,8 @@ func TestGetLogs(t *testing.T) {
err := sharedapi.ErrorResponse{}
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCodeDeprecated: apierrors.ErrValidation,
ErrorMessageDeprecated: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
ErrorCode: apierrors.ErrValidation,
ErrorMessage: fmt.Sprintf("invalid '%s' query param", controllers.QueryKeyCursor),
}, err)
})

Expand Down
Loading

0 comments on commit 0f2bbb2

Please sign in to comment.