summaryrefslogtreecommitdiffstats
path: root/example_test.go
blob: 0af83e782f5557d1f9074f582a23299dff13cd68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package exchange_test

import (
	"bytes"
	"fmt"
	"os"
	"time"

	"github.com/bojanz/currency"

	"git.samanthony.xyz/exchange"
)

var btcDef = currency.Definition{
	"1000",
	8,
	"BTC",
}

func ExampleClient_Rate() {
	key, _ := readFile(".key")
	exc := exchange.NewClient(key, exchange.WithTTL(15*time.Minute))
	cadPerBtc, _ := exc.Rate("BTC", "CAD")
	currency.Register("BTC", btcDef)
	btc, _ := currency.NewAmount("1.23", "BTC")
	cad, _ := btc.Convert("CAD", cadPerBtc.String())
	fmt.Printf("%v = %v\n", btc.Round(), cad.Round())
}

func readFile(path string) (string, error) {
	buf, err := os.ReadFile(path)
	return string(bytes.TrimSpace(buf)), err
}