Skip to content

Commit 6215e0d

Browse files
authored
Merge pull request #69 from mariuspot/master
Add WhatsApp Sandbox support to Conversations API
2 parents bfb6199 + 2abf720 commit 6215e0d

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ 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+
111119
Documentation
112120
-------------
113121
Complete documentation, instructions, and examples are available at:

client.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/url"
2121
"runtime"
2222
"strings"
23+
"sync"
2324
"time"
2425
)
2526

@@ -42,12 +43,22 @@ var (
4243
ErrUnexpectedResponse = errors.New("The MessageBird API is currently unavailable")
4344
)
4445

46+
// A Feature can be enabled
47+
type Feature int
48+
49+
const (
50+
// FeatureConversationsAPIWhatsAppSandbox Enables the WhatsApp sandbox for conversations API.
51+
FeatureConversationsAPIWhatsAppSandbox Feature = iota
52+
)
53+
4554
// Client is used to access API with a given key.
4655
// Uses standard lib HTTP client internally, so should be reused instead of created as needed and it is safe for concurrent use.
4756
type Client struct {
48-
AccessKey string // The API access key
49-
HTTPClient *http.Client // The HTTP client to send requests on
50-
DebugLog *log.Logger // Optional logger for debugging purposes
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.
5162
}
5263

5364
type contentType string
@@ -70,6 +81,7 @@ func New(accessKey string) *Client {
7081
HTTPClient: &http.Client{
7182
Timeout: httpClientTimeout,
7283
},
84+
features: make(map[Feature]bool),
7385
}
7486
}
7587

@@ -79,6 +91,30 @@ func SetVoiceErrorReader(r errorReader) {
7991
voiceErrorReader = r
8092
}
8193

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
114+
}
115+
return false
116+
}
117+
82118
// Request is for internal use only and unstable.
83119
func (c *Client) Request(v interface{}, method, path string, data interface{}) error {
84120
if !strings.HasPrefix(path, "https://") && !strings.HasPrefix(path, "http://") {

conversation/api.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const (
1616
// https://conversations.messagebird.com/v1/webhooks).
1717
apiRoot = "https://conversations.messagebird.com/v1"
1818

19+
whatsappSandboxAPIRoot = "https://whatsapp-sandbox.messagebird.com/v1"
20+
1921
// path is the path for the Conversation resource, relative to apiRoot.
2022
path = "conversations"
2123

@@ -218,7 +220,13 @@ const (
218220
// prefix the path with the Conversation API's root. This ensures the client
219221
// doesn't "handle" this for us: by default, it uses the REST API.
220222
func request(c *messagebird.Client, v interface{}, method, path string, data interface{}) error {
221-
return c.Request(v, method, fmt.Sprintf("%s/%s", apiRoot, path), data)
223+
var root string
224+
if c.IsFeatureEnabled(messagebird.FeatureConversationsAPIWhatsAppSandbox) {
225+
root = whatsappSandboxAPIRoot
226+
} else {
227+
root = apiRoot
228+
}
229+
return c.Request(v, method, fmt.Sprintf("%s/%s", root, path), data)
222230
}
223231

224232
// paginationQuery builds the query string for paginated endpoints.

internal/mbtest/test_client.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ func client(t *testing.T, accessKey string) *messagebird.Client {
3636
})
3737
},
3838
}
39+
client := messagebird.New(accessKey)
40+
client.HTTPClient.Transport = transport
41+
client.DebugLog = testLogger(t)
3942

40-
return &messagebird.Client{
41-
AccessKey: accessKey,
42-
HTTPClient: &http.Client{
43-
Transport: transport,
44-
},
45-
DebugLog: testLogger(t),
46-
}
43+
return client
4744
}
4845

4946
// testLogger creates a new logger that writes to the test's output.

0 commit comments

Comments
 (0)