package client import ( "encoding/json" "fmt" "io" "net/http" "net/url" "time" ) // Client for open hardware monitor type Client struct { Timeout time.Duration URL url.URL } // Fetch requests func (c *Client) Fetch() (*Node, error) { client := http.Client{Timeout: c.Timeout} resp, err := client.Get(c.URL.String()) if err != nil { return nil, err } defer resp.Body.Close() return c.Decode(resp.Body) } // Decode json func (c *Client) Decode(r io.Reader) (*Node, error) { node := &Node{} decoder := json.NewDecoder(r) if err := decoder.Decode(node); err != nil { return nil, err } return node, nil } // Node from data type Node struct { ID int `json:"id"` ImageURL string Max string Min string Text string Value string Children []Node } func (n *Node) String() string { return n.stringify(0) } func (n *Node) stringify(indent int) string { prefix := "" for i := 0; i < indent; i++ { prefix += " " } ret := prefix + n.Text if n.Value != "" { ret += ": " + n.Value } if n.Max != "" && n.Min != "" && n.Max != "-" && n.Min != "-" { ret += fmt.Sprintf(" (%s - %s)", n.Min, n.Max) } ret += "\n" for _, child := range n.Children { ret += child.stringify(indent + 1) } return ret }