-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_vehicles_compatibility.go
More file actions
88 lines (80 loc) · 2.49 KB
/
client_vehicles_compatibility.go
File metadata and controls
88 lines (80 loc) · 2.49 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
package mbz
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"github.com/way-platform/mbz-go/api/vehiclesv1"
)
// GetVehicleCompatibilityRequest is the request for [Client.GetVehicleCompatibility].
type GetVehicleCompatibilityRequest struct {
// VIN of the vehicle to get the compatibility for.
VIN string `json:"vin"`
}
// GetVehicleCompatibilityResponse is the response for [Client.GetVehicleCompatibility].
type GetVehicleCompatibilityResponse struct {
// VIN of the requested vehicle.
VIN string `json:"vin"`
// VehicleType is the type of the requested vehicle.
VehicleType string `json:"vehicleType,omitempty"`
// VehicleProvidesConnectivity indicates the base compatibility to data-services for the requested vehicle.
VehicleProvidesConnectivity bool `json:"vehicleProvidesConnectivity"`
// Services with the service availability.
Services []vehiclesv1.CompatibilityGenericService `json:"services"`
}
// GetVehicleCompatibility gets the compatibility of a vehicle.
func (c *Client) GetVehicleCompatibility(
ctx context.Context,
request *GetVehicleCompatibilityRequest,
opts ...ClientOption,
) (_ *GetVehicleCompatibilityResponse, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("mbz: get vehicle compatibility: %w", err)
}
}()
cfg := c.config.with(opts...)
requestURL, err := url.JoinPath(
c.baseURL,
"/v1/accounts/vehicles",
request.VIN,
"compatibilities",
)
if err != nil {
return nil, fmt.Errorf("invalid request URL: %w", err)
}
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
if err != nil {
return nil, err
}
httpRequest.Header.Set("User-Agent", getUserAgent())
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)
}
data, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
var responseBody vehiclesv1.CompatibilityResponse
if err := json.Unmarshal(data, &responseBody); err != nil {
return nil, err
}
return &GetVehicleCompatibilityResponse{
VIN: responseBody.VIN,
VehicleType: responseBody.VehicleType,
VehicleProvidesConnectivity: responseBody.VehicleProvidesConnectivity,
Services: responseBody.Services,
}, nil
}