Skip to content

Commit 9fa6d81

Browse files
committed
Explicitly set the request method instead of inferring it
This paves the way for support for the Voice API which uses DELETE and PUT.
1 parent 84c39fa commit 9fa6d81

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

client.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,18 @@ func New(AccessKey string) *Client {
6565
return &Client{AccessKey: AccessKey, HTTPClient: &http.Client{}}
6666
}
6767

68-
func (c *Client) request(v interface{}, path string, data interface{}) error {
68+
func (c *Client) request(v interface{}, method, path string, data interface{}) error {
6969
uri, err := url.Parse(Endpoint + "/" + path)
7070
if err != nil {
7171
return err
7272
}
7373

74-
method := "GET"
7574
var jsonEncoded []byte
7675
if data != nil {
7776
jsonEncoded, err = json.Marshal(data)
7877
if err != nil {
7978
return err
8079
}
81-
method = "POST"
8280
}
8381

8482
request, err := http.NewRequest(method, uri.String(), bytes.NewBuffer(jsonEncoded))
@@ -139,7 +137,7 @@ func (c *Client) request(v interface{}, path string, data interface{}) error {
139137
// with the access key.
140138
func (c *Client) Balance() (*Balance, error) {
141139
balance := &Balance{}
142-
if err := c.request(balance, "balance", nil); err != nil {
140+
if err := c.request(balance, http.MethodGet, "balance", nil); err != nil {
143141
if err == ErrResponse {
144142
return balance, err
145143
}
@@ -154,7 +152,7 @@ func (c *Client) Balance() (*Balance, error) {
154152
// created by the NewHLR function.
155153
func (c *Client) HLR(id string) (*HLR, error) {
156154
hlr := &HLR{}
157-
if err := c.request(hlr, HLRPath+"/"+id, nil); err != nil {
155+
if err := c.request(hlr, http.MethodGet, HLRPath+"/"+id, nil); err != nil {
158156
if err == ErrResponse {
159157
return hlr, err
160158
}
@@ -169,7 +167,7 @@ func (c *Client) HLR(id string) (*HLR, error) {
169167
// function.
170168
func (c *Client) HLRs() (*HLRList, error) {
171169
hlrList := &HLRList{}
172-
if err := c.request(hlrList, HLRPath, nil); err != nil {
170+
if err := c.request(hlrList, http.MethodGet, HLRPath, nil); err != nil {
173171
if err == ErrResponse {
174172
return hlrList, err
175173
}
@@ -189,7 +187,7 @@ func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
189187

190188
hlr := &HLR{}
191189

192-
if err := c.request(hlr, HLRPath, requestData); err != nil {
190+
if err := c.request(hlr, http.MethodPost, HLRPath, requestData); err != nil {
193191
if err == ErrResponse {
194192
return hlr, err
195193
}
@@ -203,7 +201,7 @@ func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
203201
// Message retrieves the information of an existing Message.
204202
func (c *Client) Message(id string) (*Message, error) {
205203
message := &Message{}
206-
if err := c.request(message, MessagePath+"/"+id, nil); err != nil {
204+
if err := c.request(message, http.MethodGet, MessagePath+"/"+id, nil); err != nil {
207205
if err == ErrResponse {
208206
return message, err
209207
}
@@ -222,7 +220,7 @@ func (c *Client) Messages(msgListParams *MessageListParams) (*MessageList, error
222220
return messageList, err
223221
}
224222

225-
if err := c.request(messageList, MessagePath+"?"+params.Encode(), nil); err != nil {
223+
if err := c.request(messageList, http.MethodGet, MessagePath+"?"+params.Encode(), nil); err != nil {
226224
if err == ErrResponse {
227225
return messageList, err
228226
}
@@ -241,7 +239,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
241239
}
242240

243241
message := &Message{}
244-
if err := c.request(message, MessagePath, requestData); err != nil {
242+
if err := c.request(message, http.MethodPost, MessagePath, requestData); err != nil {
245243
if err == ErrResponse {
246244
return message, err
247245
}
@@ -255,7 +253,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
255253
// MMSMessage retrieves the information of an existing MmsMessage.
256254
func (c *Client) MMSMessage(id string) (*MMSMessage, error) {
257255
mmsMessage := &MMSMessage{}
258-
if err := c.request(mmsMessage, MMSPath+"/"+id, nil); err != nil {
256+
if err := c.request(mmsMessage, http.MethodGet, MMSPath+"/"+id, nil); err != nil {
259257
if err == ErrResponse {
260258
return mmsMessage, err
261259
}
@@ -277,7 +275,7 @@ func (c *Client) NewMMSMessage(originator string, recipients []string, msgParams
277275
params.Set("recipients", strings.Join(recipients, ","))
278276

279277
mmsMessage := &MMSMessage{}
280-
if err := c.request(mmsMessage, MMSPath, params); err != nil {
278+
if err := c.request(mmsMessage, http.MethodPost, MMSPath, params); err != nil {
281279
if err == ErrResponse {
282280
return mmsMessage, err
283281
}
@@ -291,7 +289,7 @@ func (c *Client) NewMMSMessage(originator string, recipients []string, msgParams
291289
// VoiceMessage retrieves the information of an existing VoiceMessage.
292290
func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
293291
message := &VoiceMessage{}
294-
if err := c.request(message, VoiceMessagePath+"/"+id, nil); err != nil {
292+
if err := c.request(message, http.MethodGet, VoiceMessagePath+"/"+id, nil); err != nil {
295293
if err == ErrResponse {
296294
return message, err
297295
}
@@ -305,7 +303,7 @@ func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
305303
// VoiceMessages retrieves all VoiceMessages of the user.
306304
func (c *Client) VoiceMessages() (*VoiceMessageList, error) {
307305
messageList := &VoiceMessageList{}
308-
if err := c.request(messageList, VoiceMessagePath, nil); err != nil {
306+
if err := c.request(messageList, http.MethodGet, VoiceMessagePath, nil); err != nil {
309307
if err == ErrResponse {
310308
return messageList, err
311309
}
@@ -324,7 +322,7 @@ func (c *Client) NewVoiceMessage(recipients []string, body string, params *Voice
324322
}
325323

326324
message := &VoiceMessage{}
327-
if err := c.request(message, VoiceMessagePath, requestData); err != nil {
325+
if err := c.request(message, http.MethodPost, VoiceMessagePath, requestData); err != nil {
328326
if err == ErrResponse {
329327
return message, err
330328
}
@@ -343,7 +341,7 @@ func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, err
343341
}
344342

345343
verify := &Verify{}
346-
if err := c.request(verify, VerifyPath, requestData); err != nil {
344+
if err := c.request(verify, http.MethodPost, VerifyPath, requestData); err != nil {
347345
if err == ErrResponse {
348346
return verify, err
349347
}
@@ -362,7 +360,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
362360
path := VerifyPath + "/" + id + "?" + params.Encode()
363361

364362
verify := &Verify{}
365-
if err := c.request(verify, path, nil); err != nil {
363+
if err := c.request(verify, http.MethodGet, path, nil); err != nil {
366364
if err == ErrResponse {
367365
return verify, err
368366
}
@@ -379,7 +377,7 @@ func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, erro
379377
path := LookupPath + "/" + phoneNumber + "?" + urlParams.Encode()
380378

381379
lookup := &Lookup{}
382-
if err := c.request(lookup, path, nil); err != nil {
380+
if err := c.request(lookup, http.MethodPost, path, nil); err != nil {
383381
if err == ErrResponse {
384382
return lookup, err
385383
}
@@ -396,7 +394,7 @@ func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, e
396394
path := LookupPath + "/" + phoneNumber + "/" + HLRPath
397395

398396
hlr := &HLR{}
399-
if err := c.request(hlr, path, requestData); err != nil {
397+
if err := c.request(hlr, http.MethodPost, path, requestData); err != nil {
400398
if err == ErrResponse {
401399
return hlr, err
402400
}
@@ -413,7 +411,7 @@ func (c *Client) LookupHLR(phoneNumber string, params *LookupParams) (*HLR, erro
413411
path := LookupPath + "/" + phoneNumber + "/" + HLRPath + "?" + urlParams.Encode()
414412

415413
hlr := &HLR{}
416-
if err := c.request(hlr, path, nil); err != nil {
414+
if err := c.request(hlr, http.MethodGet, path, nil); err != nil {
417415
if err == ErrResponse {
418416
return hlr, err
419417
}

0 commit comments

Comments
 (0)