Skip to content

Commit 1add4a8

Browse files
Merge pull request #18 from messagebird/json-requests
Switch from url encoded data to JSON data
2 parents 7fd2300 + 3ff3c48 commit 1add4a8

File tree

11 files changed

+356
-147
lines changed

11 files changed

+356
-147
lines changed

client.go

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package messagebird
1111

1212
import (
13+
"bytes"
1314
"encoding/json"
1415
"errors"
1516
"io/ioutil"
@@ -64,42 +65,40 @@ func New(AccessKey string) *Client {
6465
return &Client{AccessKey: AccessKey, HTTPClient: &http.Client{}}
6566
}
6667

67-
func (c *Client) request(v interface{}, path string, params *url.Values) error {
68+
func (c *Client) request(v interface{}, path string, data interface{}) error {
6869
uri, err := url.Parse(Endpoint + "/" + path)
6970
if err != nil {
7071
return err
7172
}
7273

73-
var request *http.Request
74-
if params != nil {
75-
body := params.Encode()
76-
if request, err = http.NewRequest("POST", uri.String(), strings.NewReader(body)); err != nil {
74+
method := "GET"
75+
var jsonEncoded []byte
76+
if data != nil {
77+
jsonEncoded, err = json.Marshal(data)
78+
if err != nil {
7779
return err
7880
}
81+
method = "POST"
82+
}
7983

80-
if c.DebugLog != nil {
81-
if unescapedBody, queryError := url.QueryUnescape(body); queryError == nil {
82-
log.Printf("HTTP REQUEST: POST %s %s", uri.String(), unescapedBody)
83-
} else {
84-
log.Printf("HTTP REQUEST: POST %s %s", uri.String(), body)
85-
}
86-
}
84+
request, err := http.NewRequest(method, uri.String(), bytes.NewBuffer(jsonEncoded))
85+
if err != nil {
86+
return err
87+
}
8788

88-
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
89-
} else {
90-
if request, err = http.NewRequest("GET", uri.String(), nil); err != nil {
91-
return err
92-
}
89+
request.Header.Set("Content-Type", "application/json")
90+
request.Header.Set("Accept", "application/json")
91+
request.Header.Set("Authorization", "AccessKey "+c.AccessKey)
92+
request.Header.Set("User-Agent", "MessageBird/ApiClient/"+ClientVersion+" Go/"+runtime.Version())
9393

94-
if c.DebugLog != nil {
95-
log.Printf("HTTP REQUEST: GET %s", uri.String())
94+
if c.DebugLog != nil {
95+
if data != nil {
96+
log.Printf("HTTP REQUEST: %s %s %s", method, uri.String(), jsonEncoded)
97+
} else {
98+
log.Printf("HTTP REQUEST: %s %s", method, uri.String())
9699
}
97100
}
98101

99-
request.Header.Add("Accept", "application/json")
100-
request.Header.Add("Authorization", "AccessKey "+c.AccessKey)
101-
request.Header.Add("User-Agent", "MessageBird/ApiClient/"+ClientVersion+" Go/"+runtime.Version())
102-
103102
response, err := c.HTTPClient.Do(request)
104103
if err != nil {
105104
return err
@@ -182,14 +181,15 @@ func (c *Client) HLRs() (*HLRList, error) {
182181
}
183182

184183
// NewHLR retrieves the information of an existing HLR.
185-
func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
186-
params := &url.Values{
187-
"msisdn": {msisdn},
188-
"reference": {reference},
184+
func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
185+
requestData, err := requestDataForHLR(msisdn, reference)
186+
if err != nil {
187+
return nil, err
189188
}
190189

191190
hlr := &HLR{}
192-
if err := c.request(hlr, HLRPath, params); err != nil {
191+
192+
if err := c.request(hlr, HLRPath, requestData); err != nil {
193193
if err == ErrResponse {
194194
return hlr, err
195195
}
@@ -235,17 +235,13 @@ func (c *Client) Messages(msgListParams *MessageListParams) (*MessageList, error
235235

236236
// NewMessage creates a new message for one or more recipients.
237237
func (c *Client) NewMessage(originator string, recipients []string, body string, msgParams *MessageParams) (*Message, error) {
238-
params, err := paramsForMessage(msgParams)
238+
requestData, err := requestDataForMessage(originator, recipients, body, msgParams)
239239
if err != nil {
240240
return nil, err
241241
}
242242

243-
params.Set("originator", originator)
244-
params.Set("body", body)
245-
params.Set("recipients", strings.Join(recipients, ","))
246-
247243
message := &Message{}
248-
if err := c.request(message, MessagePath, params); err != nil {
244+
if err := c.request(message, MessagePath, requestData); err != nil {
249245
if err == ErrResponse {
250246
return message, err
251247
}
@@ -322,12 +318,13 @@ func (c *Client) VoiceMessages() (*VoiceMessageList, error) {
322318

323319
// NewVoiceMessage creates a new voice message for one or more recipients.
324320
func (c *Client) NewVoiceMessage(recipients []string, body string, params *VoiceMessageParams) (*VoiceMessage, error) {
325-
urlParams := paramsForVoiceMessage(params)
326-
urlParams.Set("body", body)
327-
urlParams.Set("recipients", strings.Join(recipients, ","))
321+
requestData, err := requestDataForVoiceMessage(recipients, body, params)
322+
if err != nil {
323+
return nil, err
324+
}
328325

329326
message := &VoiceMessage{}
330-
if err := c.request(message, VoiceMessagePath, urlParams); err != nil {
327+
if err := c.request(message, VoiceMessagePath, requestData); err != nil {
331328
if err == ErrResponse {
332329
return message, err
333330
}
@@ -340,11 +337,13 @@ func (c *Client) NewVoiceMessage(recipients []string, body string, params *Voice
340337

341338
// NewVerify generates a new One-Time-Password for one recipient.
342339
func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, error) {
343-
urlParams := paramsForVerify(params)
344-
urlParams.Set("recipient", recipient)
340+
requestData, err := requestDataForVerify(recipient, params)
341+
if err != nil {
342+
return nil, err
343+
}
345344

346345
verify := &Verify{}
347-
if err := c.request(verify, VerifyPath, urlParams); err != nil {
346+
if err := c.request(verify, VerifyPath, requestData); err != nil {
348347
if err == ErrResponse {
349348
return verify, err
350349
}
@@ -393,11 +392,11 @@ func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, erro
393392

394393
// NewLookupHLR creates a new HLR lookup for the specified number.
395394
func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
396-
urlParams := paramsForLookup(params)
395+
requestData := requestDataForLookup(params)
397396
path := LookupPath + "/" + phoneNumber + "/" + HLRPath
398397

399398
hlr := &HLR{}
400-
if err := c.request(hlr, path, urlParams); err != nil {
399+
if err := c.request(hlr, path, requestData); err != nil {
401400
if err == ErrResponse {
402401
return hlr, err
403402
}

hlr.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package messagebird
22

3-
import "time"
3+
import (
4+
"errors"
5+
"time"
6+
)
47

58
// HLR stands for Home Location Register.
69
// Contains information about the subscribers identity, telephone number, the associated services and general information about the location of the subscriber
@@ -26,3 +29,24 @@ type HLRList struct {
2629
Links map[string]*string
2730
Items []HLR
2831
}
32+
33+
type hlrRequest struct {
34+
MSISDN string `json:"msisdn"`
35+
Reference string `json:"reference"`
36+
}
37+
38+
func requestDataForHLR(msisdn string, reference string) (*hlrRequest, error) {
39+
if msisdn == "" {
40+
return nil, errors.New("msisdn is required")
41+
}
42+
if reference == "" {
43+
return nil, errors.New("reference is required")
44+
}
45+
46+
request := &hlrRequest{
47+
MSISDN: msisdn,
48+
Reference: reference,
49+
}
50+
51+
return request, nil
52+
}

hlr_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ func TestHLR(t *testing.T) {
9797
assertHLRObject(t, hlr)
9898
}
9999

100+
func TestRequestDataForHLR(t *testing.T) {
101+
requestData, err := requestDataForHLR("31612345678", "MyReference")
102+
if err != nil {
103+
t.Fatalf("Didn't expect an error while getting the request data for a HLR: %s", err)
104+
}
105+
106+
if requestData.MSISDN != "31612345678" {
107+
t.Errorf("Unexpected msisdn: %s, expected: 31612345678", requestData.MSISDN)
108+
}
109+
if requestData.Reference != "MyReference" {
110+
t.Errorf("Unexpected reference: %s, expected: MyReference", requestData.Reference)
111+
}
112+
}
113+
100114
func TestNewHLR(t *testing.T) {
101115
SetServerResponse(http.StatusOK, hlrObject)
102116

lookup.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ type LookupParams struct {
2727
Reference string
2828
}
2929

30+
type lookupRequest struct {
31+
CountryCode string `json:"countryCode,omitempty"`
32+
Reference string `json:"reference,omitempty"`
33+
}
34+
35+
func requestDataForLookup(params *LookupParams) *lookupRequest {
36+
request := &lookupRequest{}
37+
38+
if params == nil {
39+
return request
40+
}
41+
42+
request.CountryCode = params.CountryCode
43+
request.Reference = params.Reference
44+
45+
return request
46+
}
47+
3048
func paramsForLookup(params *LookupParams) *url.Values {
3149
urlParams := &url.Values{}
3250

lookup_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ func TestLookupHLR(t *testing.T) {
9090
checkHLR(t, hlr)
9191
}
9292

93+
func TestRequestDataForLookupHLR(t *testing.T) {
94+
lookupParams := &LookupParams{
95+
CountryCode: "NL",
96+
Reference: "MyReference",
97+
}
98+
request := requestDataForLookup(lookupParams)
99+
100+
if request.CountryCode != "NL" {
101+
t.Errorf("Unexpected country code: %s, expected: NL", request.CountryCode)
102+
}
103+
if request.Reference != "MyReference" {
104+
t.Errorf("Unexpected reference: %s, expected: MyReference", request.Reference)
105+
}
106+
}
107+
93108
func TestNewLookupHLR(t *testing.T) {
94109
SetServerResponse(201, lookupHLRObject)
95110

message.go

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,49 +61,55 @@ type MessageListParams struct {
6161
Offset int
6262
}
6363

64-
// paramsForMessage converts the specified MessageParams struct to a
65-
// url.Values pointer and returns it.
66-
func paramsForMessage(params *MessageParams) (*url.Values, error) {
67-
urlParams := &url.Values{}
68-
69-
if params == nil {
70-
return urlParams, nil
71-
}
64+
type messageRequest struct {
65+
Originator string `json:"originator"`
66+
Body string `json:"body"`
67+
Recipients []string `json:"recipients"`
68+
Type string `json:"type,omitempty"`
69+
Reference string `json:"reference,omitempty"`
70+
Validity int `json:"validity,omitempty"`
71+
Gateway int `json:"gateway,omitempty"`
72+
TypeDetails TypeDetails `json:"typeDetails,omitempty"`
73+
DataCoding string `json:"dataCoding,omitempty"`
74+
MClass int `json:"mclass,omitempty"`
75+
ScheduledDatetime string `json:"scheduledDatetime,omitempty"`
76+
}
7277

73-
if params.Type != "" {
74-
urlParams.Set("type", params.Type)
75-
if params.Type == "flash" {
76-
urlParams.Set("mclass", "0")
77-
}
78-
}
79-
if params.Reference != "" {
80-
urlParams.Set("reference", params.Reference)
78+
func requestDataForMessage(originator string, recipients []string, body string, params *MessageParams) (*messageRequest, error) {
79+
if originator == "" {
80+
return nil, errors.New("originator is required")
8181
}
82-
if params.Validity != 0 {
83-
urlParams.Set("validity", strconv.Itoa(params.Validity))
82+
if len(recipients) == 0 {
83+
return nil, errors.New("at least 1 recipient is required")
8484
}
85-
if params.Gateway != 0 {
86-
urlParams.Set("gateway", strconv.Itoa(params.Gateway))
85+
if body == "" {
86+
return nil, errors.New("body is required")
8787
}
8888

89-
for k, v := range params.TypeDetails {
90-
if vs, ok := v.(string); ok {
91-
urlParams.Set("typeDetails["+k+"]", vs)
92-
} else if vi, ok := v.(int); ok {
93-
urlParams.Set("typeDetails["+k+"]", strconv.Itoa(vi))
94-
} else {
95-
return nil, errors.New("Unknown type for typeDetails value")
96-
}
89+
request := &messageRequest{
90+
Originator: originator,
91+
Recipients: recipients,
92+
Body: body,
9793
}
9894

99-
if params.DataCoding != "" {
100-
urlParams.Set("datacoding", params.DataCoding)
101-
}
102-
if params.ScheduledDatetime.Unix() > 0 {
103-
urlParams.Set("scheduledDatetime", params.ScheduledDatetime.Format(time.RFC3339))
95+
if params == nil {
96+
return request, nil
10497
}
10598

106-
return urlParams, nil
99+
request.Type = params.Type
100+
if request.Type == "flash" {
101+
request.MClass = 0
102+
} else {
103+
request.MClass = 1
104+
}
105+
request.Reference = params.Reference
106+
request.Validity = params.Validity
107+
request.Gateway = params.Gateway
108+
request.TypeDetails = params.TypeDetails
109+
request.DataCoding = params.DataCoding
110+
request.ScheduledDatetime = params.ScheduledDatetime.Format(time.RFC3339)
111+
112+
return request, nil
107113
}
108114

109115
// paramsForMessageList converts the specified MessageListParams struct to a

0 commit comments

Comments
 (0)