diff --git a/src/bank/teller/init.go b/src/bank/teller/init.go index 4eb5975..438bf51 100644 --- a/src/bank/teller/init.go +++ b/src/bank/teller/init.go @@ -1,7 +1,6 @@ package teller import ( - "bufio" "context" _ "embed" "fmt" @@ -9,7 +8,6 @@ import ( "net/http" "os" "slices" - "strings" "text/template" ) @@ -21,14 +19,6 @@ var ( ) func Init(ctx context.Context) error { - reader := bufio.NewReader(os.Stdin) - - if Token == "" { - } else if fmt.Println("Token already exists; are you sure [nY]?"); false { - } else if text, _ := reader.ReadString('\n'); !strings.Contains(text, "Y") { - return fmt.Errorf("token already exists") - } - environment := "development" if sandbox := !slices.Contains(os.Args, "forreal"); sandbox { environment = "sandbox" @@ -67,7 +57,7 @@ func Init(ctx context.Context) error { select { case <-ctx.Done(): case newToken := <-newTokens: - return fmt.Errorf("not impl: %q => token.txt", newToken) + return fmt.Errorf("not impl: %q >> token.txt", newToken) } return ctx.Err() diff --git a/src/bank/teller/teller.go b/src/bank/teller/teller.go index f71108c..5b6b4bb 100644 --- a/src/bank/teller/teller.go +++ b/src/bank/teller/teller.go @@ -26,7 +26,7 @@ var ( //go:embed private_key.pem privateKey []byte //go:embed token.txt - Token string + Tokens string ) func New() (Client, error) { @@ -36,17 +36,26 @@ func New() (Client, error) { func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) { var result []bank.Account - err := c.get(ctx, "https://api.teller.io/accounts", &result) - return result, err + for _, token := range strings.Fields(Tokens) { + var more []bank.Account + if err := c.get(ctx, "https://api.teller.io/accounts", token, &more); err != nil { + return nil, err + } + for i := range more { + more[i].Token = token + } + result = append(result, more...) + } + return result, nil } func (c Client) Transactions(ctx context.Context, a bank.Account) ([]bank.Transaction, error) { var result []bank.Transaction - err := c.get(ctx, "https://api.teller.io/accounts/"+a.Account+"/transactions", &result) + err := c.get(ctx, "https://api.teller.io/accounts/"+a.Account+"/transactions", a.Token, &result) return result, err } -func (c Client) get(ctx context.Context, url string, ptr interface{}) error { +func (c Client) get(ctx context.Context, url, token string, ptr interface{}) error { httpc := &http.Client{ Timeout: time.Second, Transport: &http.Transport{ @@ -59,7 +68,7 @@ func (c Client) get(ctx context.Context, url string, ptr interface{}) error { if err != nil { return err } - req.SetBasicAuth(strings.TrimSpace(Token), "") + req.SetBasicAuth(token, "") req = req.WithContext(ctx) resp, err := httpc.Do(req) diff --git a/src/bank/teller/teller_integration_test.go b/src/bank/teller/teller_integration_test.go index 5c839aa..d37272c 100644 --- a/src/bank/teller/teller_integration_test.go +++ b/src/bank/teller/teller_integration_test.go @@ -10,7 +10,7 @@ import ( ) func Test(t *testing.T) { - teller.Token = "test_token_bfu2cyvq3il6o" + teller.Tokens = "test_token_bfu2cyvq3il6o" c, err := teller.New() if err != nil { diff --git a/src/bank/teller/teller_manual_test.go b/src/bank/teller/teller_manual_test.go index 970d60d..cd56fed 100644 --- a/src/bank/teller/teller_manual_test.go +++ b/src/bank/teller/teller_manual_test.go @@ -13,7 +13,7 @@ import ( ) func TestIntegration(t *testing.T) { - teller.Token = "test_token_bfu2cyvq3il6o" + teller.Tokens = "test_token_bfu2cyvq3il6o" //curl --cert certificate.pem --cert-key private_key.pem --auth test_token_bfu2cyvq3il6o: https://api.teller.io/accounts cert, err := tls.LoadX509KeyPair("./certificate.pem", "./private_key.pem") diff --git a/src/bank/types.go b/src/bank/types.go index afc36cc..a204e11 100644 --- a/src/bank/types.go +++ b/src/bank/types.go @@ -13,6 +13,7 @@ type Account struct { } `json:"institution"` Name string `json:"last_four"` Account string `json:"id"` + Token string `json:"-"` } type Transaction struct {