teller accepts multi token in tokens.txt
cicd / ci (push) Failing after 17s Details

main
Bel LaPointe 2025-05-23 23:00:09 -06:00
parent 5e61378d63
commit 929d15c5b7
5 changed files with 19 additions and 19 deletions

View File

@ -1,7 +1,6 @@
package teller package teller
import ( import (
"bufio"
"context" "context"
_ "embed" _ "embed"
"fmt" "fmt"
@ -9,7 +8,6 @@ import (
"net/http" "net/http"
"os" "os"
"slices" "slices"
"strings"
"text/template" "text/template"
) )
@ -21,14 +19,6 @@ var (
) )
func Init(ctx context.Context) error { 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" environment := "development"
if sandbox := !slices.Contains(os.Args, "forreal"); sandbox { if sandbox := !slices.Contains(os.Args, "forreal"); sandbox {
environment = "sandbox" environment = "sandbox"
@ -67,7 +57,7 @@ func Init(ctx context.Context) error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
case newToken := <-newTokens: case newToken := <-newTokens:
return fmt.Errorf("not impl: %q => token.txt", newToken) return fmt.Errorf("not impl: %q >> token.txt", newToken)
} }
return ctx.Err() return ctx.Err()

View File

@ -26,7 +26,7 @@ var (
//go:embed private_key.pem //go:embed private_key.pem
privateKey []byte privateKey []byte
//go:embed token.txt //go:embed token.txt
Token string Tokens string
) )
func New() (Client, error) { func New() (Client, error) {
@ -36,17 +36,26 @@ func New() (Client, error) {
func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) { func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) {
var result []bank.Account var result []bank.Account
err := c.get(ctx, "https://api.teller.io/accounts", &result) for _, token := range strings.Fields(Tokens) {
return result, err 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) { func (c Client) Transactions(ctx context.Context, a bank.Account) ([]bank.Transaction, error) {
var result []bank.Transaction 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 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{ httpc := &http.Client{
Timeout: time.Second, Timeout: time.Second,
Transport: &http.Transport{ Transport: &http.Transport{
@ -59,7 +68,7 @@ func (c Client) get(ctx context.Context, url string, ptr interface{}) error {
if err != nil { if err != nil {
return err return err
} }
req.SetBasicAuth(strings.TrimSpace(Token), "") req.SetBasicAuth(token, "")
req = req.WithContext(ctx) req = req.WithContext(ctx)
resp, err := httpc.Do(req) resp, err := httpc.Do(req)

View File

@ -10,7 +10,7 @@ import (
) )
func Test(t *testing.T) { func Test(t *testing.T) {
teller.Token = "test_token_bfu2cyvq3il6o" teller.Tokens = "test_token_bfu2cyvq3il6o"
c, err := teller.New() c, err := teller.New()
if err != nil { if err != nil {

View File

@ -13,7 +13,7 @@ import (
) )
func TestIntegration(t *testing.T) { 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 //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") cert, err := tls.LoadX509KeyPair("./certificate.pem", "./private_key.pem")

View File

@ -13,6 +13,7 @@ type Account struct {
} `json:"institution"` } `json:"institution"`
Name string `json:"last_four"` Name string `json:"last_four"`
Account string `json:"id"` Account string `json:"id"`
Token string `json:"-"`
} }
type Transaction struct { type Transaction struct {