Add doc comments to api package

This commit is contained in:
2021-10-22 21:01:18 -07:00
parent 80a5867d6b
commit d7057e3f9c
4 changed files with 37 additions and 0 deletions

View File

@@ -11,8 +11,10 @@ import (
"go.arsenm.dev/itd/internal/types"
)
// Default socket address
const DefaultAddr = "/tmp/itd/socket"
// Client is the socket API client
type Client struct {
conn net.Conn
respCh chan types.Response
@@ -23,6 +25,7 @@ type Client struct {
dfuProgressCh chan DFUProgress
}
// New creates a new client and sets it up
func New(addr string) (*Client, error) {
conn, err := net.Dial("unix", addr)
if err != nil {
@@ -48,6 +51,16 @@ func New(addr string) (*Client, error) {
return out, err
}
func (c *Client) Close() error {
err := c.conn.Close()
if err != nil {
return err
}
close(c.respCh)
return nil
}
// request sends a request to itd and waits for and returns the response
func (c *Client) request(req types.Request) (types.Response, error) {
// Encode request into connection
err := json.NewEncoder(c.conn).Encode(req)
@@ -64,6 +77,7 @@ func (c *Client) request(req types.Request) (types.Response, error) {
return res, nil
}
// requestNoRes sends a request to itd and does not wait for the response
func (c *Client) requestNoRes(req types.Request) error {
// Encode request into connection
err := json.NewEncoder(c.conn).Encode(req)
@@ -73,6 +87,7 @@ func (c *Client) requestNoRes(req types.Request) error {
return nil
}
// handleResp handles the received response as needed
func (c *Client) handleResp(res types.Response) error {
switch res.Type {
case types.ResTypeWatchHeartRate: