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
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()

View File

@ -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)

View File

@ -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 {

View File

@ -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")

View File

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