Skip to content

Commit

Permalink
fix(nordigen): skip account on rate limit exceeded
Browse files Browse the repository at this point in the history
Crude way to fail gracefully when the rate limit is exceeded.

Ref: #76

commit-id:25e9d0c8
  • Loading branch information
martinohansen committed Sep 16, 2024
1 parent b7d7c5f commit 6e80284
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions reader/nordigen/nordigen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nordigen

import (
"errors"
"fmt"
"log/slog"
"regexp"
Expand All @@ -10,6 +11,8 @@ import (
"github.com/martinohansen/ynabber"
)

const rateLimitExceededStatusCode = 429

type Reader struct {
Config *ynabber.Config
Client *nordigen.Client
Expand Down Expand Up @@ -75,13 +78,13 @@ func (r Reader) Bulk() (t []ynabber.Transaction, err error) {
return nil, fmt.Errorf("failed to authorize: %w", err)
}

r.logger.Info("bulk reading", "accounts", len(req.Accounts))
r.logger.Info("", "accounts", len(req.Accounts))
for _, account := range req.Accounts {
logger := r.logger.With("account", account)
accountMetadata, err := r.Client.GetAccountMetadata(account)
if err != nil {
return nil, fmt.Errorf("failed to get account metadata: %w", err)
}
logger := r.logger.With("iban", accountMetadata.Iban)

// Handle expired, or suspended accounts by recreating the
// requisition.
Expand All @@ -100,6 +103,11 @@ func (r Reader) Bulk() (t []ynabber.Transaction, err error) {
logger.Info("reading transactions")
transactions, err := r.Client.GetAccountTransactions(string(account.ID))
if err != nil {
var apiErr *nordigen.APIError
if errors.As(err, &apiErr) && apiErr.StatusCode == rateLimitExceededStatusCode {
logger.Warn("rate limit exceeded, skipping account")
continue
}
return nil, fmt.Errorf("failed to get transactions: %w", err)
}

Expand Down

0 comments on commit 6e80284

Please sign in to comment.