Skip to content

Commit ffc5cb9

Browse files
authored
Merge branch 'master' into master
2 parents 8ff0795 + bf934da commit ffc5cb9

File tree

107 files changed

+4096
-1062
lines changed

Some content is hidden

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

107 files changed

+4096
-1062
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: true
1313
matrix:
14-
go-version: [ 1.14.x, 1.15.x, 1.16.x ]
14+
go-version: [ 1.16.x, 1.17.x, 1.18.x ]
1515

1616
name: Go ${{ matrix.go-version }}
1717

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 2-Clause License
22

3-
Copyright (c) 2014-2015, MessageBird
3+
Copyright (c) 2014-2022, MessageBird
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ MessageBird's REST API for Go
22
=============================
33
This repository contains the open source Go client for MessageBird's REST API. Documentation can be found at: https://developers.messagebird.com.
44

5-
[![Build Status](https://travis-ci.org/messagebird/go-rest-api.svg?branch=master)](https://travis-ci.org/messagebird/go-rest-api) [![PkgGoDev](https://pkg.go.dev/badge/github.com/messagebird/go-rest-api/v7)](https://pkg.go.dev/github.com/messagebird/go-rest-api/v7)
5+
[![Build Status](https://travis-ci.org/messagebird/go-rest-api.svg?branch=master)](https://travis-ci.org/messagebird/go-rest-api) [![PkgGoDev](https://pkg.go.dev/badge/github.com/messagebird/go-rest-api/v9)](https://pkg.go.dev/github.com/messagebird/go-rest-api/v9)
66

77
Requirements
88
------------
@@ -15,15 +15,15 @@ Installation
1515
The easiest way to use the MessageBird API in your Go project is to install it using *go get*:
1616

1717
```
18-
$ go get github.com/messagebird/go-rest-api/v7
18+
$ go get github.com/messagebird/go-rest-api/v9
1919
```
2020

2121
Examples
2222
--------
2323
Here is a quick example on how to get started. Assuming the **go get** installation worked, you can import the messagebird package like this:
2424

2525
```go
26-
import "github.com/messagebird/go-rest-api/v7"
26+
import "github.com/messagebird/go-rest-api/v9"
2727
```
2828

2929
Then, create an instance of **messagebird.Client**. It can be used to access the MessageBird APIs.
@@ -69,8 +69,8 @@ For this reason, errors returned by the `voice` package are of type `voice.Error
6969
An example of "simple" error handling is shown in the example above. Let's look how we can gain more in-depth insight in what exactly went wrong:
7070

7171
```go
72-
import "github.com/messagebird/go-rest-api/v7"
73-
import "github.com/messagebird/go-rest-api/v7/sms"
72+
import "github.com/messagebird/go-rest-api/v9"
73+
import "github.com/messagebird/go-rest-api/v9/sms"
7474

7575
// ...
7676

@@ -91,7 +91,7 @@ if err != nil {
9191
`voice.ErrorResponse` is very similar, except that it holds `voice.Error` structs - those contain only `Code` and `Message` (not description!) fields:
9292

9393
```go
94-
import "github.com/messagebird/go-rest-api/v7/voice"
94+
import "github.com/messagebird/go-rest-api/v9/voice"
9595

9696
// ...
9797

@@ -108,14 +108,6 @@ if err != nil {
108108
}
109109
```
110110

111-
Conversations WhatsApp Sandbox
112-
-------------
113-
To use the whatsapp sandbox you need to enable the `FeatureConversationsAPIWhatsAppSandbox` feature.
114-
115-
```go
116-
client.EnableFeatures(messagebird.FeatureConversationsAPIWhatsAppSandbox)
117-
```
118-
119111
Documentation
120112
-------------
121113
Complete documentation, instructions, and examples are available at:
@@ -127,4 +119,4 @@ If you're upgrading from older versions, please read the [Messagebird `go-rest-a
127119

128120
License
129121
-------
130-
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
122+
The MessageBird REST Client for Go is licensed under [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause). Copyright (c) 2022 MessageBird

UPGRADING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,23 @@ So all imports of `go-rest-api` are replaced as `go-rest-api/v6`.
103103
## `v6.1.0` -> `v7.0.0`
104104
### Verify Recipient type
105105
As v7 introduces support for using the Verify API with email recipients, the `Verify.Recipient` field has been changed from to a string type.
106+
107+
## `v7.1.0` -> `v8.0.0`
108+
### Update SMS API
109+
Added [missed fields](https://github.com/messagebird/go-rest-api/pull/119/commits/f9331269238f1518dd35d798a0fbf251bb04bb62) in SMS API.
110+
Updated `sms.Delete` method so now in return only error or nil as result.
111+
112+
## `v8.0.0` -> `v9.0.0`
113+
### General
114+
* New `PaginationRequest` instead of many in APIs.
115+
* New interface `messagebird.Client` and `messagebird.DefaultClient` instead of old implementation `messagebird.Client` and new `MockClient` for better testing.
116+
* List structures now have a plural ending instead of using word "List". Example: `ContactList` -> `Contacts`.
117+
* `conversations.ConversationStatus` replaced by `conversations.Status`.
118+
* `number.NumberPattern` replaced by `number.SearchPattern`.
119+
* Added [Partner Accounts API](https://developers.messagebird.com/api/partner/).
120+
### Conversations API
121+
* Replaced `conversations.CreateMessage` with `conversations.Reply` which send a new message to an existing conversation.
122+
* Replaced `conversations.ListMessages` with `conversations.ListConversationMessages` which fetch messages in indicated conversation.
123+
* Added `conversations.SendMessage` to send a message to a specific recipient in a specific platform.
124+
* Added `conversations.ListByContact` to retrieves the list of conversation IDs of a specific contact ID.
125+
* Now `conversations.ListMessages` retrieves a list of messages given a list of message IDs or a timestamp (not both).

api.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package messagebird
2+
3+
import (
4+
"net/url"
5+
"strconv"
6+
)
7+
8+
// PaginationRequest can be used to set pagination options in List().
9+
type PaginationRequest struct {
10+
Limit, Offset int
11+
}
12+
13+
func (cpr *PaginationRequest) QueryParams() string {
14+
if cpr == nil {
15+
return ""
16+
}
17+
18+
query := url.Values{}
19+
if cpr.Limit > 0 {
20+
query.Set("limit", strconv.Itoa(cpr.Limit))
21+
}
22+
if cpr.Offset >= 0 {
23+
query.Set("offset", strconv.Itoa(cpr.Offset))
24+
}
25+
26+
return query.Encode()
27+
}
28+
29+
// DefaultPagination provides reasonable values for List requests.
30+
var DefaultPagination = &PaginationRequest{
31+
Limit: 20,
32+
Offset: 0,
33+
}

balance/balance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package balance
33
import (
44
"net/http"
55

6-
messagebird "github.com/messagebird/go-rest-api/v7"
6+
messagebird "github.com/messagebird/go-rest-api/v9"
77
)
88

99
// Balance describes your balance information.
@@ -17,7 +17,7 @@ const path = "balance"
1717

1818
// Read returns the balance information for the account that is associated with
1919
// the access key.
20-
func Read(c *messagebird.Client) (*Balance, error) {
20+
func Read(c messagebird.Client) (*Balance, error) {
2121
balance := &Balance{}
2222
if err := c.Request(balance, http.MethodGet, path, nil); err != nil {
2323
return nil, err

balance/balance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"net/http"
55
"testing"
66

7-
messagebird "github.com/messagebird/go-rest-api/v7"
8-
"github.com/messagebird/go-rest-api/v7/internal/mbtest"
7+
messagebird "github.com/messagebird/go-rest-api/v9"
8+
"github.com/messagebird/go-rest-api/v9/internal/mbtest"
99
"github.com/stretchr/testify/assert"
1010
)
1111

client.go

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2014 MessageBird B.V.
2+
// Copyright (c) 2022 MessageBird B.V.
33
// All rights reserved.
44
//
55
// Author: Maurice Nonnekes <[email protected]>
@@ -20,45 +20,38 @@ import (
2020
"net/url"
2121
"runtime"
2222
"strings"
23-
"sync"
2423
"time"
2524
)
2625

2726
const (
2827
// ClientVersion is used in User-Agent request header to provide server with API level.
29-
ClientVersion = "7.0.0"
28+
ClientVersion = "9.0.0"
3029

3130
// Endpoint points you to MessageBird REST API.
3231
Endpoint = "https://rest.messagebird.com"
3332

3433
// httpClientTimeout is used to limit http.Client waiting time.
3534
httpClientTimeout = 15 * time.Second
36-
37-
// voiceHost is the host name for the Voice API.
38-
voiceHost = "voice.messagebird.com"
3935
)
4036

4137
var (
4238
// ErrUnexpectedResponse is used when there was an internal server error and nothing can be done at this point.
43-
ErrUnexpectedResponse = errors.New("The MessageBird API is currently unavailable")
39+
ErrUnexpectedResponse = errors.New("the MessageBird API is currently unavailable")
4440
)
4541

4642
// A Feature can be enabled
4743
type Feature int
4844

49-
const (
50-
// FeatureConversationsAPIWhatsAppSandbox Enables the WhatsApp sandbox for conversations API.
51-
FeatureConversationsAPIWhatsAppSandbox Feature = iota
52-
)
45+
type Client interface {
46+
Request(v interface{}, method, path string, data interface{}) error
47+
}
5348

54-
// Client is used to access API with a given key.
49+
// DefaultClient is used to access API with a given key.
5550
// Uses standard lib HTTP client internally, so should be reused instead of created as needed and it is safe for concurrent use.
56-
type Client struct {
57-
AccessKey string // The API access key.
58-
HTTPClient *http.Client // The HTTP client to send requests on.
59-
DebugLog *log.Logger // Optional logger for debugging purposes.
60-
features map[Feature]bool // Enabled features.
61-
featuresMutex sync.RWMutex // Mutex for accessing feature map.
51+
type DefaultClient struct {
52+
AccessKey string // The API access key.
53+
HTTPClient *http.Client // The HTTP client to send requests on.
54+
DebugLog *log.Logger // Optional logger for debugging purposes.
6255
}
6356

6457
type contentType string
@@ -72,51 +65,25 @@ const (
7265
// errorReader reads the provided byte slice into an appropriate error.
7366
type errorReader func([]byte) error
7467

75-
var voiceErrorReader errorReader
68+
var customErrorReader errorReader
69+
70+
// SetErrorReader takes an errorReader that must parse raw JSON errors
71+
func SetErrorReader(r errorReader) {
72+
customErrorReader = r
73+
}
7674

7775
// New creates a new MessageBird client object.
78-
func New(accessKey string) *Client {
79-
return &Client{
76+
func New(accessKey string) *DefaultClient {
77+
return &DefaultClient{
8078
AccessKey: accessKey,
8179
HTTPClient: &http.Client{
8280
Timeout: httpClientTimeout,
8381
},
84-
features: make(map[Feature]bool),
85-
}
86-
}
87-
88-
// SetVoiceErrorReader takes an errorReader that must parse raw JSON errors
89-
// returned from the Voice API.
90-
func SetVoiceErrorReader(r errorReader) {
91-
voiceErrorReader = r
92-
}
93-
94-
// EnableFeatures enables a feature.
95-
func (c *Client) EnableFeatures(feature Feature) {
96-
c.featuresMutex.Lock()
97-
defer c.featuresMutex.Unlock()
98-
c.features[feature] = true
99-
}
100-
101-
// DisableFeatures disables a feature.
102-
func (c *Client) DisableFeatures(feature Feature) {
103-
c.featuresMutex.Lock()
104-
defer c.featuresMutex.Unlock()
105-
c.features[feature] = false
106-
}
107-
108-
// IsFeatureEnabled checks if a feature is enabled.
109-
func (c *Client) IsFeatureEnabled(feature Feature) bool {
110-
c.featuresMutex.RLock()
111-
defer c.featuresMutex.RUnlock()
112-
if enabled, ok := c.features[feature]; ok {
113-
return enabled
11482
}
115-
return false
11683
}
11784

11885
// Request is for internal use only and unstable.
119-
func (c *Client) Request(v interface{}, method, path string, data interface{}) error {
86+
func (c *DefaultClient) Request(v interface{}, method, path string, data interface{}) error {
12087
if !strings.HasPrefix(path, "https://") && !strings.HasPrefix(path, "http://") {
12188
path = fmt.Sprintf("%s/%s", Endpoint, path)
12289
}
@@ -167,7 +134,7 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
167134
}
168135

169136
switch response.StatusCode {
170-
case http.StatusOK, http.StatusCreated:
137+
case http.StatusOK, http.StatusCreated, http.StatusAccepted:
171138
// Status codes 200 and 201 are indicative of being able to convert the
172139
// response body to the struct that was specified.
173140
if err := json.Unmarshal(responseBody, &v); err != nil {
@@ -185,17 +152,22 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
185152
return ErrUnexpectedResponse
186153
default:
187154
// Anything else than a 200/201/204/500 should be a JSON error.
188-
if uri.Host == voiceHost && voiceErrorReader != nil {
189-
return voiceErrorReader(responseBody)
155+
if customErrorReader != nil {
156+
return customErrorReader(responseBody)
190157
}
191158

192-
var errorResponse ErrorResponse
193-
if err := json.Unmarshal(responseBody, &errorResponse); err != nil {
194-
return err
195-
}
159+
return defaultErrorReader(responseBody)
160+
}
161+
}
196162

197-
return errorResponse
163+
func defaultErrorReader(b []byte) error {
164+
var errorResponse ErrorResponse
165+
166+
if err := json.Unmarshal(b, &errorResponse); err != nil {
167+
return fmt.Errorf("failed to unmarshal response json %s, error: %v", string(b), err)
198168
}
169+
170+
return errorResponse
199171
}
200172

201173
// prepareRequestBody takes untyped data and attempts constructing a meaningful

0 commit comments

Comments
 (0)