• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / reporting / 1571619767

13 Sep 2024 10:52AM UTC coverage: 85.292% (+0.06%) from 85.235%
1571619767

push

gitlab-ci

web-flow
Merge pull request #202 from mzedel/chore/deprecate

Chore/deprecate

2998 of 3515 relevant lines covered (85.29%)

14.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/client/deviceauth/client.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package deviceauth
15

16
import (
17
        "context"
18
        "encoding/json"
19
        "net/http"
20
        "strconv"
21
        "strings"
22
        "time"
23

24
        "github.com/pkg/errors"
25

26
        "github.com/mendersoftware/go-lib-micro/log"
27

28
        "github.com/mendersoftware/reporting/model"
29
        "github.com/mendersoftware/reporting/utils"
30
)
31

32
const (
33
        urlSearch      = "/api/internal/v1/devauth/tenants/:tid/devices"
34
        defaultPage    = 1
35
        defaultTimeout = 10 * time.Second
36
)
37

38
//go:generate ../../x/mockgen.sh
39
type Client interface {
40
        //GetDevices uses the search endpoint to get devices just by ids (not filters)
41
        GetDevices(
42
                ctx context.Context,
43
                tid string,
44
                deviceIDs []string,
45
        ) (map[string]DeviceAuthDevice, error)
46
}
47

48
type client struct {
49
        client  *http.Client
50
        urlBase string
51
}
52

53
func NewClient(urlBase string) Client {
13✔
54
        return &client{
13✔
55
                client:  &http.Client{},
13✔
56
                urlBase: urlBase,
13✔
57
        }
13✔
58
}
13✔
59

60
func (c *client) GetDevices(
61
        ctx context.Context,
62
        tid string,
63
        deviceIDs []string,
64
) (map[string]DeviceAuthDevice, error) {
18✔
65
        l := log.FromContext(ctx)
18✔
66

18✔
67
        url := utils.JoinURL(c.urlBase, urlSearch)
18✔
68
        url = strings.Replace(url, ":tid", tid, 1)
18✔
69

18✔
70
        ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
18✔
71
        defer cancel()
18✔
72

18✔
73
        req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
18✔
74
        if err != nil {
20✔
75
                return nil, errors.Wrapf(err, "failed to create request")
2✔
76
        }
2✔
77

78
        q := req.URL.Query()
16✔
79
        for _, deviceID := range deviceIDs {
41✔
80
                q.Add(model.AttrNameID, deviceID)
25✔
81
        }
25✔
82
        page := strconv.Itoa(defaultPage)
16✔
83
        perPage := strconv.Itoa(len(deviceIDs))
16✔
84
        q.Add("page", page)
16✔
85
        q.Add("per_page", perPage)
16✔
86
        req.URL.RawQuery = q.Encode()
16✔
87

16✔
88
        rsp, err := c.client.Do(req)
16✔
89
        if err != nil {
18✔
90
                return nil, errors.Wrapf(err, "failed to submit %s %s", req.Method, req.URL)
2✔
91
        }
2✔
92
        defer rsp.Body.Close()
14✔
93

14✔
94
        if rsp.StatusCode != http.StatusOK {
16✔
95
                err := errors.Errorf("%s %s request failed with status %v",
2✔
96
                        req.Method, req.URL, rsp.Status)
2✔
97
                l.Errorf(err.Error())
2✔
98
                return nil, err
2✔
99
        }
2✔
100

101
        dec := json.NewDecoder(rsp.Body)
12✔
102
        var devDevs []DeviceAuthDevice
12✔
103
        if err = dec.Decode(&devDevs); err != nil {
14✔
104
                return nil, errors.Wrap(err, "failed to parse request body")
2✔
105
        }
2✔
106

107
        devices := make(map[string]DeviceAuthDevice, len(devDevs))
10✔
108
        for _, d := range devDevs {
32✔
109
                d.LastCheckinDate = utils.TruncateToDay(d.LastCheckinDate)
22✔
110
                devices[d.ID] = d
22✔
111
        }
22✔
112
        return devices, nil
10✔
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc