|
| 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! |
0 commit comments