|
| 1 | +package contact |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "strconv" |
| 9 | + "time" |
| 10 | + |
| 11 | + messagebird "github.com/messagebird/go-rest-api" |
| 12 | +) |
| 13 | + |
| 14 | +// Contact gets returned by the API. |
| 15 | +type Contact struct { |
| 16 | + ID string |
| 17 | + HRef string |
| 18 | + MSISDN int64 |
| 19 | + FirstName string |
| 20 | + LastName string |
| 21 | + CustomDetails struct { |
| 22 | + Custom1 string |
| 23 | + Custom2 string |
| 24 | + Custom3 string |
| 25 | + Custom4 string |
| 26 | + } |
| 27 | + Groups struct { |
| 28 | + TotalCount int |
| 29 | + HRef string |
| 30 | + } |
| 31 | + Messages struct { |
| 32 | + TotalCount int |
| 33 | + HRef string |
| 34 | + } |
| 35 | + CreatedDatetime time.Time |
| 36 | + UpdatedDatetime time.Time |
| 37 | +} |
| 38 | + |
| 39 | +type ContactList struct { |
| 40 | + Limit, Offset int |
| 41 | + Count, TotalCount int |
| 42 | + Items []Contact |
| 43 | +} |
| 44 | + |
| 45 | +// ListOptions can be used to set pagination options in List(). |
| 46 | +type ListOptions struct { |
| 47 | + Limit, Offset int |
| 48 | +} |
| 49 | + |
| 50 | +// Request represents a contact for write operations, e.g. for creating a new |
| 51 | +// contact or updating an existing one. |
| 52 | +type Request struct { |
| 53 | + MSISDN string `json:"msisdn,omitempty"` |
| 54 | + FirstName string `json:"firstName,omitempty"` |
| 55 | + LastName string `json:"lastName,omitempty"` |
| 56 | + Custom1 string `json:"custom1,omitempty"` |
| 57 | + Custom2 string `json:"custom2,omitempty"` |
| 58 | + Custom3 string `json:"custom3,omitempty"` |
| 59 | + Custom4 string `json:"custom4,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +// path represents the path to the Contacts resource. |
| 63 | +const path = "contacts" |
| 64 | + |
| 65 | +// DefaultListOptions provides reasonable values for List(). |
| 66 | +var DefaultListOptions = &ListOptions{ |
| 67 | + Limit: 20, |
| 68 | + Offset: 0, |
| 69 | +} |
| 70 | + |
| 71 | +func Create(c *messagebird.Client, contactRequest *Request) (*Contact, error) { |
| 72 | + if err := validateCreate(contactRequest); err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + contact := &Contact{} |
| 77 | + if err := c.Request(contact, http.MethodPost, path, contactRequest); err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + return contact, nil |
| 82 | +} |
| 83 | + |
| 84 | +func validateCreate(contactRequest *Request) error { |
| 85 | + if contactRequest.MSISDN == "" { |
| 86 | + return errors.New("msisdn is required") |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// Delete attempts deleting the contact with the provided ID. If nil is returned, |
| 93 | +// the resource was deleted successfully. |
| 94 | +func Delete(c *messagebird.Client, id string) error { |
| 95 | + if id == "" { |
| 96 | + return errors.New("id is required") |
| 97 | + } |
| 98 | + |
| 99 | + return c.Request(nil, http.MethodDelete, path+"/"+id, nil) |
| 100 | +} |
| 101 | + |
| 102 | +// List retrieves a paginated list of contacts, based on the options provided. |
| 103 | +// It's worth noting DefaultListOptions. |
| 104 | +func List(c *messagebird.Client, options *ListOptions) (*ContactList, error) { |
| 105 | + query, err := listQuery(options) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + contactList := &ContactList{} |
| 111 | + if err = c.Request(contactList, http.MethodGet, path+"?"+query, nil); err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + return contactList, nil |
| 116 | +} |
| 117 | + |
| 118 | +func listQuery(options *ListOptions) (string, error) { |
| 119 | + if options.Limit < 10 { |
| 120 | + return "", fmt.Errorf("minimum limit is 10, got %d", options.Limit) |
| 121 | + } |
| 122 | + |
| 123 | + if options.Offset < 0 { |
| 124 | + return "", fmt.Errorf("offset can not be negative") |
| 125 | + } |
| 126 | + |
| 127 | + values := &url.Values{} |
| 128 | + |
| 129 | + values.Set("limit", strconv.Itoa(options.Limit)) |
| 130 | + values.Set("offset", strconv.Itoa(options.Offset)) |
| 131 | + |
| 132 | + return values.Encode(), nil |
| 133 | +} |
| 134 | + |
| 135 | +// Read retrieves the information of an existing contact. |
| 136 | +func Read(c *messagebird.Client, id string) (*Contact, error) { |
| 137 | + contact := &Contact{} |
| 138 | + if err := c.Request(contact, http.MethodGet, path+"/"+id, nil); err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + return contact, nil |
| 143 | +} |
| 144 | + |
| 145 | +// Update updates the record referenced by id with any values set in contactRequest. |
| 146 | +// Do not set any values that should not be updated. |
| 147 | +func Update(c *messagebird.Client, id string, contactRequest *Request) (*Contact, error) { |
| 148 | + contact := &Contact{} |
| 149 | + if err := c.Request(contact, http.MethodPatch, path+"/"+id, contactRequest); err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + return contact, nil |
| 154 | +} |
0 commit comments