diff options
Diffstat (limited to 'example_test.go')
| -rw-r--r-- | example_test.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..0af83e7 --- /dev/null +++ b/example_test.go @@ -0,0 +1,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 +} |