Skip to content

Commit dcc3e63

Browse files
committed
Move CallFlows to the new voice package
1 parent 6b5f3fd commit dcc3e63

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

callflow.go renamed to voice/callflow.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package messagebird
1+
package voice
22

33
import (
44
"encoding/json"
55
"fmt"
66
"time"
7+
8+
"github.com/messagebird/go-rest-api"
79
)
810

911
// CallFlowList is a single page from the total collection of call flows.
@@ -111,9 +113,9 @@ func (callflow *CallFlow) UnmarshalJSON(data []byte) error {
111113
// CallFlowByID fetches a callflow by it's ID.
112114
//
113115
// An error is returned if no such call flow exists or is accessible.
114-
func (c *Client) CallFlowByID(id string) (*CallFlow, error) {
116+
func CallFlowByID(client *messagebird.Client, id string) (*CallFlow, error) {
115117
callflow := &CallFlow{}
116-
err := c.Request(callflow, "GET", "call-flow/"+id, nil)
118+
err := client.Request(callflow, "GET", "call-flow/"+id, nil)
117119
return callflow, err
118120
}
119121

@@ -122,41 +124,43 @@ func (c *Client) CallFlowByID(id string) (*CallFlow, error) {
122124
// Page indices start at 1.
123125
//
124126
// Typically, a page contains 10 callflows.
125-
func (c *Client) CallFlows(page int) (*CallFlowList, error) {
127+
func CallFlows(client *messagebird.Client, page int) (*CallFlowList, error) {
126128
list := &CallFlowList{}
127-
err := c.Request(list, "GET", fmt.Sprintf("call-flow/?page=%d", page), nil)
129+
err := client.Request(list, "GET", fmt.Sprintf("call-flow/?page=%d", page), nil)
128130
return list, err
129131
}
130132

131-
// CreateCallFlow creates the specified callflow.
133+
// CreateCallFlow creates the callflow remotely.
132134
//
133-
// The updated callflow is returned.
134-
func (c *Client) CreateCallFlow(callflow *CallFlow) (*CallFlow, error) {
135+
// The callflow is updated in-place.
136+
func (callflow *CallFlow) Create(client *messagebird.Client) error {
135137
var data struct {
136138
Data []CallFlow `json:"data"`
137139
}
138-
if err := c.Request(&data, "POST", "call-flow/", callflow); err != nil {
139-
return nil, err
140+
if err := client.Request(&data, "POST", "call-flow/", callflow); err != nil {
141+
return err
140142
}
141-
return &data.Data[0], nil
143+
*callflow = data.Data[0]
144+
return nil
142145
}
143146

144-
// UpdateCallFlow updates the specified call flow by overwriting it.
147+
// UpdateCallFlow updates the call flow by overwriting it.
145148
//
146149
// An error is returned if no such call flow exists or is accessible.
147-
func (c *Client) UpdateCallFlow(callflow *CallFlow) (*CallFlow, error) {
150+
func (callflow *CallFlow) Update(client *messagebird.Client) error {
148151
var data struct {
149152
Data []CallFlow `json:"data"`
150153
}
151-
if err := c.Request(callflow, "PUT", "call-flow/"+callflow.ID, callflow); err != nil {
152-
return nil, err
154+
if err := client.Request(callflow, "PUT", "call-flow/"+callflow.ID, callflow); err != nil {
155+
return err
153156
}
154-
return &data.Data[0], nil
157+
*callflow = data.Data[0]
158+
return nil
155159
}
156160

157-
// DeleteCallFlow deletes the specified CallFlow.
158-
func (c *Client) DeleteCallFlow(callflow *CallFlow) error {
159-
return c.Request(nil, "DELETE", "call-flow/"+callflow.ID, nil)
161+
// DeleteCallFlow deletes the CallFlow.
162+
func (callflow *CallFlow) Delete(client *messagebird.Client) error {
163+
return client.Request(nil, "DELETE", "call-flow/"+callflow.ID, nil)
160164
}
161165

162166
// A CallFlowStep is a single step that can be taken in a callflow.

callflow_test.go renamed to voice/callflow_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package messagebird
1+
package voice
22

33
import (
44
"encoding/json"
@@ -131,7 +131,7 @@ func TestCallFlowJSONUnmarshal(t *testing.T) {
131131
}
132132

133133
func TestCreateCallFlow(t *testing.T) {
134-
SetServerResponse(http.StatusOK, []byte(`{
134+
mbClient, stop := testClient(http.StatusOK, []byte(`{
135135
"data": [
136136
{
137137
"id": "the-id",
@@ -141,8 +141,9 @@ func TestCreateCallFlow(t *testing.T) {
141141
}
142142
]
143143
}`))
144-
newCf, err := mbClient.CreateCallFlow(&CallFlow{})
145-
if err != nil {
144+
defer stop()
145+
newCf := &CallFlow{}
146+
if err := newCf.Create(mbClient); err != nil {
146147
t.Fatal(err)
147148
}
148149
if newCf.ID != "the-id" {

0 commit comments

Comments
 (0)