itd/api/api.go

47 lines
752 B
Go
Raw Normal View History

2022-04-23 00:12:30 +00:00
package api
import (
"io"
"net"
2022-04-23 00:12:30 +00:00
2023-01-03 06:30:17 +00:00
"go.arsenm.dev/itd/internal/rpc"
"storj.io/drpc/drpcconn"
2022-04-23 00:12:30 +00:00
)
const DefaultAddr = "/tmp/itd/socket"
type Client struct {
2023-01-03 06:30:17 +00:00
conn *drpcconn.Conn
client rpc.DRPCITDClient
2022-04-23 00:12:30 +00:00
}
func New(sockPath string) (*Client, error) {
conn, err := net.Dial("unix", sockPath)
2022-04-23 00:12:30 +00:00
if err != nil {
return nil, err
}
2023-01-03 06:30:17 +00:00
dconn := drpcconn.New(conn)
2022-04-23 00:12:30 +00:00
2023-01-03 06:30:17 +00:00
return &Client{
conn: dconn,
client: rpc.NewDRPCITDClient(dconn),
}, nil
2022-04-23 00:12:30 +00:00
}
func NewFromConn(conn io.ReadWriteCloser) *Client {
2023-01-03 06:30:17 +00:00
dconn := drpcconn.New(conn)
return &Client{
2023-01-03 06:30:17 +00:00
conn: dconn,
client: rpc.NewDRPCITDClient(dconn),
}
}
2023-01-03 06:30:17 +00:00
func (c *Client) FS() *FSClient {
return &FSClient{rpc.NewDRPCFSClient(c.conn)}
}
2022-04-23 00:12:30 +00:00
func (c *Client) Close() error {
2023-01-03 06:30:17 +00:00
return c.conn.Close()
2022-04-23 00:12:30 +00:00
}