-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_vehiclespecification.go
More file actions
81 lines (77 loc) · 2.47 KB
/
client_vehiclespecification.go
File metadata and controls
81 lines (77 loc) · 2.47 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
package mbz
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"github.com/way-platform/mbz-go/api/vehiclespecificationfleetv1"
mbzv1 "github.com/way-platform/mbz-go/proto/gen/go/wayplatform/connect/mbz/v1"
)
// GetVehicleSpecificationRequest is the request for [Client.GetVehicleSpecification].
type GetVehicleSpecificationRequest struct {
// VIN is the VIN (or FIN) of the vehicle (17 characters).
VIN string `json:"vin"`
// Locale is the market locale.
Locale string `json:"locale"`
}
// GetVehicleSpecification gets the vehicle marketing information for a given vehicle ID.
// This method requires API key authentication via [WithAPIKey].
func (c *Client) GetVehicleSpecification(
ctx context.Context,
request *GetVehicleSpecificationRequest,
opts ...ClientOption,
) (_ *mbzv1.VehicleSpecification, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("mbz: get vehicle specification: %w", err)
}
}()
cfg := c.config.with(opts...)
if request.VIN == "" {
return nil, fmt.Errorf("VIN is required")
}
values := url.Values{}
if request.Locale != "" {
values.Set("locale", request.Locale)
} else {
values.Set("locale", string(vehiclespecificationfleetv1.LocalesEnUS))
}
// Set all optional parameters to true to maximize data retrieval
values.Set("additionalSpecs", "true")
values.Set("optionsNullDescription", "true")
values.Set("options", "true")
values.Set("technicalData", "true")
values.Set("payloadNullValues", "true")
requestURL := fmt.Sprintf("%s/vehicles/%s", vehiclespecificationfleetv1.BaseURL, request.VIN)
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
if err != nil {
return nil, err
}
httpRequest.Header.Set("User-Agent", getUserAgent())
httpRequest.URL.RawQuery = values.Encode()
httpRequest.Header.Set("Accept", "application/json")
httpResponse, err := c.httpClient(cfg).Do(httpRequest)
if err != nil {
return nil, err
}
defer func() {
if closeErr := httpResponse.Body.Close(); closeErr != nil {
log.Printf("mbz: failed to close response body: %v", closeErr)
}
}()
if httpResponse.StatusCode != http.StatusOK {
return nil, newResponseError(httpResponse)
}
responseData, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
var openAPIResp vehiclespecificationfleetv1.VehicleSpecificationResponse
if err := json.Unmarshal(responseData, &openAPIResp); err != nil {
return nil, err
}
return vehicleDataToProto(openAPIResp.VehicleData), nil
}