myStrom: add token support (#27047)
This commit is contained in:
parent
27e31808e3
commit
f8d09800a5
5 changed files with 28 additions and 9 deletions
|
|
@ -16,12 +16,13 @@ func init() {
|
|||
// NewMyStromFromConfig creates a myStrom meter from generic config
|
||||
func NewMyStromFromConfig(other map[string]any) (api.Meter, error) {
|
||||
var cc struct {
|
||||
URI string
|
||||
URI string
|
||||
Token string
|
||||
}
|
||||
|
||||
if err := util.DecodeOther(other, &cc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mystrom.NewConnection(cc.URI), nil
|
||||
return mystrom.NewConnection(cc.URI, cc.Token), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,29 +17,36 @@ type Report struct {
|
|||
|
||||
type Connection struct {
|
||||
*request.Helper
|
||||
uri string
|
||||
uri string
|
||||
token string
|
||||
}
|
||||
|
||||
func NewConnection(uri string) *Connection {
|
||||
func NewConnection(uri, token string) *Connection {
|
||||
return &Connection{
|
||||
uri: util.DefaultScheme(strings.TrimSuffix(uri, "/"), "http"),
|
||||
token: token,
|
||||
Helper: request.NewHelper(util.NewLogger("mystrom")),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Connection) Request(path string) error {
|
||||
uri := fmt.Sprintf("%s/%s", c.uri, path)
|
||||
req, err := request.New(http.MethodGet, uri, nil, nil)
|
||||
if err == nil {
|
||||
_, err = c.DoBody(req)
|
||||
req, _ := request.New(http.MethodGet, uri, nil, nil)
|
||||
if c.token != "" {
|
||||
req.Header.Set("Token", c.token)
|
||||
}
|
||||
_, err := c.DoBody(req)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Connection) Report() (Report, error) {
|
||||
var res Report
|
||||
uri := fmt.Sprintf("%s/report", c.uri)
|
||||
err := c.GetJSON(uri, &res)
|
||||
req, _ := request.New(http.MethodGet, uri, nil, request.AcceptJSON)
|
||||
if c.token != "" {
|
||||
req.Header.Set("Token", c.token)
|
||||
}
|
||||
err := c.DoJSON(req, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue