Skip to content

Commit

Permalink
Breaking change to CallResource struct (#2)
Browse files Browse the repository at this point in the history
Implemented Conference support
  • Loading branch information
jtwatson authored May 30, 2020
1 parent c14c123 commit ae45d0c
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 4 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 JP

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 65 additions & 1 deletion twilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *Client) SetMute(ctx context.Context, conferenceSid, callSid string, mut
return nil
}

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

return callResource, nil
}

// ConferenceResource retrieves conference details
func (c *Client) ConferenceResource(ctx context.Context, conferenceSid string) (*ConferenceResource, error) {
ctx, span := trace.StartSpan(ctx, "twilio.Client.ConferenceResource()")
defer span.End()

url := fmt.Sprintf("%s/Accounts/%s/Conferences/%s.json", baseURL, c.accountSid, conferenceSid)

req, err := c.newRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errors.WithMessage(err, "twilio.Client.ConferenceResource()")
}

res, err := c.httpClient.Do(req)
if err != nil {
return nil, errors.WithMessage(err, "twilio.Client.ConferenceResource(): http.Do(")
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, errors.WithMessage(decodeError(res.Body), "twilio.Client.ConferenceResource()")
}

conferencecResource := &ConferenceResource{}

if err := json.NewDecoder(res.Body).Decode(conferencecResource); err != nil {
return nil, errors.WithMessage(err, "twilio.Client.ConferenceResource(): json.Decoder.Decode()")
}

return conferencecResource, nil
}

// ParticipantResource retrieves participant details
func (c *Client) ParticipantResources(ctx context.Context, conferenceSid string) ([]ParticipantResource, error) {
ctx, span := trace.StartSpan(ctx, "twilio.Client.ParticipantResource()")
defer span.End()

url := fmt.Sprintf("%s/Accounts/%s/Conferences/%s/Participants.json", baseURL, c.accountSid, conferenceSid)

req, err := c.newRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errors.WithMessage(err, "twilio.Client.ParticipantResource()")
}

res, err := c.httpClient.Do(req)
if err != nil {
return nil, errors.WithMessage(err, "twilio.Client.ParticipantResource(): http.Do(")
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, errors.WithMessage(decodeError(res.Body), "twilio.Client.ParticipantResource()")
}

resource := &struct {
Participants []ParticipantResource
}{}

if err := json.NewDecoder(res.Body).Decode(resource); err != nil {
return nil, errors.WithMessage(err, "twilio.Client.ParticipantResource(): json.Decoder.Decode()")
}

return resource.Participants, nil
}
69 changes: 66 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package twilio

import (
"fmt"
"strings"
"time"
)

// CallResource holds the details of a call resouce
Expand All @@ -12,8 +14,8 @@ type CallResource struct {
AnsweredBy string `json:"answered_by,omitempty"`
APIVersion string `json:"api_version,omitempty"`
CallerName string `json:"caller_name,omitempty"`
DateCreated string `json:"date_created,omitempty"`
DateUpdated string `json:"date_updated,omitempty"`
DateCreated TwilioTime `json:"date_created,omitempty"`
DateUpdated TwilioTime `json:"date_updated,omitempty"`
Direction string `json:"direction,omitempty"`
Duration string `json:"duration,omitempty"`
EndTime string `json:"end_time,omitempty"`
Expand Down Expand Up @@ -44,7 +46,7 @@ type SubresourceUris struct {
Payments string `json:"payments,omitempty"`
}

// Call describes a outgoing call settings
// Call describes outgoing call settings
type Call struct {
AccountSid string `url:"AccountSid,omitempty"`
ApplicationSid string `url:"ApplicationSid,omitempty"`
Expand Down Expand Up @@ -81,13 +83,74 @@ type Call struct {
URL string `url:"Url,omitempty"`
}

// ConferenceResource holds the details of a conference
type ConferenceResource struct {
AccountSid string `json:"account_sid,omitempty"`
DateCreated TwilioTime `json:"date_created,omitempty"`
DateUpdated TwilioTime `json:"date_updated,omitempty"`
ApiVersion string `json:"api_version,omitempty"`
FriendlyName string `json:"friendly_name,omitempty"`
Region string `json:"region,omitempty"`
Sid string `json:"sid,omitempty"`
Status string `json:"status,omitempty"` // init, in-progress, or completed.
Uri string `json:"uri,omitempty"`
SubresourceUris map[string]string `json:"subresource_uris,omitempty"`
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.
CallSidEndingConference string `json:"call_sid_ending_conference,omitempty"`
}

// ParticipantResource holds the details of a participant
type ParticipantResource struct {
AccountSid string `json:"account_sid,omitempty"`
CallSid string `json:"call_sid,omitempty"`
CallSidToCoach string `json:"call_sid_to_coach,omitempty"`
Coaching bool `json:"coaching,omitempty"`
ConferenceSid string `json:"conference_sid,omitempty"`
DateCreated TwilioTime `json:"date_created,omitempty"`
DateUpdated TwilioTime `json:"date_updated,omitempty"`
EndConferenceOnExit bool `json:"end_conference_on_exit,omitempty"`
Muted bool `json:"muted,omitempty"`
Hold bool `json:"hold,omitempty"`
StartConferenceOnEnter bool `json:"start_conference_on_enter,omitempty"`
Status string `json:"status,omitempty"` // queued, connecting, ringing, connected, complete, or failed
Uri string `json:"uri,omitempty"`
}

// APIError holds the details of errors returned from twilio
type APIError struct {
Code int `json:"code"`
Message string `json:"message"`
MoreInfo string `json:"more_info"`
Status int `json:"status"`
}

// Error returns string representation of the error
func (a *APIError) Error() string {
return fmt.Sprintf("APIError: %s: more_info: %s", a.Message, a.MoreInfo)
}

// TwilioTime implements interfaces for json Marshalling and Unmarshalling
type TwilioTime struct {
time.Time
}

const ttLayout = "Mon, 02 Jan 2006 15:04:05 -0700" // 2006/01/02|15:04:05

// UnmarshalJSON implements the Unmarshaler interface
func (tt *TwilioTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
tt.Time = time.Time{}
return
}
tt.Time, err = time.Parse(ttLayout, s)
return
}

// MarshslJSON implements the Marshaler interface
func (tt *TwilioTime) MarshalJSON() ([]byte, error) {
if tt.Time.IsZero() {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", tt.Time.Format(ttLayout))), nil
}

0 comments on commit ae45d0c

Please sign in to comment.