Skip to content

Commit 406aea7

Browse files
committed
Create a messagebird.New() function and update the documentation to reflect that addition; Also shorthand some 'if err' stuff
1 parent 5ace938 commit 406aea7

File tree

9 files changed

+31
-33
lines changed

9 files changed

+31
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import "github.com/messagebird/go-rest-api/messagebird"
2929
Then, create an instance of **messagebird.Client**:
3030

3131
```go
32-
mb := &messagebird.Client{AccessKey: "test_gshuPaZoeEG6ovbc8M79w0QyM"}
32+
mb := messagebird.New("test_gshuPaZoeEG6ovbc8M79w0QyM")
3333
```
3434

3535
Now you can query the API for information or send data. For example, if we want to request our balance information you'd do something like this:

examples/balance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var AccessKey = "test_gshuPaZoeEG6ovbc8M79w0QyM"
1010

1111
func main() {
1212
// Create a MessageBird client with the specified AccessKey.
13-
mb := &messagebird.Client{AccessKey: AccessKey}
13+
mb := messagebird.New(AccessKey)
1414

1515
// Fetch the Balance object.
1616
balance, err := mb.Balance()

examples/hlr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
}
1717

1818
// Create a MessageBird client with the specified AccessKey.
19-
mb := &messagebird.Client{AccessKey: AccessKey}
19+
mb := messagebird.New(AccessKey)
2020

2121
// Fetch the HLR object.
2222
hlr, err := mb.HLR("d26c94c0353bd8e171a3979h97860638")

examples/hlr_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var AccessKey = "test_gshuPaZoeEG6ovbc8M79w0QyM"
1010

1111
func main() {
1212
// Create a MessageBird client with the specified AccessKey.
13-
mb := &messagebird.Client{AccessKey: AccessKey}
13+
mb := messagebird.New(AccessKey)
1414

1515
// Fetch the HLR object.
1616
hlr, err := mb.CreateHLR("31612345678", "MyReference")

examples/message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
}
1717

1818
// Create a MessageBird client with the specified AccessKey.
19-
mb := &messagebird.Client{AccessKey: AccessKey}
19+
mb := messagebird.New(AccessKey)
2020

2121
// Fetch the Message object.
2222
message, err := mb.Message(MessageId)

examples/message_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var AccessKey = "test_gshuPaZoeEG6ovbc8M79w0QyM"
1111

1212
func main() {
1313
// Create a MessageBird client with the specified AccessKey.
14-
mb := &messagebird.Client{AccessKey: AccessKey}
14+
mb := messagebird.New(AccessKey)
1515

1616
// The optional parameters.
1717
params := &url.Values{"reference": {"MyReference"}}

examples/voice_message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
}
1717

1818
// Create a MessageBird client with the specified AccessKey.
19-
mb := &messagebird.Client{AccessKey: AccessKey}
19+
mb := messagebird.New(AccessKey)
2020

2121
// Fetch the VoiceMessage object.
2222
vmsg, err := mb.VoiceMessage(MessageId)

examples/voice_message_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var AccessKey = "test_gshuPaZoeEG6ovbc8M79w0QyM"
1111

1212
func main() {
1313
// Create a MessageBird client with the specified AccessKey.
14-
mb := &messagebird.Client{AccessKey: AccessKey}
14+
mb := messagebird.New(AccessKey)
1515

1616
// The optional parameters.
1717
params := &url.Values{"reference": {"MyReference"}}

messagebird/messagebird.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
)
1818

1919
const (
20-
CLIENT_VERSION = "1.0.0"
21-
ENDPOINT = "https://rest.messagebird.com"
20+
ClientVersion = "1.0.1"
21+
Endpoint = "https://rest.messagebird.com"
2222
)
2323

2424
type Recipient struct {
@@ -97,20 +97,24 @@ type Client struct {
9797
AccessKey string
9898
}
9999

100+
// Create a new Client.
101+
func New(AccessKey string) *Client {
102+
return &Client{AccessKey}
103+
}
104+
100105
// This function performs a call to MessageBird's HTTP API and expects JSON in
101106
// return. It then tries to unmarshal the JSON body of the response to the
102107
// specified struct.
103108
func (c Client) request(v interface{}, path string, params *url.Values) error {
104109
var request *http.Request
105110

106111
// Construct the URI of the request.
107-
uri, err := url.Parse(ENDPOINT + "/" + path)
112+
uri, err := url.Parse(Endpoint + "/" + path)
108113
if err != nil {
109114
return err
110115
}
111116

112-
// Create an http.Client and construct a new request.
113-
client := &http.Client{}
117+
// Construct a new request.
114118
if params == nil {
115119
request, err = http.NewRequest("GET", uri.String(), nil)
116120
} else {
@@ -123,14 +127,15 @@ func (c Client) request(v interface{}, path string, params *url.Values) error {
123127
// Add a basic set of headers to the request.
124128
request.Header.Add("Accept", "application/json")
125129
request.Header.Add("Authorization", "AccessKey "+c.AccessKey)
126-
request.Header.Add("User-Agent", "MessageBird/ApiClient/"+CLIENT_VERSION+" Go/"+runtime.Version())
130+
request.Header.Add("User-Agent", "MessageBird/ApiClient/"+ClientVersion+" Go/"+runtime.Version())
127131

128132
// Add the Content-Type header if this is a POST request.
129133
if params != nil {
130134
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
131135
}
132136

133-
// Execute the HTTP request and wait for a response.
137+
// Create an http.Client, execute the HTTP request and wait for a response.
138+
client := &http.Client{}
134139
response, err := client.Do(request)
135140
if err != nil {
136141
return err
@@ -146,8 +151,7 @@ func (c Client) request(v interface{}, path string, params *url.Values) error {
146151
}
147152

148153
// Convert the JSON body to the specified struct.
149-
err = json.Unmarshal(body, v)
150-
if err != nil {
154+
if err = json.Unmarshal(body, v); err != nil {
151155
return err
152156
}
153157

@@ -158,8 +162,7 @@ func (c Client) request(v interface{}, path string, params *url.Values) error {
158162
func (c Client) Balance() (*Balance, error) {
159163
balance := &Balance{}
160164

161-
err := c.request(balance, "balance", nil)
162-
if err != nil {
165+
if err := c.request(balance, "balance", nil); err != nil {
163166
return nil, err
164167
}
165168

@@ -170,8 +173,7 @@ func (c Client) Balance() (*Balance, error) {
170173
func (c Client) HLR(id string) (*HLR, error) {
171174
hlr := &HLR{}
172175

173-
err := c.request(hlr, "hlr/"+id, nil)
174-
if err != nil {
176+
if err := c.request(hlr, "hlr/"+id, nil); err != nil {
175177
return nil, err
176178
}
177179

@@ -186,8 +188,7 @@ func (c Client) CreateHLR(msisdn string, reference string) (*HLR, error) {
186188

187189
hlr := &HLR{}
188190

189-
err := c.request(hlr, "hlr", params)
190-
if err != nil {
191+
if err := c.request(hlr, "hlr", params); err != nil {
191192
return nil, err
192193
}
193194

@@ -198,16 +199,14 @@ func (c Client) CreateHLR(msisdn string, reference string) (*HLR, error) {
198199
func (c Client) Message(id string) (*Message, error) {
199200
msg := &Message{}
200201

201-
err := c.request(msg, "messages/"+id, nil)
202-
if err != nil {
202+
if err := c.request(msg, "messages/"+id, nil); err != nil {
203203
return nil, err
204204
}
205205

206206
return msg, nil
207207
}
208208

209-
// This function creates a new message, which is sent to one or more
210-
// recipients.
209+
// This function creates a new message, which is sent to one or more recipients.
211210
func (c Client) CreateMessage(originator string, recipients []string, body string, params *url.Values) (*Message, error) {
212211
recips := strings.Join(recipients, ",")
213212

@@ -224,25 +223,25 @@ func (c Client) CreateMessage(originator string, recipients []string, body strin
224223

225224
msg := &Message{}
226225

227-
err := c.request(msg, "messages", params)
228-
if err != nil {
226+
if err := c.request(msg, "messages", params); err != nil {
229227
return nil, err
230228
}
231229

232230
return msg, nil
233231
}
234232

233+
// This function retrieves the information of a specific voice message.
235234
func (c Client) VoiceMessage(id string) (*VoiceMessage, error) {
236235
vmsg := &VoiceMessage{}
237236

238-
err := c.request(vmsg, "voicemessages/"+id, nil)
239-
if err != nil {
237+
if err := c.request(vmsg, "voicemessages/"+id, nil); err != nil {
240238
return nil, err
241239
}
242240

243241
return vmsg, nil
244242
}
245243

244+
// This function creates a new voice message
246245
func (c Client) CreateVoiceMessage(recipients []string, body string, params *url.Values) (*VoiceMessage, error) {
247246
recips := strings.Join(recipients, ",")
248247

@@ -257,8 +256,7 @@ func (c Client) CreateVoiceMessage(recipients []string, body string, params *url
257256

258257
vmsg := &VoiceMessage{}
259258

260-
err := c.request(vmsg, "voicemessages", params)
261-
if err != nil {
259+
if err := c.request(vmsg, "voicemessages", params); err != nil {
262260
return nil, err
263261
}
264262

0 commit comments

Comments
 (0)