Skip to content

Commit 326d2a8

Browse files
committed
Set the correct API root for Voice requests
1 parent 6288dcd commit 326d2a8

File tree

8 files changed

+24
-18
lines changed

8 files changed

+24
-18
lines changed

client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ func New(AccessKey string) *Client {
6868

6969
// Request is for internal use only and unstable.
7070
func (c *Client) Request(v interface{}, method, path string, data interface{}) error {
71-
uri, err := url.Parse(Endpoint + "/" + path)
71+
if !strings.HasPrefix(path, "https://") && !strings.HasPrefix(path, "http://") {
72+
path = fmt.Sprintf("%s/%s", Endpoint, path)
73+
}
74+
uri, err := url.Parse(path)
7275
if err != nil {
7376
return err
7477
}

voice/call.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ func (call *Call) UnmarshalJSON(data []byte) error {
108108
// An error is returned if no such call flow exists or is accessible.
109109
func CallByID(client *messagebird.Client, id string) (*Call, error) {
110110
call := &Call{}
111-
err := client.Request(call, http.MethodGet, "calls/"+id, nil)
111+
err := client.Request(call, http.MethodGet, apiRoot+"/calls/"+id, nil)
112112
return call, err
113113
}
114114

115115
// Calls returns a Paginator which iterates over all Calls.
116116
func Calls(client *messagebird.Client) *Paginator {
117-
return newPaginator(client, "calls/", reflect.TypeOf(Call{}))
117+
return newPaginator(client, apiRoot+"/calls/", reflect.TypeOf(Call{}))
118118
}
119119

120120
// InitiateCall initiates an outbound call.
@@ -141,18 +141,18 @@ func InitiateCall(client *messagebird.Client, source, destination string, callfl
141141
body.Webhook.Token = webhook.Token
142142
}
143143
call := &Call{}
144-
err := client.Request(call, http.MethodPost, "calls/", body)
144+
err := client.Request(call, http.MethodPost, apiRoot+"/calls/", body)
145145
return call, err
146146
}
147147

148148
// Delete deletes the Call.
149149
//
150150
// If the call is in progress, it hangs up all legs.
151151
func (call *Call) Delete(client *messagebird.Client) error {
152-
return client.Request(nil, http.MethodDelete, "calls/"+call.ID, nil)
152+
return client.Request(nil, http.MethodDelete, apiRoot+"/calls/"+call.ID, nil)
153153
}
154154

155155
// Legs returns a paginator over all Legs associated with a call.
156156
func (call *Call) Legs(client *messagebird.Client) *Paginator {
157-
return newPaginator(client, fmt.Sprintf("calls/%s/legs", call.ID), reflect.TypeOf(Leg{}))
157+
return newPaginator(client, fmt.Sprintf("%s/calls/%s/legs", apiRoot, call.ID), reflect.TypeOf(Leg{}))
158158
}

voice/callflow.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ func (callflow *CallFlow) UnmarshalJSON(data []byte) error {
106106
// An error is returned if no such call flow exists or is accessible.
107107
func CallFlowByID(client *messagebird.Client, id string) (*CallFlow, error) {
108108
callflow := &CallFlow{}
109-
err := client.Request(callflow, http.MethodGet, "call-flows/"+id, nil)
109+
err := client.Request(callflow, http.MethodGet, apiRoot+"/call-flows/"+id, nil)
110110
return callflow, err
111111
}
112112

113113
// CallFlows returns a Paginator which iterates over all CallFlows.
114114
func CallFlows(client *messagebird.Client) *Paginator {
115-
return newPaginator(client, "call-flows/", reflect.TypeOf(CallFlow{}))
115+
return newPaginator(client, apiRoot+"/call-flows/", reflect.TypeOf(CallFlow{}))
116116
}
117117

118118
// Create creates the callflow remotely.
@@ -122,7 +122,7 @@ func (callflow *CallFlow) Create(client *messagebird.Client) error {
122122
var data struct {
123123
Data []CallFlow `json:"data"`
124124
}
125-
if err := client.Request(&data, http.MethodPost, "call-flows/", callflow); err != nil {
125+
if err := client.Request(&data, http.MethodPost, apiRoot+"/call-flows/", callflow); err != nil {
126126
return err
127127
}
128128
*callflow = data.Data[0]
@@ -136,7 +136,7 @@ func (callflow *CallFlow) Update(client *messagebird.Client) error {
136136
var data struct {
137137
Data []CallFlow `json:"data"`
138138
}
139-
if err := client.Request(&data, http.MethodPut, "call-flows/"+callflow.ID, callflow); err != nil {
139+
if err := client.Request(&data, http.MethodPut, apiRoot+"/call-flows/"+callflow.ID, callflow); err != nil {
140140
return err
141141
}
142142
*callflow = data.Data[0]
@@ -145,7 +145,7 @@ func (callflow *CallFlow) Update(client *messagebird.Client) error {
145145

146146
// Delete deletes the CallFlow.
147147
func (callflow *CallFlow) Delete(client *messagebird.Client) error {
148-
return client.Request(nil, http.MethodDelete, "call-flows/"+callflow.ID, nil)
148+
return client.Request(nil, http.MethodDelete, apiRoot+"/call-flows/"+callflow.ID, nil)
149149
}
150150

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

voice/leg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,5 @@ func (leg *Leg) UnmarshalJSON(data []byte) error {
150150

151151
// Recordings retrieves the Recording objects associated with a leg.
152152
func (leg *Leg) Recordings(client *messagebird.Client) *Paginator {
153-
return newPaginator(client, fmt.Sprintf("calls/%s/legs/%s/recordings", leg.CallID, leg.ID), reflect.TypeOf(Recording{}))
153+
return newPaginator(client, fmt.Sprintf("%s/calls/%s/legs/%s/recordings", apiRoot, leg.CallID, leg.ID), reflect.TypeOf(Recording{}))
154154
}

voice/recording.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (rec *Recording) Transcriptions(client *messagebird.Client) *Paginator {
101101

102102
// DownloadFile streams the recorded WAV file.
103103
func (rec *Recording) DownloadFile(client *messagebird.Client) (io.ReadCloser, error) {
104-
req, err := http.NewRequest(http.MethodGet, messagebird.Endpoint+rec.links["file"], nil)
104+
req, err := http.NewRequest(http.MethodGet, apiRoot+rec.links["file"], nil)
105105
if err != nil {
106106
return nil, err
107107
}

voice/transcription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (trans *Transcription) UnmarshalJSON(data []byte) error {
7272
//
7373
// This is a plain text file.
7474
func (trans *Transcription) Contents(client *messagebird.Client) (string, error) {
75-
req, err := http.NewRequest(http.MethodGet, messagebird.Endpoint+trans.links["file"], nil)
75+
req, err := http.NewRequest(http.MethodGet, apiRoot+trans.links["file"], nil)
7676
if err != nil {
7777
return "", err
7878
}

voice/voice.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package voice
2+
3+
const apiRoot = "https://voice.messagebird.com"

voice/webhook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (wh *Webhook) UnmarshalJSON(data []byte) error {
6666

6767
// Webhooks returns a paginator over all webhooks.
6868
func Webhooks(client *messagebird.Client) *Paginator {
69-
return newPaginator(client, "webhooks/", reflect.TypeOf(Webhook{}))
69+
return newPaginator(client, apiRoot+"/webhooks/", reflect.TypeOf(Webhook{}))
7070
}
7171

7272
// CreateWebHook creates a new webhook the specified url that will be called
@@ -80,7 +80,7 @@ func CreateWebHook(client *messagebird.Client, url, token string) (*Webhook, err
8080
Token: token,
8181
}
8282
wh := &Webhook{}
83-
if err := client.Request(wh, http.MethodPost, "webhooks/", data); err != nil {
83+
if err := client.Request(wh, http.MethodPost, apiRoot+"/webhooks/", data); err != nil {
8484
return nil, err
8585
}
8686
return wh, nil
@@ -91,7 +91,7 @@ func (wh *Webhook) Update(client *messagebird.Client) error {
9191
var data struct {
9292
Data []Webhook `json:"data"`
9393
}
94-
if err := client.Request(&data, http.MethodPut, "webhooks/"+wh.ID, wh); err != nil {
94+
if err := client.Request(&data, http.MethodPut, apiRoot+"/webhooks/"+wh.ID, wh); err != nil {
9595
return err
9696
}
9797
*wh = data.Data[0]
@@ -100,5 +100,5 @@ func (wh *Webhook) Update(client *messagebird.Client) error {
100100

101101
// Delete deletes a webhook.
102102
func (wh *Webhook) Delete(client *messagebird.Client) error {
103-
return client.Request(nil, http.MethodDelete, "webhooks/"+wh.ID, nil)
103+
return client.Request(nil, http.MethodDelete, apiRoot+"/webhooks/"+wh.ID, nil)
104104
}

0 commit comments

Comments
 (0)