Skip to content

Commit 9802c4a

Browse files
committed
switch to resty v3
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 4948e28 commit 9802c4a

File tree

6 files changed

+41
-37
lines changed

6 files changed

+41
-37
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ require (
88
github.com/jessevdk/go-flags v1.6.1
99
github.com/patrickmn/go-cache v2.1.0+incompatible
1010
github.com/prometheus/client_golang v1.23.2
11-
github.com/webdevops/go-common v0.0.0-20251205143010-9409efa47a03
12-
go.uber.org/zap v1.27.1
11+
github.com/webdevops/go-common v0.0.0-20251219160827-5d6c8ef5b897
12+
resty.dev/v3 v3.0.0-beta.5
1313
)
1414

1515
require (
1616
github.com/KimMachineGun/automemlimit v0.7.5 // indirect
1717
github.com/beorn7/perks v1.0.1 // indirect
1818
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1919
github.com/dustin/go-humanize v1.0.1 // indirect
20+
github.com/kr/text v0.2.0 // indirect
2021
github.com/lmittmann/tint v1.1.2 // indirect
2122
github.com/mattn/go-isatty v0.0.20 // indirect
2223
github.com/miekg/dns v1.1.69 // indirect
@@ -26,7 +27,6 @@ require (
2627
github.com/prometheus/common v0.67.4 // indirect
2728
github.com/prometheus/procfs v0.19.2 // indirect
2829
go.uber.org/automaxprocs v1.6.0 // indirect
29-
go.uber.org/multierr v1.11.0 // indirect
3030
go.yaml.in/yaml/v2 v2.4.3 // indirect
3131
golang.org/x/mod v0.31.0 // indirect
3232
golang.org/x/net v0.48.0 // indirect

go.sum

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
44
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
55
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
66
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
7+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
78
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
89
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
910
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
@@ -54,17 +55,13 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
5455
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
5556
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
5657
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
57-
github.com/webdevops/go-common v0.0.0-20251205143010-9409efa47a03 h1:1HP2iGrDLo41gL/BfQBFFVb1FJ457ZDMYgr224SDzMI=
58-
github.com/webdevops/go-common v0.0.0-20251205143010-9409efa47a03/go.mod h1:2RZgXC980Lwz2M00Ghm+8/fGY864X7xzXPzFR2RojHc=
58+
github.com/webdevops/go-common v0.0.0-20251219160827-5d6c8ef5b897 h1:t3NXCsS6MEbB0AARmi+GJvcuTuXDnYayLPfMILs6h0A=
59+
github.com/webdevops/go-common v0.0.0-20251219160827-5d6c8ef5b897/go.mod h1:2RZgXC980Lwz2M00Ghm+8/fGY864X7xzXPzFR2RojHc=
5960
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
6061
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
6162
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
6263
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
6364
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
64-
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
65-
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
66-
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
67-
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
6865
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
6966
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
7067
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -156,3 +153,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
156153
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
157154
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
158155
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
156+
resty.dev/v3 v3.0.0-beta.5 h1:NV1xbqOLzSq7XMTs1t/HLPvu7xrxoXzF90SR4OO6faQ=
157+
resty.dev/v3 v3.0.0-beta.5/go.mod h1:NTOerrC/4T7/FE6tXIZGIysXXBdgNqwMZuKtxpea9NM=

shellyplug/resty.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package shellyplug
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"log/slog"
87
"time"
98

10-
resty "github.com/go-resty/resty/v2"
119
"github.com/patrickmn/go-cache"
10+
"github.com/webdevops/go-common/log/slogger"
11+
resty "resty.dev/v3"
1212

1313
"github.com/webdevops/shelly-plug-exporter/discovery"
1414
)
@@ -38,7 +38,7 @@ func (sp *ShellyPlug) restyClient(ctx context.Context, target discovery.Discover
3838
}
3939
}
4040

41-
restyLogger := sp.logger.With(
41+
restyLogger := sp.logger.WithGroup("request").With(
4242
slog.String("target", target.Address),
4343
slog.String("type", target.Type),
4444
)
@@ -53,36 +53,41 @@ func (sp *ShellyPlug) restyClient(ctx context.Context, target discovery.Discover
5353
if sp.resty.userAgent != "" {
5454
client.SetHeader("User-Agent", sp.resty.userAgent)
5555
}
56-
// client.SetRetryCount(2).
57-
// SetRetryWaitTime(1 * time.Second).
58-
// SetRetryMaxWaitTime(5 * time.Second)
56+
57+
client.SetRetryCount(2).
58+
SetRetryWaitTime(1 * time.Second).
59+
SetRetryMaxWaitTime(5 * time.Second)
5960

6061
if sp.auth.username != "" {
6162
client.SetDisableWarn(true)
6263
client.SetBasicAuth(sp.auth.username, sp.auth.password)
6364
client.SetDigestAuth(sp.auth.username, sp.auth.password)
6465
}
6566

66-
// client.AddRequestMiddleware(func(c *resty.Client, req *resty.Request) error {
67-
// c.Logger().Debugf(`send request: %s %s`, req.Method, req.URL)
68-
// return nil
69-
// })
70-
//
71-
// client.AddResponseMiddleware(func(c *resty.Client, res *resty.Response) error {
72-
// c.Logger().Debugf(`got response: %s %s with status %v`, res.Request.Method, res.Request.RawRequest.URL.String(), res.StatusCode())
73-
// return nil
74-
// })
75-
76-
client.OnAfterResponse(func(c *resty.Client, res *resty.Response) error {
67+
client.AddRequestMiddleware(func(c *resty.Client, req *resty.Request) error {
68+
c.Logger().(*slogger.Logger).With(
69+
slog.String("method", req.Method),
70+
slog.String("url", req.URL),
71+
).Debugf(`send request`)
72+
return nil
73+
})
74+
75+
client.AddResponseMiddleware(func(c *resty.Client, res *resty.Response) error {
76+
logger := c.Logger().(*slogger.Logger).With(
77+
slog.String("method", res.Request.Method),
78+
slog.String("url", res.Request.RawRequest.URL.String()),
79+
slog.Int("status", res.StatusCode()),
80+
)
81+
7782
switch res.StatusCode() {
78-
case 401:
79-
return errors.New(`shelly plug requires authentication and/or credentials are invalid`)
8083
case 200:
81-
// all ok, proceed
82-
return nil
84+
logger.Debugf(`request successfull`)
8385
default:
84-
return fmt.Errorf(`expected http status 200, got %v`, res.StatusCode())
86+
logger.Debugf(`request failed`)
87+
return fmt.Errorf(`request failed with status code %d`, res.StatusCode())
8588
}
89+
90+
return nil
8691
})
8792

8893
restyCache.SetDefault(cacheKey, client)

shellyplug/results.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (sp *ShellyPlug) targetGetShellyInfo(target discovery.DiscoveryTarget) (Res
2424

2525
client := sp.restyClient(sp.ctx, target)
2626

27-
r := client.R().ForceContentType("application/json").SetResult(&result)
27+
r := client.R().SetForceResponseContentType("application/json").SetResult(&result)
2828
_, err := r.Get("/shelly")
2929
return result, err
3030
}

shellyprober/gen1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package shellyprober
33
import (
44
"context"
55

6-
resty "github.com/go-resty/resty/v2"
76
"github.com/patrickmn/go-cache"
7+
resty "resty.dev/v3"
88

99
"github.com/webdevops/shelly-plug-exporter/discovery"
1010
)
@@ -100,7 +100,7 @@ type (
100100
)
101101

102102
func (sp *ShellyProberGen1) fetch(url string, response interface{}) error {
103-
r := sp.Client.R().ForceContentType("application/json").SetResult(&response)
103+
r := sp.Client.R().SetForceResponseContentType("application/json").SetResult(&response)
104104
_, err := r.Get(url)
105105
return err
106106
}

shellyprober/gen2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"encoding/json"
66
"fmt"
77

8-
resty "github.com/go-resty/resty/v2"
98
"github.com/patrickmn/go-cache"
9+
resty "resty.dev/v3"
1010

1111
"github.com/webdevops/shelly-plug-exporter/discovery"
1212
)
@@ -114,13 +114,13 @@ type (
114114
)
115115

116116
func (sp *ShellyProberGen2) fetch(url string, response interface{}) error {
117-
r := sp.Client.R().ForceContentType("application/json").SetResult(&response)
117+
r := sp.Client.R().SetForceResponseContentType("application/json").SetResult(&response)
118118
_, err := r.Get(url)
119119
return err
120120
}
121121

122122
func (sp *ShellyProberGen2) fetchWithCache(url string, response interface{}) error {
123-
cacheKey := fmt.Sprintf("%s/%s", sp.Client.BaseURL, url)
123+
cacheKey := fmt.Sprintf("%s/%s", sp.Client.BaseURL(), url)
124124

125125
if val, ok := sp.Cache.Get(cacheKey); ok {
126126
if data, err := json.Marshal(val); err == nil {

0 commit comments

Comments
 (0)