Skip to content

Commit c5bf92b

Browse files
authored
Merge pull request #41 from messagebird/refactor-package
Refactor package
2 parents f522693 + 348eca0 commit c5bf92b

25 files changed

+1342
-1178
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Now you can query the API for information or send data. For example, if we want
3636

3737
```go
3838
// Request the balance information, returned as a Balance object.
39-
balance, err := client.Balance()
39+
balance, err := balance.Read(client)
4040
if err != nil {
4141
switch errResp := err.(type) {
4242
case messagebird.ErrorResponse:

balance.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

balance/balance.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package balance
2+
3+
import (
4+
"net/http"
5+
6+
messagebird "github.com/messagebird/go-rest-api"
7+
)
8+
9+
// Balance describes your balance information.
10+
type Balance struct {
11+
Payment string
12+
Type string
13+
Amount float32
14+
}
15+
16+
const path = "balance"
17+
18+
// Read returns the balance information for the account that is associated with
19+
// the access key.
20+
func Read(c *messagebird.Client) (*Balance, error) {
21+
balance := &Balance{}
22+
if err := c.Request(balance, http.MethodGet, path, nil); err != nil {
23+
return nil, err
24+
}
25+
26+
return balance, nil
27+
}

balance_test.go renamed to balance/balance_test.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,58 @@
1-
package messagebird
1+
package balance
22

3-
import "testing"
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
messagebird "github.com/messagebird/go-rest-api"
8+
"github.com/messagebird/go-rest-api/internal/messagebirdtest"
9+
)
410

511
var balanceObject = []byte(`{
6-
"payment":"prepaid",
7-
"type":"credits",
8-
"amount":9.2
9-
}`)
12+
"payment":"prepaid",
13+
"type":"credits",
14+
"amount":9.2
15+
}`)
1016

1117
const Epsilon float32 = 0.001
1218

1319
func cmpFloat32(a, b float32) bool {
1420
return (a-b) < Epsilon && (b-a) < Epsilon
1521
}
1622

17-
func TestBalance(t *testing.T) {
18-
SetServerResponse(200, balanceObject)
23+
func TestMain(m *testing.M) {
24+
messagebirdtest.EnableServer(m)
25+
}
26+
27+
func TestRead(t *testing.T) {
28+
messagebirdtest.WillReturn(balanceObject, http.StatusOK)
29+
client := messagebirdtest.Client(t)
1930

20-
balance, err := mbClient.Balance()
31+
balance, err := Read(client)
2132
if err != nil {
2233
t.Fatalf("Didn't expect error while fetching the balance: %s", err)
2334
}
2435

2536
if balance.Payment != "prepaid" {
2637
t.Errorf("Unexpected balance payment: %s", balance.Payment)
2738
}
39+
2840
if balance.Type != "credits" {
2941
t.Errorf("Unexpected balance type: %s", balance.Type)
3042
}
43+
3144
if !cmpFloat32(balance.Amount, 9.2) {
3245
t.Errorf("Unexpected balance amount: %.2f", balance.Amount)
3346
}
3447
}
3548

36-
func TestBalanceError(t *testing.T) {
37-
SetServerResponse(405, accessKeyErrorObject)
38-
_, err := mbClient.Balance()
49+
func TestReadError(t *testing.T) {
50+
messagebirdtest.WillReturnAccessKeyError()
51+
client := messagebirdtest.Client(t)
52+
53+
_, err := Read(client)
3954

40-
errorResponse, ok := err.(ErrorResponse)
55+
errorResponse, ok := err.(messagebird.ErrorResponse)
4156
if !ok {
4257
t.Fatalf("Expected ErrorResponse to be returned, instead I got %s", err)
4358
}

client.go

Lines changed: 0 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ const (
3434
httpClientTimeout = 15 * time.Second
3535
)
3636

37-
const (
38-
// HLRPath represents the path to the HLR resource.
39-
HLRPath = "hlr"
40-
// MessagePath represents the path to the Message resource.
41-
MessagePath = "messages"
42-
// MMSPath represents the path to the MMS resource.
43-
MMSPath = "mms"
44-
// VoiceMessagePath represents the path to the VoiceMessage resource.
45-
VoiceMessagePath = "voicemessages"
46-
// VerifyPath represents the path to the Verify resource.
47-
VerifyPath = "verify"
48-
// LookupPath represents the path to the Lookup resource.
49-
LookupPath = "lookup"
50-
)
51-
5237
var (
5338
// ErrUnexpectedResponse is used when there was an internal server error and nothing can be done at this point.
5439
ErrUnexpectedResponse = errors.New("The MessageBird API is currently unavailable")
@@ -146,224 +131,3 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
146131

147132
return errorResponse
148133
}
149-
150-
// Balance returns the balance information for the account that is associated
151-
// with the access key.
152-
func (c *Client) Balance() (*Balance, error) {
153-
balance := &Balance{}
154-
if err := c.Request(balance, http.MethodGet, "balance", nil); err != nil {
155-
return nil, err
156-
}
157-
158-
return balance, nil
159-
}
160-
161-
// HLR looks up an existing HLR object for the specified id that was previously
162-
// created by the NewHLR function.
163-
func (c *Client) HLR(id string) (*HLR, error) {
164-
hlr := &HLR{}
165-
if err := c.Request(hlr, http.MethodGet, HLRPath+"/"+id, nil); err != nil {
166-
return nil, err
167-
}
168-
169-
return hlr, nil
170-
}
171-
172-
// HLRs lists all HLR objects that were previously created by the NewHLR
173-
// function.
174-
func (c *Client) HLRs() (*HLRList, error) {
175-
hlrList := &HLRList{}
176-
if err := c.Request(hlrList, http.MethodGet, HLRPath, nil); err != nil {
177-
return nil, err
178-
}
179-
180-
return hlrList, nil
181-
}
182-
183-
// NewHLR retrieves the information of an existing HLR.
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
188-
}
189-
190-
hlr := &HLR{}
191-
192-
if err := c.Request(hlr, http.MethodPost, HLRPath, requestData); err != nil {
193-
return nil, err
194-
}
195-
196-
return hlr, nil
197-
}
198-
199-
// Message retrieves the information of an existing Message.
200-
func (c *Client) Message(id string) (*Message, error) {
201-
message := &Message{}
202-
if err := c.Request(message, http.MethodGet, MessagePath+"/"+id, nil); err != nil {
203-
return nil, err
204-
}
205-
206-
return message, nil
207-
}
208-
209-
// Messages retrieves all messages of the user represented as a MessageList object.
210-
func (c *Client) Messages(msgListParams *MessageListParams) (*MessageList, error) {
211-
messageList := &MessageList{}
212-
params, err := paramsForMessageList(msgListParams)
213-
if err != nil {
214-
return messageList, err
215-
}
216-
217-
if err := c.Request(messageList, http.MethodGet, MessagePath+"?"+params.Encode(), nil); err != nil {
218-
return nil, err
219-
}
220-
221-
return messageList, nil
222-
}
223-
224-
// NewMessage creates a new message for one or more recipients.
225-
func (c *Client) NewMessage(originator string, recipients []string, body string, msgParams *MessageParams) (*Message, error) {
226-
requestData, err := requestDataForMessage(originator, recipients, body, msgParams)
227-
if err != nil {
228-
return nil, err
229-
}
230-
231-
message := &Message{}
232-
if err := c.Request(message, http.MethodPost, MessagePath, requestData); err != nil {
233-
return nil, err
234-
}
235-
236-
return message, nil
237-
}
238-
239-
// MMSMessage retrieves the information of an existing MmsMessage.
240-
func (c *Client) MMSMessage(id string) (*MMSMessage, error) {
241-
mmsMessage := &MMSMessage{}
242-
if err := c.Request(mmsMessage, http.MethodGet, MMSPath+"/"+id, nil); err != nil {
243-
return nil, err
244-
}
245-
246-
return mmsMessage, nil
247-
}
248-
249-
// NewMMSMessage creates a new MMS message for one or more recipients.
250-
func (c *Client) NewMMSMessage(originator string, recipients []string, msgParams *MMSMessageParams) (*MMSMessage, error) {
251-
params, err := paramsForMMSMessage(msgParams)
252-
if err != nil {
253-
return nil, err
254-
}
255-
256-
params.Set("originator", originator)
257-
params.Set("recipients", strings.Join(recipients, ","))
258-
259-
mmsMessage := &MMSMessage{}
260-
if err := c.Request(mmsMessage, http.MethodPost, MMSPath, params); err != nil {
261-
return nil, err
262-
}
263-
264-
return mmsMessage, nil
265-
}
266-
267-
// VoiceMessage retrieves the information of an existing VoiceMessage.
268-
func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
269-
message := &VoiceMessage{}
270-
if err := c.Request(message, http.MethodGet, VoiceMessagePath+"/"+id, nil); err != nil {
271-
return nil, err
272-
}
273-
274-
return message, nil
275-
}
276-
277-
// VoiceMessages retrieves all VoiceMessages of the user.
278-
func (c *Client) VoiceMessages() (*VoiceMessageList, error) {
279-
messageList := &VoiceMessageList{}
280-
if err := c.Request(messageList, http.MethodGet, VoiceMessagePath, nil); err != nil {
281-
return nil, err
282-
}
283-
284-
return messageList, nil
285-
}
286-
287-
// NewVoiceMessage creates a new voice message for one or more recipients.
288-
func (c *Client) NewVoiceMessage(recipients []string, body string, params *VoiceMessageParams) (*VoiceMessage, error) {
289-
requestData, err := requestDataForVoiceMessage(recipients, body, params)
290-
if err != nil {
291-
return nil, err
292-
}
293-
294-
message := &VoiceMessage{}
295-
if err := c.Request(message, http.MethodPost, VoiceMessagePath, requestData); err != nil {
296-
return nil, err
297-
}
298-
299-
return message, nil
300-
}
301-
302-
// NewVerify generates a new One-Time-Password for one recipient.
303-
func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, error) {
304-
requestData, err := requestDataForVerify(recipient, params)
305-
if err != nil {
306-
return nil, err
307-
}
308-
309-
verify := &Verify{}
310-
if err := c.Request(verify, http.MethodPost, VerifyPath, requestData); err != nil {
311-
return nil, err
312-
}
313-
314-
return verify, nil
315-
}
316-
317-
// VerifyToken performs token value check against MessageBird API.
318-
func (c *Client) VerifyToken(id, token string) (*Verify, error) {
319-
params := &url.Values{}
320-
params.Set("token", token)
321-
322-
path := VerifyPath + "/" + id + "?" + params.Encode()
323-
324-
verify := &Verify{}
325-
if err := c.Request(verify, http.MethodGet, path, nil); err != nil {
326-
return nil, err
327-
}
328-
329-
return verify, nil
330-
}
331-
332-
// Lookup performs a new lookup for the specified number.
333-
func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, error) {
334-
urlParams := paramsForLookup(params)
335-
path := LookupPath + "/" + phoneNumber + "?" + urlParams.Encode()
336-
337-
lookup := &Lookup{}
338-
if err := c.Request(lookup, http.MethodPost, path, nil); err != nil {
339-
return nil, err
340-
}
341-
342-
return lookup, nil
343-
}
344-
345-
// NewLookupHLR creates a new HLR lookup for the specified number.
346-
func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
347-
requestData := requestDataForLookup(params)
348-
path := LookupPath + "/" + phoneNumber + "/" + HLRPath
349-
350-
hlr := &HLR{}
351-
if err := c.Request(hlr, http.MethodPost, path, requestData); err != nil {
352-
return nil, err
353-
}
354-
355-
return hlr, nil
356-
}
357-
358-
// LookupHLR performs a HLR lookup for the specified number.
359-
func (c *Client) LookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
360-
urlParams := paramsForLookup(params)
361-
path := LookupPath + "/" + phoneNumber + "/" + HLRPath + "?" + urlParams.Encode()
362-
363-
hlr := &HLR{}
364-
if err := c.Request(hlr, http.MethodGet, path, nil); err != nil {
365-
return nil, err
366-
}
367-
368-
return hlr, nil
369-
}

0 commit comments

Comments
 (0)