github.com/CyCoreSystems/ari@v4.8.4+incompatible/client/native/mailbox.go (about)

     1  package native
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  
     7  	"github.com/CyCoreSystems/ari"
     8  )
     9  
    10  // Mailbox provides the ARI Mailbox accessors for the native client
    11  type Mailbox struct {
    12  	client *Client
    13  }
    14  
    15  // Get gets a lazy handle for the mailbox name
    16  func (m *Mailbox) Get(key *ari.Key) *ari.MailboxHandle {
    17  	return ari.NewMailboxHandle(m.client.stamp(key), m)
    18  }
    19  
    20  // List lists the mailboxes and returns a list of handles
    21  func (m *Mailbox) List(filter *ari.Key) (mx []*ari.Key, err error) {
    22  
    23  	mailboxes := []struct {
    24  		Name string `json:"name"`
    25  	}{}
    26  
    27  	if filter == nil {
    28  		filter = ari.NodeKey(m.client.node, m.client.ApplicationName())
    29  	}
    30  
    31  	err = m.client.get("/mailboxes", &mailboxes)
    32  	for _, i := range mailboxes {
    33  		k := m.client.stamp(ari.NewKey(ari.MailboxKey, i.Name))
    34  		if filter.Match(k) {
    35  			mx = append(mx, k)
    36  		}
    37  	}
    38  
    39  	return
    40  }
    41  
    42  // Data retrieves the state of the given mailbox
    43  func (m *Mailbox) Data(key *ari.Key) (*ari.MailboxData, error) {
    44  	if key == nil || key.ID == "" {
    45  		return nil, errors.New("mailbox key not supplied")
    46  	}
    47  
    48  	var data = new(ari.MailboxData)
    49  	if err := m.client.get("/mailboxes/"+key.ID, data); err != nil {
    50  		return nil, dataGetError(err, "mailbox", "%v", key.ID)
    51  	}
    52  
    53  	data.Key = m.client.stamp(key)
    54  	return data, nil
    55  }
    56  
    57  // Update updates the new and old message counts of the mailbox
    58  func (m *Mailbox) Update(key *ari.Key, oldMessages int, newMessages int) error {
    59  	req := map[string]string{
    60  		"oldMessages": strconv.Itoa(oldMessages),
    61  		"newMessages": strconv.Itoa(newMessages),
    62  	}
    63  	return m.client.put("/mailboxes/"+key.ID, nil, &req)
    64  }
    65  
    66  // Delete deletes the mailbox
    67  func (m *Mailbox) Delete(key *ari.Key) error {
    68  	return m.client.del("/mailboxes/"+key.ID, nil, "")
    69  }