@@ -2,12 +2,14 @@ package twilio
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"io"
7
8
"net/http"
8
9
"net/url"
9
10
"strings"
10
11
12
+ "github.com/google/go-querystring/query"
11
13
"github.com/pkg/errors"
12
14
"go.opencensus.io/trace"
13
15
)
@@ -62,7 +64,7 @@ func (c *Client) DisconnectCall(ctx context.Context, callSid string) error {
62
64
defer res .Body .Close ()
63
65
64
66
if res .StatusCode != http .StatusOK {
65
- return fmt . Errorf ( "twilio.Client.DisconnectCall(): expected status code 200, got %d" , res . StatusCode )
67
+ return errors . WithMessage ( decodeError ( res . Body ), "twilio.Client.DisconnectCall()" )
66
68
}
67
69
68
70
return nil
@@ -91,45 +93,76 @@ func (c *Client) SetMute(ctx context.Context, conferenceSid, callSid string, mut
91
93
defer res .Body .Close ()
92
94
93
95
if res .StatusCode != http .StatusOK {
94
- return fmt . Errorf ( "twilio.Client.SetMute(): expected status code 200, got %d" , res . StatusCode )
96
+ return errors . WithMessage ( decodeError ( res . Body ), "twilio.Client.SetMute()" )
95
97
}
96
98
97
99
return nil
98
100
}
99
101
100
- // CallResource recieves call resource details
101
- func (c * Client ) CallResource (ctx context.Context , callSid string ) {
102
+ // CallResource receives call resource details
103
+ func (c * Client ) CallResource (ctx context.Context , callSid string ) ( * CallResource , error ) {
102
104
ctx , span := trace .StartSpan (ctx , "twilio.Client.CallResource()" )
103
105
defer span .End ()
104
106
105
- }
107
+ url := fmt .Sprintf ("%s/Accounts/%s/Calls/%s.json" , baseURL , c .accountSid , callSid )
108
+
109
+ req , err := c .newRequest (ctx , http .MethodGet , url , nil )
110
+ if err != nil {
111
+ return nil , errors .WithMessage (err , "twilio.Client.CallResource()" )
112
+ }
113
+
114
+ res , err := c .httpClient .Do (req )
115
+ if err != nil {
116
+ return nil , errors .WithMessage (err , "twilio.Client.CallResource(): http.Do(" )
117
+ }
118
+ defer res .Body .Close ()
106
119
107
- // CallResource holds the details of a call resouce
108
- type CallResource struct {
109
- Sid string `json:"sid,omitempty"`
110
- DateCreated string `json:"date_created,omitempty"`
111
- DateUpdated string `json:"date_updated,omitempty"`
112
- ParentCallSid string `json:"parent_call_sid,omitempty"`
113
- AccountSid string `json:"account_sid,omitempty"`
114
- To string `json:"to,omitempty"`
115
- From string `json:"from,omitempty"`
116
- PhoneNumberSid string `json:"phone_number_sid,omitempty"`
117
- Status string `json:"status,omitempty"`
118
- StartTime string `json:"start_time,omitempty"`
119
- EndTime string `json:"end_time,omitempty"`
120
- Duration string `json:"duration,omitempty"`
121
- Price string `json:"price,omitempty"`
122
- Direction string `json:"direction,omitempty"`
123
- AnsweredBy string `json:"answered_by,omitempty"`
124
- APIVersion string `json:"api_version,omitempty"`
125
- ForwardedFrom string `json:"forwarded_from,omitempty"`
126
- CallerName string `json:"caller_name,omitempty"`
127
- URI string `json:"uri,omitempty"`
128
- SubresourceUris SubresourceUris `json:"subresource_uris,omitempty"`
120
+ if res .StatusCode != http .StatusOK {
121
+ return nil , errors .WithMessage (decodeError (res .Body ), "twilio.Client.CallResource()" )
122
+ }
123
+
124
+ callResource := & CallResource {}
125
+
126
+ if err := json .NewDecoder (res .Body ).Decode (callResource ); err != nil {
127
+ return nil , errors .WithMessage (err , "twilio.Client.CallResource(): json.Decoder.Decode()" )
128
+ }
129
+
130
+ return callResource , nil
129
131
}
130
132
131
- // SubresourceUris holds details for subresource uri's
132
- type SubresourceUris struct {
133
- Notifications string `json:"notifications,omitempty"`
134
- Recordings string `json:"recordings,omitempty"`
133
+ // Call creates an outbound call returning the resulting CallResource
134
+ func (c * Client ) Call (ctx context.Context , call * Call ) (* CallResource , error ) {
135
+ ctx , span := trace .StartSpan (ctx , "twilio.Client.Call()" )
136
+ defer span .End ()
137
+
138
+ params , err := query .Values (call )
139
+ if err != nil {
140
+ return nil , errors .WithMessage (err , "twilio.Client.Call(): query.Values()" )
141
+ }
142
+
143
+ url := fmt .Sprintf ("%s/Accounts/%s/Calls.json" , baseURL , c .accountSid )
144
+ body := strings .NewReader (params .Encode ())
145
+
146
+ req , err := c .newRequest (ctx , http .MethodPost , url , body )
147
+ if err != nil {
148
+ return nil , errors .WithMessage (err , "twilio.Client.Call()" )
149
+ }
150
+
151
+ res , err := c .httpClient .Do (req )
152
+ if err != nil {
153
+ return nil , errors .WithMessage (err , "twilio.Client.Call(): http.Do(" )
154
+ }
155
+ defer res .Body .Close ()
156
+
157
+ if res .StatusCode != http .StatusCreated {
158
+ return nil , errors .WithMessage (decodeError (res .Body ), "twilio.Client.Call()" )
159
+ }
160
+
161
+ callResource := & CallResource {}
162
+
163
+ if err := json .NewDecoder (res .Body ).Decode (callResource ); err != nil {
164
+ return nil , errors .WithMessage (err , "twilio.Client.Call(): json.Decoder.Decode()" )
165
+ }
166
+
167
+ return callResource , nil
135
168
}
0 commit comments