@@ -2,13 +2,13 @@ package shellyplug
22
33import (
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 )
0 commit comments