-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_vehicles.go
More file actions
170 lines (162 loc) · 5.11 KB
/
client_vehicles.go
File metadata and controls
170 lines (162 loc) · 5.11 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package rfms
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"github.com/way-platform/rfms-go/internal/convert/convertv2"
"github.com/way-platform/rfms-go/internal/convert/convertv4"
"github.com/way-platform/rfms-go/internal/openapi/rfmsv2oapi"
"github.com/way-platform/rfms-go/internal/openapi/rfmsv4oapi"
rfmsv5 "github.com/way-platform/rfms-go/proto/gen/go/wayplatform/connect/rfms/v5"
)
// VehiclesRequest is the request for the [Client.Vehicles] method.
type VehiclesRequest struct {
// LastVIN is the last VIN included in the previous response.
LastVIN string `json:"lastVin"`
}
// VehiclesResponse is the response for the [Client.Vehicles] method.
type VehiclesResponse struct {
// Vehicles in the response.
Vehicles []*rfmsv5.Vehicle `json:"vehicles"`
// MoreDataAvailable indicates if there is more data available.
MoreDataAvailable bool `json:"moreDataAvailable"`
}
// Vehicles implements the rFMS API method "GET /vehicles".
func (c *Client) Vehicles(
ctx context.Context,
request VehiclesRequest,
opts ...ClientOption,
) (_ VehiclesResponse, err error) {
cfg := c.config.with(opts...)
switch cfg.apiVersion {
case V2_1:
return c.vehiclesV2(ctx, request, cfg)
case V4:
return c.vehiclesV4(ctx, request, cfg)
default:
return VehiclesResponse{}, fmt.Errorf("unsupported API version")
}
}
func (c *Client) vehiclesV2(
ctx context.Context,
request VehiclesRequest,
cfg ClientConfig,
) (_ VehiclesResponse, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("rFMS v2 vehicles: %w", err)
}
}()
// Build query parameters
query := url.Values{}
if request.LastVIN != "" {
query.Set("lastVin", request.LastVIN)
}
// Build path with query parameters
path := "/vehicles"
if len(query) > 0 {
path += "?" + query.Encode()
}
// Apply per-request configuration overrides
fullURL := cfg.baseURL + path
// Create the request
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, nil)
if err != nil {
return VehiclesResponse{}, fmt.Errorf("http request: %w", err)
}
// Set headers
httpRequest.Header.Set("User-Agent", getUserAgent())
httpRequest.Header.Set("Accept", "application/vnd.fmsstandard.com.Vehicles.v2.1+json")
// Create HTTP client and make request
client := c.httpClient(cfg)
httpResponse, err := client.Do(httpRequest)
if err != nil {
return VehiclesResponse{}, fmt.Errorf("http request: %w", err)
}
defer func() {
_ = httpResponse.Body.Close()
}()
if httpResponse.StatusCode != http.StatusOK {
return VehiclesResponse{}, newHTTPError(httpResponse)
}
data, err := io.ReadAll(httpResponse.Body)
if err != nil {
return VehiclesResponse{}, fmt.Errorf("read response body: %w", err)
}
var response rfmsv2oapi.Vehicles
if err := json.Unmarshal(data, &response); err != nil {
return VehiclesResponse{}, fmt.Errorf("unmarshal v2 response body: %w", err)
}
result := VehiclesResponse{
MoreDataAvailable: response.MoreDataAvailable != nil && *response.MoreDataAvailable,
Vehicles: make([]*rfmsv5.Vehicle, 0, len(response.Vehicle)),
}
for _, vehicle := range response.Vehicle {
result.Vehicles = append(result.Vehicles, convertv2.Vehicle(&vehicle))
}
return result, nil
}
func (c *Client) vehiclesV4(
ctx context.Context,
request VehiclesRequest,
cfg ClientConfig,
) (_ VehiclesResponse, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("rFMS v4 vehicles: %w", err)
}
}()
// Build query parameters
query := url.Values{}
if request.LastVIN != "" {
query.Set("lastVin", request.LastVIN)
}
// Build path with query parameters
path := "/vehicles"
if len(query) > 0 {
path += "?" + query.Encode()
}
// Apply per-request configuration overrides
fullURL := cfg.baseURL + path
// Create the request
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, nil)
if err != nil {
return VehiclesResponse{}, fmt.Errorf("http request: %w", err)
}
// Set headers
httpRequest.Header.Set("User-Agent", getUserAgent())
httpRequest.Header.Set("Accept", "application/json; rfms=vehicles.v4.0")
// Create HTTP client and make request
client := c.httpClient(cfg)
httpResponse, err := client.Do(httpRequest)
if err != nil {
return VehiclesResponse{}, fmt.Errorf("http request: %w", err)
}
defer func() {
_ = httpResponse.Body.Close()
}()
if httpResponse.StatusCode != http.StatusOK {
return VehiclesResponse{}, newHTTPError(httpResponse)
}
data, err := io.ReadAll(httpResponse.Body)
if err != nil {
return VehiclesResponse{}, fmt.Errorf("read response body: %w", err)
}
slog.Debug("vehicles v4 response", "body", json.RawMessage(data))
var response rfmsv4oapi.VehicleResponseObject
if err := json.Unmarshal(data, &response); err != nil {
return VehiclesResponse{}, fmt.Errorf("unmarshal v4 response body: %w", err)
}
result := VehiclesResponse{
MoreDataAvailable: response.MoreDataAvailable != nil && *response.MoreDataAvailable,
Vehicles: make([]*rfmsv5.Vehicle, 0, len(response.VehicleResponse.Vehicles)),
}
for _, vehicle := range response.VehicleResponse.Vehicles {
result.Vehicles = append(result.Vehicles, convertv4.Vehicle(&vehicle))
}
return result, nil
}