Skip to content

Commit ae45d0c

Browse files
authored
Breaking change to CallResource struct (#2)
Implemented Conference support
1 parent c14c123 commit ae45d0c

File tree

3 files changed

+152
-4
lines changed

3 files changed

+152
-4
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 JP
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

twilio.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (c *Client) SetMute(ctx context.Context, conferenceSid, callSid string, mut
9999
return nil
100100
}
101101

102-
// CallResource receives call resource details
102+
// CallResource retrieves call details
103103
func (c *Client) CallResource(ctx context.Context, callSid string) (*CallResource, error) {
104104
ctx, span := trace.StartSpan(ctx, "twilio.Client.CallResource()")
105105
defer span.End()
@@ -166,3 +166,67 @@ func (c *Client) Call(ctx context.Context, call *Call) (*CallResource, error) {
166166

167167
return callResource, nil
168168
}
169+
170+
// ConferenceResource retrieves conference details
171+
func (c *Client) ConferenceResource(ctx context.Context, conferenceSid string) (*ConferenceResource, error) {
172+
ctx, span := trace.StartSpan(ctx, "twilio.Client.ConferenceResource()")
173+
defer span.End()
174+
175+
url := fmt.Sprintf("%s/Accounts/%s/Conferences/%s.json", baseURL, c.accountSid, conferenceSid)
176+
177+
req, err := c.newRequest(ctx, http.MethodGet, url, nil)
178+
if err != nil {
179+
return nil, errors.WithMessage(err, "twilio.Client.ConferenceResource()")
180+
}
181+
182+
res, err := c.httpClient.Do(req)
183+
if err != nil {
184+
return nil, errors.WithMessage(err, "twilio.Client.ConferenceResource(): http.Do(")
185+
}
186+
defer res.Body.Close()
187+
188+
if res.StatusCode != http.StatusOK {
189+
return nil, errors.WithMessage(decodeError(res.Body), "twilio.Client.ConferenceResource()")
190+
}
191+
192+
conferencecResource := &ConferenceResource{}
193+
194+
if err := json.NewDecoder(res.Body).Decode(conferencecResource); err != nil {
195+
return nil, errors.WithMessage(err, "twilio.Client.ConferenceResource(): json.Decoder.Decode()")
196+
}
197+
198+
return conferencecResource, nil
199+
}
200+
201+
// ParticipantResource retrieves participant details
202+
func (c *Client) ParticipantResources(ctx context.Context, conferenceSid string) ([]ParticipantResource, error) {
203+
ctx, span := trace.StartSpan(ctx, "twilio.Client.ParticipantResource()")
204+
defer span.End()
205+
206+
url := fmt.Sprintf("%s/Accounts/%s/Conferences/%s/Participants.json", baseURL, c.accountSid, conferenceSid)
207+
208+
req, err := c.newRequest(ctx, http.MethodGet, url, nil)
209+
if err != nil {
210+
return nil, errors.WithMessage(err, "twilio.Client.ParticipantResource()")
211+
}
212+
213+
res, err := c.httpClient.Do(req)
214+
if err != nil {
215+
return nil, errors.WithMessage(err, "twilio.Client.ParticipantResource(): http.Do(")
216+
}
217+
defer res.Body.Close()
218+
219+
if res.StatusCode != http.StatusOK {
220+
return nil, errors.WithMessage(decodeError(res.Body), "twilio.Client.ParticipantResource()")
221+
}
222+
223+
resource := &struct {
224+
Participants []ParticipantResource
225+
}{}
226+
227+
if err := json.NewDecoder(res.Body).Decode(resource); err != nil {
228+
return nil, errors.WithMessage(err, "twilio.Client.ParticipantResource(): json.Decoder.Decode()")
229+
}
230+
231+
return resource.Participants, nil
232+
}

types.go

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package twilio
22

33
import (
44
"fmt"
5+
"strings"
6+
"time"
57
)
68

79
// CallResource holds the details of a call resouce
@@ -12,8 +14,8 @@ type CallResource struct {
1214
AnsweredBy string `json:"answered_by,omitempty"`
1315
APIVersion string `json:"api_version,omitempty"`
1416
CallerName string `json:"caller_name,omitempty"`
15-
DateCreated string `json:"date_created,omitempty"`
16-
DateUpdated string `json:"date_updated,omitempty"`
17+
DateCreated TwilioTime `json:"date_created,omitempty"`
18+
DateUpdated TwilioTime `json:"date_updated,omitempty"`
1719
Direction string `json:"direction,omitempty"`
1820
Duration string `json:"duration,omitempty"`
1921
EndTime string `json:"end_time,omitempty"`
@@ -44,7 +46,7 @@ type SubresourceUris struct {
4446
Payments string `json:"payments,omitempty"`
4547
}
4648

47-
// Call describes a outgoing call settings
49+
// Call describes outgoing call settings
4850
type Call struct {
4951
AccountSid string `url:"AccountSid,omitempty"`
5052
ApplicationSid string `url:"ApplicationSid,omitempty"`
@@ -81,13 +83,74 @@ type Call struct {
8183
URL string `url:"Url,omitempty"`
8284
}
8385

86+
// ConferenceResource holds the details of a conference
87+
type ConferenceResource struct {
88+
AccountSid string `json:"account_sid,omitempty"`
89+
DateCreated TwilioTime `json:"date_created,omitempty"`
90+
DateUpdated TwilioTime `json:"date_updated,omitempty"`
91+
ApiVersion string `json:"api_version,omitempty"`
92+
FriendlyName string `json:"friendly_name,omitempty"`
93+
Region string `json:"region,omitempty"`
94+
Sid string `json:"sid,omitempty"`
95+
Status string `json:"status,omitempty"` // init, in-progress, or completed.
96+
Uri string `json:"uri,omitempty"`
97+
SubresourceUris map[string]string `json:"subresource_uris,omitempty"`
98+
ReasonConferenceEnded string `json:"reason_conference_ended,omitempty"` // conference-ended-via-api, participant-with-end-conference-on-exit-left, participant-with-end-conference-on-exit-kicked, last-participant-kicked, or last-participant-left.
99+
CallSidEndingConference string `json:"call_sid_ending_conference,omitempty"`
100+
}
101+
102+
// ParticipantResource holds the details of a participant
103+
type ParticipantResource struct {
104+
AccountSid string `json:"account_sid,omitempty"`
105+
CallSid string `json:"call_sid,omitempty"`
106+
CallSidToCoach string `json:"call_sid_to_coach,omitempty"`
107+
Coaching bool `json:"coaching,omitempty"`
108+
ConferenceSid string `json:"conference_sid,omitempty"`
109+
DateCreated TwilioTime `json:"date_created,omitempty"`
110+
DateUpdated TwilioTime `json:"date_updated,omitempty"`
111+
EndConferenceOnExit bool `json:"end_conference_on_exit,omitempty"`
112+
Muted bool `json:"muted,omitempty"`
113+
Hold bool `json:"hold,omitempty"`
114+
StartConferenceOnEnter bool `json:"start_conference_on_enter,omitempty"`
115+
Status string `json:"status,omitempty"` // queued, connecting, ringing, connected, complete, or failed
116+
Uri string `json:"uri,omitempty"`
117+
}
118+
119+
// APIError holds the details of errors returned from twilio
84120
type APIError struct {
85121
Code int `json:"code"`
86122
Message string `json:"message"`
87123
MoreInfo string `json:"more_info"`
88124
Status int `json:"status"`
89125
}
90126

127+
// Error returns string representation of the error
91128
func (a *APIError) Error() string {
92129
return fmt.Sprintf("APIError: %s: more_info: %s", a.Message, a.MoreInfo)
93130
}
131+
132+
// TwilioTime implements interfaces for json Marshalling and Unmarshalling
133+
type TwilioTime struct {
134+
time.Time
135+
}
136+
137+
const ttLayout = "Mon, 02 Jan 2006 15:04:05 -0700" // 2006/01/02|15:04:05
138+
139+
// UnmarshalJSON implements the Unmarshaler interface
140+
func (tt *TwilioTime) UnmarshalJSON(b []byte) (err error) {
141+
s := strings.Trim(string(b), "\"")
142+
if s == "null" {
143+
tt.Time = time.Time{}
144+
return
145+
}
146+
tt.Time, err = time.Parse(ttLayout, s)
147+
return
148+
}
149+
150+
// MarshslJSON implements the Marshaler interface
151+
func (tt *TwilioTime) MarshalJSON() ([]byte, error) {
152+
if tt.Time.IsZero() {
153+
return []byte("null"), nil
154+
}
155+
return []byte(fmt.Sprintf("\"%s\"", tt.Time.Format(ttLayout))), nil
156+
}

0 commit comments

Comments
 (0)