Skip to content

Commit a00e972

Browse files
committed
Add contact endpoint support
1 parent 3fc804a commit a00e972

9 files changed

+522
-0
lines changed

contact/contact.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
78+
if err := c.Request(contact, http.MethodPost, path, contactRequest); err != nil {
79+
return nil, err
80+
}
81+
82+
return contact, nil
83+
}
84+
85+
func validateCreate(contactRequest *Request) error {
86+
if contactRequest.MSISDN == "" {
87+
return errors.New("msisdn is required")
88+
}
89+
90+
return nil
91+
}
92+
93+
// Delete attempts deleting the contact with the provided ID. If nil is returned,
94+
// the resource was deleted successfully.
95+
func Delete(c *messagebird.Client, id string) error {
96+
if id == "" {
97+
return errors.New("id is required")
98+
}
99+
100+
return c.Request(nil, http.MethodDelete, path+"/"+id, nil)
101+
}
102+
103+
// List retrieves a paginated list of contacts, based on the options provided.
104+
// It's worth noting DefaultListOptions.
105+
func List(c *messagebird.Client, options *ListOptions) (*ContactList, error) {
106+
query, err := listQuery(options)
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
contactList := &ContactList{}
112+
113+
err = c.Request(contactList, http.MethodGet, path+"?"+query, nil)
114+
115+
return contactList, err
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+
139+
if err := c.Request(contact, http.MethodGet, path+"/"+id, nil); err != nil {
140+
return nil, err
141+
}
142+
143+
return contact, nil
144+
}
145+
146+
// Update updates the record referenced by id with any values set in contactRequest.
147+
// Do not set any values that should not be updated.
148+
func Update(c *messagebird.Client, id string, contactRequest *Request) (*Contact, error) {
149+
contact := &Contact{}
150+
151+
if err := c.Request(contact, http.MethodPatch, path+"/"+id, contactRequest); err != nil {
152+
return nil, err
153+
}
154+
155+
return contact, nil
156+
}

0 commit comments

Comments
 (0)