|
| 1 | +package shellyplug |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + resty "github.com/go-resty/resty/v2" |
| 10 | + "github.com/patrickmn/go-cache" |
| 11 | + "go.uber.org/zap" |
| 12 | + |
| 13 | + "github.com/webdevops/shelly-plug-exporter/discovery" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + restyCache *cache.Cache |
| 18 | +) |
| 19 | + |
| 20 | +func (sp *ShellyPlug) SetUserAgent(val string) { |
| 21 | + sp.resty.userAgent = val |
| 22 | +} |
| 23 | + |
| 24 | +func (sp *ShellyPlug) SetTimeout(timeout time.Duration) { |
| 25 | + sp.resty.timeout = timeout |
| 26 | +} |
| 27 | + |
| 28 | +func (sp *ShellyPlug) SetHttpAuth(username, password string) { |
| 29 | + sp.auth.username = username |
| 30 | + sp.auth.password = password |
| 31 | +} |
| 32 | + |
| 33 | +func (sp *ShellyPlug) restyClient(ctx context.Context, target discovery.DiscoveryTarget) (client *resty.Client) { |
| 34 | + cacheKey := target.Address |
| 35 | + if val, ok := restyCache.Get(cacheKey); ok { |
| 36 | + if client, ok := val.(*resty.Client); ok { |
| 37 | + return client |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + restyLogger := sp.logger.With( |
| 42 | + zap.String("target", target.Address), |
| 43 | + zap.String("type", target.Type), |
| 44 | + ) |
| 45 | + |
| 46 | + client = resty.New() |
| 47 | + client.SetBaseURL(target.BaseUrl()) |
| 48 | + client.SetLogger(restyLogger) |
| 49 | + if sp.resty.timeout.Seconds() > 0 { |
| 50 | + client.SetTimeout(sp.resty.timeout) |
| 51 | + } |
| 52 | + |
| 53 | + if sp.resty.userAgent != "" { |
| 54 | + client.SetHeader("User-Agent", sp.resty.userAgent) |
| 55 | + } |
| 56 | + // client.SetRetryCount(2). |
| 57 | + // SetRetryWaitTime(1 * time.Second). |
| 58 | + // SetRetryMaxWaitTime(5 * time.Second) |
| 59 | + |
| 60 | + if sp.auth.username != "" { |
| 61 | + client.SetDisableWarn(true) |
| 62 | + client.SetBasicAuth(sp.auth.username, sp.auth.password) |
| 63 | + client.SetDigestAuth(sp.auth.username, sp.auth.password) |
| 64 | + } |
| 65 | + |
| 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 { |
| 77 | + switch res.StatusCode() { |
| 78 | + case 401: |
| 79 | + return errors.New(`shelly plug requires authentication and/or credentials are invalid`) |
| 80 | + case 200: |
| 81 | + // all ok, proceed |
| 82 | + return nil |
| 83 | + default: |
| 84 | + return fmt.Errorf(`expected http status 200, got %v`, res.StatusCode()) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + restyCache.SetDefault(cacheKey, client) |
| 89 | + |
| 90 | + return |
| 91 | +} |
0 commit comments