-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
95 lines (80 loc) · 2.05 KB
/
Copy pathexample_test.go
File metadata and controls
95 lines (80 loc) · 2.05 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package fipe_test
import (
"context"
"errors"
"fmt"
"log"
fipe "github.com/fipe-api/go-sdk"
)
func ExampleNew() {
// The free tier needs no token; pass one for higher rate limits.
client := fipe.New(fipe.WithSubscriptionToken("your-token"))
brands, err := client.Brands(context.Background(), fipe.Cars)
if err != nil {
log.Fatal(err)
}
fmt.Println(brands[0].Name)
}
func ExampleClient_Brands() {
client := fipe.New()
brands, err := client.Brands(context.Background(), fipe.Cars)
if err != nil {
log.Fatal(err)
}
for _, b := range brands {
fmt.Println(b.Code, b.Name)
}
}
func ExampleClient_Vehicle() {
client := fipe.New()
ctx := context.Background()
// Drill down brand -> model -> year -> price.
models, err := client.Models(ctx, fipe.Cars, "59")
if err != nil {
log.Fatal(err)
}
years, err := client.Years(ctx, fipe.Cars, "59", models[0].Code)
if err != nil {
log.Fatal(err)
}
vehicle, err := client.Vehicle(ctx, fipe.Cars, "59", models[0].Code, years[0].Code)
if err != nil {
log.Fatal(err)
}
fmt.Println(vehicle.Model, vehicle.Price)
}
func ExampleClient_VehicleByFipeCode() {
client := fipe.New()
vehicle, err := client.VehicleByFipeCode(context.Background(), fipe.Cars, "005340-6", "2014-3")
if errors.Is(err, fipe.ErrNotFound) {
log.Fatal("unknown FIPE code or year")
} else if err != nil {
log.Fatal(err)
}
fmt.Println(vehicle.Price, vehicle.ReferenceMonth)
}
func ExampleClient_HistoryByFipeCode() {
client := fipe.New()
vehicle, err := client.HistoryByFipeCode(context.Background(), fipe.Cars, "005340-6", "2014-3")
if err != nil {
log.Fatal(err)
}
for _, h := range vehicle.PriceHistory {
fmt.Println(h.Month, h.Price)
}
}
func ExampleWithReference() {
client := fipe.New()
ctx := context.Background()
// Query prices from a past monthly reference table.
refs, err := client.References(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println("latest table:", refs[0].Month)
brands, err := client.Brands(ctx, fipe.Cars, fipe.WithReference(308))
if err != nil {
log.Fatal(err)
}
fmt.Println(len(brands))
}