fix cache test
parent
f69a850bd8
commit
4997264f4c
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -22,7 +23,11 @@ func New(client bank.Agg) Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) {
|
func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) {
|
||||||
if result := []bank.Account{}; fromCache("accounts", &result) == nil {
|
k := "accounts"
|
||||||
|
result := []bank.Account{}
|
||||||
|
if err := fromCache(k, &result); err != nil {
|
||||||
|
log.Printf("%q not in cache: %v", k, err)
|
||||||
|
} else {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,13 +36,17 @@ func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
toCache("accounts", result)
|
toCache(k, result)
|
||||||
|
|
||||||
return result, nil
|
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) {
|
||||||
if result := []bank.Transaction{}; fromCache(path.Join("accounts", a.Account), &result) == nil {
|
k := path.Join("accounts.d", a.Account)
|
||||||
|
result := []bank.Transaction{}
|
||||||
|
if err := fromCache(k, &result); err != nil {
|
||||||
|
log.Printf("%q not in cache: %v", k, err)
|
||||||
|
} else {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,7 +55,7 @@ func (c Client) Transactions(ctx context.Context, a bank.Account) ([]bank.Transa
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
toCache(path.Join("accounts", a.Account), result)
|
toCache(k, result)
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
@ -56,15 +65,25 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func toCache(k string, v interface{}) {
|
func toCache(k string, v interface{}) {
|
||||||
|
if err := _toCache(k, v); err != nil {
|
||||||
|
log.Printf("failed to cache %s: %v", k, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func _toCache(k string, v interface{}) error {
|
||||||
b, err := json.Marshal(v)
|
b, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p := path.Join(d, k)
|
p := path.Join(d, k)
|
||||||
|
os.MkdirAll(path.Dir(p), os.ModePerm)
|
||||||
if err := os.WriteFile(p, b, os.ModePerm); err != nil {
|
if err := os.WriteFile(p, b, os.ModePerm); err != nil {
|
||||||
os.Remove(p)
|
os.Remove(p)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromCache(k string, ptr interface{}) error {
|
func fromCache(k string, ptr interface{}) error {
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
c, err := teller.New()
|
tellerC, err := teller.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
client := cache.New(tellerC)
|
||||||
client := cache.New(c)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
|
@ -25,7 +24,7 @@ func Test(t *testing.T) {
|
||||||
i := i
|
i := i
|
||||||
client := client
|
client := client
|
||||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||||
accounts, err := c.Accounts(ctx)
|
accounts, err := client.Accounts(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -33,7 +32,7 @@ func Test(t *testing.T) {
|
||||||
for _, account := range accounts {
|
for _, account := range accounts {
|
||||||
account := account
|
account := account
|
||||||
t.Run(account.Account, func(t *testing.T) {
|
t.Run(account.Account, func(t *testing.T) {
|
||||||
transactions, err := c.Transactions(ctx, account)
|
transactions, err := client.Transactions(ctx, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,10 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gogs.inhome.blapointe.com/ana-ledger/src/bank"
|
"gogs.inhome.blapointe.com/ana-ledger/src/bank"
|
||||||
|
|
@ -56,7 +59,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(Token, "")
|
req.SetBasicAuth(strings.TrimSpace(Token), "")
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
resp, err := httpc.Do(req)
|
resp, err := httpc.Do(req)
|
||||||
|
|
@ -65,5 +68,9 @@ func (c Client) get(ctx context.Context, url string, ptr interface{}) error {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
return json.NewDecoder(resp.Body).Decode(ptr)
|
b, _ := io.ReadAll(resp.Body)
|
||||||
|
if err := json.Unmarshal(b, &ptr); err != nil {
|
||||||
|
return fmt.Errorf("cannot unmarshal: %w: %s", err, b)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
test_token_dwdwcxnnhh5du
|
token_2utqstwpn3pxwgvyno56hqdehq
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue