|
| 1 | +package fipe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | +) |
| 8 | + |
| 9 | +// References lists the FIPE monthly reference tables, newest first. |
| 10 | +func (c *Client) References(ctx context.Context) ([]Reference, error) { |
| 11 | + var out []Reference |
| 12 | + if err := c.get(ctx, "/references", &out); err != nil { |
| 13 | + return nil, err |
| 14 | + } |
| 15 | + return out, nil |
| 16 | +} |
| 17 | + |
| 18 | +// Brands lists the brands available for a vehicle type. |
| 19 | +func (c *Client) Brands(ctx context.Context, vt VehicleType, opts ...RequestOption) ([]Brand, error) { |
| 20 | + var out []Brand |
| 21 | + path := fmt.Sprintf("/%s/brands", vt) |
| 22 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + return out, nil |
| 26 | +} |
| 27 | + |
| 28 | +// Models lists the models of a brand. |
| 29 | +func (c *Client) Models(ctx context.Context, vt VehicleType, brandID string, opts ...RequestOption) ([]Model, error) { |
| 30 | + var out []Model |
| 31 | + path := fmt.Sprintf("/%s/brands/%s/models", vt, url.PathEscape(brandID)) |
| 32 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + return out, nil |
| 36 | +} |
| 37 | + |
| 38 | +// Years lists the model-year variants of a model. |
| 39 | +func (c *Client) Years(ctx context.Context, vt VehicleType, brandID, modelID string, opts ...RequestOption) ([]Year, error) { |
| 40 | + var out []Year |
| 41 | + path := fmt.Sprintf("/%s/brands/%s/models/%s/years", vt, url.PathEscape(brandID), url.PathEscape(modelID)) |
| 42 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + return out, nil |
| 46 | +} |
| 47 | + |
| 48 | +// Vehicle returns the FIPE price details for a brand, model and year. |
| 49 | +func (c *Client) Vehicle(ctx context.Context, vt VehicleType, brandID, modelID, yearID string, opts ...RequestOption) (*Vehicle, error) { |
| 50 | + var out Vehicle |
| 51 | + path := fmt.Sprintf("/%s/brands/%s/models/%s/years/%s", vt, url.PathEscape(brandID), url.PathEscape(modelID), url.PathEscape(yearID)) |
| 52 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + return &out, nil |
| 56 | +} |
| 57 | + |
| 58 | +// YearsByBrand lists all model years available for a brand. |
| 59 | +func (c *Client) YearsByBrand(ctx context.Context, vt VehicleType, brandID string, opts ...RequestOption) ([]Year, error) { |
| 60 | + var out []Year |
| 61 | + path := fmt.Sprintf("/%s/brands/%s/years", vt, url.PathEscape(brandID)) |
| 62 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + return out, nil |
| 66 | +} |
| 67 | + |
| 68 | +// ModelsByBrandYear lists the models of a brand available for a given year. |
| 69 | +func (c *Client) ModelsByBrandYear(ctx context.Context, vt VehicleType, brandID, yearID string, opts ...RequestOption) ([]Model, error) { |
| 70 | + var out []Model |
| 71 | + path := fmt.Sprintf("/%s/brands/%s/years/%s/models", vt, url.PathEscape(brandID), url.PathEscape(yearID)) |
| 72 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + return out, nil |
| 76 | +} |
| 77 | + |
| 78 | +// YearsByFipeCode lists the model-year variants of a vehicle by its FIPE code |
| 79 | +// (e.g. "005340-6"). |
| 80 | +func (c *Client) YearsByFipeCode(ctx context.Context, vt VehicleType, fipeCode string, opts ...RequestOption) ([]Year, error) { |
| 81 | + var out []Year |
| 82 | + path := fmt.Sprintf("/%s/%s/years", vt, url.PathEscape(fipeCode)) |
| 83 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + return out, nil |
| 87 | +} |
| 88 | + |
| 89 | +// VehicleByFipeCode returns the FIPE price details for a vehicle by its FIPE |
| 90 | +// code and year. |
| 91 | +func (c *Client) VehicleByFipeCode(ctx context.Context, vt VehicleType, fipeCode, yearID string, opts ...RequestOption) (*Vehicle, error) { |
| 92 | + var out Vehicle |
| 93 | + path := fmt.Sprintf("/%s/%s/years/%s", vt, url.PathEscape(fipeCode), url.PathEscape(yearID)) |
| 94 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + return &out, nil |
| 98 | +} |
| 99 | + |
| 100 | +// HistoryByFipeCode returns the vehicle details including its price history |
| 101 | +// across reference months. |
| 102 | +func (c *Client) HistoryByFipeCode(ctx context.Context, vt VehicleType, fipeCode, yearID string, opts ...RequestOption) (*Vehicle, error) { |
| 103 | + var out Vehicle |
| 104 | + path := fmt.Sprintf("/%s/%s/years/%s/history", vt, url.PathEscape(fipeCode), url.PathEscape(yearID)) |
| 105 | + if err := c.get(ctx, path, &out, opts...); err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + return &out, nil |
| 109 | +} |
0 commit comments