1
1
package contact
2
2
3
3
import (
4
- "errors"
5
- "fmt"
6
4
"net/http"
7
- "net/url"
8
- "strconv"
9
5
"time"
10
6
11
- messagebird "github.com/messagebird/go-rest-api/v8 "
7
+ messagebird "github.com/messagebird/go-rest-api/v9 "
12
8
)
13
9
10
+ // path represents the path to the Contacts resource.
11
+ const path = "contacts"
12
+
14
13
// Contact gets returned by the API.
15
14
type Contact struct {
16
15
ID string
@@ -36,20 +35,15 @@ type Contact struct {
36
35
UpdatedDatetime * time.Time
37
36
}
38
37
39
- type ContactList struct {
38
+ type Contacts struct {
40
39
Limit , Offset int
41
40
Count , TotalCount int
42
41
Items []Contact
43
42
}
44
43
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
44
+ // CreateRequest represents a contact for write operations, e.g. for creating a new
51
45
// contact or updating an existing one.
52
- type Request struct {
46
+ type CreateRequest struct {
53
47
MSISDN string `json:"msisdn,omitempty"`
54
48
FirstName string `json:"firstName,omitempty"`
55
49
LastName string `json:"lastName,omitempty"`
@@ -59,20 +53,12 @@ type Request struct {
59
53
Custom4 string `json:"custom4,omitempty"`
60
54
}
61
55
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 ,
56
+ type ViewRequest struct {
57
+ MSISDN string `json:"msisdn,omitempty"`
58
+ Name string `json:"firstName,omitempty"`
69
59
}
70
60
71
- func Create (c * messagebird.Client , contactRequest * Request ) (* Contact , error ) {
72
- if err := validateCreate (contactRequest ); err != nil {
73
- return nil , err
74
- }
75
-
61
+ func Create (c messagebird.Client , contactRequest * CreateRequest ) (* Contact , error ) {
76
62
contact := & Contact {}
77
63
if err := c .Request (contact , http .MethodPost , path , contactRequest ); err != nil {
78
64
return nil , err
@@ -81,61 +67,27 @@ func Create(c *messagebird.Client, contactRequest *Request) (*Contact, error) {
81
67
return contact , nil
82
68
}
83
69
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
70
// Delete attempts deleting the contact with the provided ID. If nil is returned,
93
71
// 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
-
72
+ func Delete (c messagebird.Client , id string ) error {
99
73
return c .Request (nil , http .MethodDelete , path + "/" + id , nil )
100
74
}
101
75
102
76
// List retrieves a paginated list of contacts, based on the options provided.
103
77
// 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 {
78
+ func List (c messagebird.Client , options * messagebird.PaginationRequest ) (* Contacts , error ) {
79
+ contactList := & Contacts {}
80
+ if err := c .Request (contactList , http .MethodGet , path + "?" + options .QueryParams (), nil ); err != nil {
112
81
return nil , err
113
82
}
114
83
115
84
return contactList , nil
116
85
}
117
86
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
87
// Read retrieves the information of an existing contact.
136
- func Read (c * messagebird.Client , id string ) (* Contact , error ) {
88
+ func Read (c messagebird.Client , id string , req * ViewRequest ) (* Contact , error ) {
137
89
contact := & Contact {}
138
- if err := c .Request (contact , http .MethodGet , path + "/" + id , nil ); err != nil {
90
+ if err := c .Request (contact , http .MethodGet , path + "/" + id , req ); err != nil {
139
91
return nil , err
140
92
}
141
93
@@ -144,7 +96,7 @@ func Read(c *messagebird.Client, id string) (*Contact, error) {
144
96
145
97
// Update updates the record referenced by id with any values set in contactRequest.
146
98
// Do not set any values that should not be updated.
147
- func Update (c * messagebird.Client , id string , contactRequest * Request ) (* Contact , error ) {
99
+ func Update (c messagebird.Client , id string , contactRequest * CreateRequest ) (* Contact , error ) {
148
100
contact := & Contact {}
149
101
if err := c .Request (contact , http .MethodPatch , path + "/" + id , contactRequest ); err != nil {
150
102
return nil , err
0 commit comments