-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_unit_list.go
More file actions
95 lines (83 loc) · 1.94 KB
/
client_unit_list.go
File metadata and controls
95 lines (83 loc) · 1.94 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 mapon
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
maponv1 "github.com/way-platform/mapon-go/proto/gen/go/wayplatform/connect/mapon/v1"
)
// This API endpoint is documented in:
// docs/api/methods/08-method-unit.html
// ListUnits lists the units available for the current API key.
func (c *Client) ListUnits(
ctx context.Context,
request *maponv1.ListUnitsRequest,
) (_ *maponv1.ListUnitsResponse, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("mapon: list units: %w", err)
}
}()
params := url.Values{}
for _, id := range request.GetUnitIds() {
params.Add("unit_id[]", strconv.FormatInt(id, 10))
}
// Always include all available data
allIncludes := []string{
"io_din",
"fuel",
"fuel_tank",
"can",
"reefer",
"drivers",
"temperature",
"ambienttemp",
"humidity",
"device",
"supply_voltage",
"battery_voltage",
"battery_level_percentage",
"relays",
"weights",
"ignition",
"tachograph",
"altitude",
"technical_details",
"trailer_connections",
"ev_values",
}
for _, inc := range allIncludes {
params.Add("include[]", inc)
}
requestURL, err := url.Parse(c.baseURL + "/unit/list.json")
if err != nil {
return nil, fmt.Errorf("invalid request URL: %w", err)
}
requestURL.RawQuery = params.Encode()
httpRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), nil)
if err != nil {
return nil, err
}
httpRequest.Header.Set("User-Agent", getUserAgent())
httpResponse, err := c.httpClient(c.config).Do(httpRequest)
if err != nil {
return nil, err
}
defer func() { _ = httpResponse.Body.Close() }()
if httpResponse.StatusCode != http.StatusOK {
return nil, newResponseError(httpResponse)
}
data, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
units, err := ParseUnitsResponse(data)
if err != nil {
return nil, err
}
resp := &maponv1.ListUnitsResponse{}
resp.SetUnits(units)
return resp, nil
}