Skip to content

Commit b6c60a2

Browse files
authored
Merge pull request #47 from messagebird/v5_staging
V5 staging
2 parents 722b6e0 + 607eec0 commit b6c60a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2778
-1537
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ 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 {
41-
// messagebird.ErrResponse means custom JSON errors.
42-
if err == messagebird.ErrResponse {
43-
for _, mbError := range balance.Errors {
44-
fmt.Printf("Error: %#v\n", mbError)
45-
}
46-
}
47-
48-
return
41+
switch errResp := err.(type) {
42+
case messagebird.ErrorResponse:
43+
for _, mbError := range errResp.Errors {
44+
fmt.Printf("Error: %#v\n", mbError)
45+
}
46+
}
47+
48+
return
4949
}
5050

5151
fmt.Println(" payment :", balance.Payment)
@@ -68,6 +68,10 @@ Documentation
6868
Complete documentation, instructions, and examples are available at:
6969
[https://developers.messagebird.com](https://developers.messagebird.com).
7070

71+
Upgrading
72+
---------
73+
If you're upgrading from older versions, please read the [Messagebird `go-rest-api` uprading guide](UPGRADING.md).
74+
7175
License
7276
-------
7377
The MessageBird REST Client for Go is licensed under [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause). Copyright (c) 2014, 2015, MessageBird

UPGRADING.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# MessageBird `go-rest-api` upgrading guide
2+
This guide documents breaking changes across major versions and should be taken into account when updating your dependencies.
3+
4+
## `v4.2.1` -> `v5.0.0`
5+
6+
### Package structure
7+
The most obvious change in this version is the new package structure. Nearly all code used to live in the `messagebird` package - `v5` changes this.
8+
9+
All resources (balance, voicemessage et al) are now moved to their own packages.
10+
This also means the functions no longer take the client as a receiver, but as their first parameter.
11+
12+
Let's see some code. We'll fetch the balance for your account.
13+
14+
Before:
15+
```go
16+
client := messagebird.New("YOUR_ACCESS_KEY")
17+
balance, err := client.Balance()
18+
19+
// ...
20+
```
21+
22+
After:
23+
```go
24+
client := messagebird.New("YOUR_ACCESS_KEY")
25+
balance, err := balance.Read(client)
26+
27+
// ...
28+
```
29+
30+
### Naming
31+
#### Functions
32+
Some function names have changed. They are now more consistent across the several resources(/packages).
33+
34+
Typically, the HTTP method for the underlaying API calls determine the function's name. Here's an overview.
35+
36+
| HTTP method | Function name | Note |
37+
| :------------ |:------------- | :--------------------------------------------------------------|
38+
| GET | List() | Lists a resource, e.g. `/some-resource` |
39+
| GET | Read() | Retrieves a single resource, e.g. `/some-resource/{resourceId}`|
40+
| POST | Create() | - |
41+
42+
#### Resources
43+
Previously, SMS messages were referred to simply as `messages` (e.g. `client.NewMessage()`) .
44+
The package that takes care of this now is named `sms`. Naming it `message` is a [bad idea](https://blog.golang.org/package-names), and `smsmessage` doesn't look very friendly.
45+
46+
MMS messages are handled by the `mms` package.
47+
48+
Many structs have been renamed too. We used to have `messagebird.MMSMessageParams`, but we don't want any stuttering like `mms.MMSMessageParams`. That's now simply `mms.Params`.
49+
50+
### Recipient
51+
The `Recipient.Recipient` field used to be of type `int`.
52+
On i386 architectures, the `int` type has only 32 bits, meaning MSISDNs overflowed this. [This PR](https://github.com/messagebird/go-rest-api/pull/19) fixes this by changing its type to `int64`.
53+
The struct is mainly used for unmarshalling responses so you likely need not worry about this.
54+
55+
### Error handling
56+
The way errors are handled changes significantly in `v5`.
57+
58+
Before, the errors returned by the MessageBird API would be included in the resulting resource. You would do this (ignoring the change to the package structure, as described above):
59+
60+
```go
61+
balance, err := balance.Read(client)
62+
if err != nil && err == messagebird.ErrResponse {
63+
for _, mbError := range balance.Errors {
64+
// Do something with mbError.
65+
}
66+
}
67+
```
68+
69+
Starting with `v5`, errors from the MessageBird API also implement the `error` interface.
70+
They are no longer defined on the resources, but instead returned as you're used to from most Go code (its last return value).
71+
72+
It looks like this:
73+
74+
```go
75+
balance, err := balance.Read(client)
76+
if err != nil {
77+
// Print a generic error message.
78+
log.Printf("%s\n", err)
79+
80+
// Or inspect individual errors:
81+
switch errResp := err.(type) {
82+
case messagebird.ErrorResponse:
83+
for _, mbError := range errResp.Errors {
84+
log.Printf("%d: %s in %s", mbError.Code, mbError.Description, mbError.Parameter)
85+
86+
// Implements `error` as well...
87+
log.Printf("%s\n", mbError)
88+
}
89+
}
90+
}
91+
```
92+
93+
### Other improvements
94+
Although those are all breaking changes, it's worth mentioning this new version brings a number of other improvements. An example is that the client now supports contacts and groups - enjoy!

balance.go

Lines changed: 0 additions & 9 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: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
1-
package messagebird
1+
package balance
22

3-
import "testing"
3+
import (
4+
"net/http"
5+
"testing"
46

5-
var balanceObject = []byte(`{
6-
"payment":"prepaid",
7-
"type":"credits",
8-
"amount":9.2
9-
}`)
7+
messagebird "github.com/messagebird/go-rest-api"
8+
"github.com/messagebird/go-rest-api/internal/mbtest"
9+
)
1010

1111
const Epsilon float32 = 0.001
1212

1313
func cmpFloat32(a, b float32) bool {
1414
return (a-b) < Epsilon && (b-a) < Epsilon
1515
}
1616

17-
func TestBalance(t *testing.T) {
18-
SetServerResponse(200, balanceObject)
17+
func TestMain(m *testing.M) {
18+
mbtest.EnableServer(m)
19+
}
20+
21+
func TestRead(t *testing.T) {
22+
mbtest.WillReturnTestdata(t, "balance.json", http.StatusOK)
23+
client := mbtest.Client(t)
1924

20-
balance, err := mbClient.Balance()
25+
balance, err := Read(client)
2126
if err != nil {
2227
t.Fatalf("Didn't expect error while fetching the balance: %s", err)
2328
}
2429

2530
if balance.Payment != "prepaid" {
2631
t.Errorf("Unexpected balance payment: %s", balance.Payment)
2732
}
33+
2834
if balance.Type != "credits" {
2935
t.Errorf("Unexpected balance type: %s", balance.Type)
3036
}
37+
3138
if !cmpFloat32(balance.Amount, 9.2) {
3239
t.Errorf("Unexpected balance amount: %.2f", balance.Amount)
3340
}
3441
}
3542

36-
func TestBalanceError(t *testing.T) {
37-
SetServerResponse(405, accessKeyErrorObject)
38-
_, err := mbClient.Balance()
43+
func TestReadError(t *testing.T) {
44+
mbtest.WillReturnAccessKeyError()
45+
client := mbtest.Client(t)
46+
47+
_, err := Read(client)
3948

40-
errorResponse, ok := err.(ErrorResponse)
49+
errorResponse, ok := err.(messagebird.ErrorResponse)
4150
if !ok {
4251
t.Fatalf("Expected ErrorResponse to be returned, instead I got %s", err)
4352
}

balance/testdata/balance.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"payment": "prepaid",
3+
"type": "credits",
4+
"amount": 9.2
5+
}

0 commit comments

Comments
 (0)