10 const apiurl = "https://mailmux.net/v1/aliases"
12 const jsonContentType = "application/json"
15 conn net.Conn // or io.ReadWriteCloser?
18 func Dial(network, address string) (*Client, error) {
19 c, err := net.Dial(network, address)
23 return &Client{conn: c}, nil
26 func (c *Client) exchange(tmsg *Mcall) (*Mcall, error) {
27 if err := c.tx(tmsg); err != nil {
28 return nil, fmt.Errorf("transmit tmsg: %w", err)
32 return nil, fmt.Errorf("receive rmsg: %w", err)
34 // TODO sanity checks here.
35 // did we get back the message type we expected?
39 func (c *Client) tx(tmsg *Mcall) error {
40 return json.NewEncoder(c.conn).Encode(tmsg)
43 func (c *Client) rx() (*Mcall, error) {
44 return ParseMcall(c.conn)
47 func (c *Client) Auth(username, password string) error {
53 rmsg, err := c.exchange(tmsg)
57 if rmsg.Type == Rerror {
58 return errors.New(rmsg.Error)
63 func (c *Client) NewAlias() ([]Alias, error) {
67 rmsg, err := c.exchange(tmsg)
69 return nil, fmt.Errorf("exchange tmsg: %w", err)
71 if rmsg.Type == Rerror {
72 return nil, errors.New(rmsg.Error)
74 return rmsg.Aliases, nil
77 func (c *Client) Aliases() ([]Alias, error) {
81 rmsg, err := c.exchange(tmsg)
83 return nil, fmt.Errorf("exchange tmsg: %w", err)
85 if rmsg.Type == Rerror {
86 return nil, errors.New(rmsg.Error)
88 return rmsg.Aliases, nil
91 func (c *Client) Close() error {