From d8d6378371051c0ebf80e834068ae2f64de04578 Mon Sep 17 00:00:00 2001 From: Neil Garb Date: Thu, 28 Oct 2021 19:01:51 +0200 Subject: [PATCH] Add WithContext() methods to the API (#92) Co-authored-by: Neil Garb --- common.go | 7 +++++-- conference.go | 43 ++++++++++++++++++++++++++++++++++++------- fax.go | 33 +++++++++++++++++++++++++++------ gotwilio.go | 13 +++++++------ phonenumbers.go | 25 +++++++++++++++++++++---- proxy_participant.go | 37 +++++++++++++++++++++++++++++++------ proxy_service.go | 25 +++++++++++++++++++++---- proxy_session.go | 25 +++++++++++++++++++++---- queue.go | 7 ++++++- sms.go | 41 +++++++++++++++++++++++++++++++++-------- usage.go | 7 ++++++- video.go | 25 +++++++++++++++++++++---- voice.go | 29 +++++++++++++++++++++++------ 13 files changed, 258 insertions(+), 59 deletions(-) diff --git a/common.go b/common.go index 17e6525..7618bf1 100644 --- a/common.go +++ b/common.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -20,11 +21,13 @@ type ListResources struct { Messages []*SmsResponse `json:"messages"` t *Twilio + ctx context.Context } -func (t *Twilio) newListResources() *ListResources { +func (t *Twilio) newListResources(ctx context.Context) *ListResources { lr := new(ListResources) lr.t = t + lr.ctx = ctx return lr } @@ -33,7 +36,7 @@ func (l *ListResources) hasNext() bool { } func (l *ListResources) next() (*Exception, error) { - resp, err := l.t.get(l.NextPageUri) + resp, err := l.t.get(l.ctx, l.NextPageUri) if err != nil { return nil, err } diff --git a/conference.go b/conference.go index 4e110a3..75574a4 100644 --- a/conference.go +++ b/conference.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "fmt" "net/http" @@ -79,7 +80,11 @@ type ConferenceParticipantOptions struct { // GetConference fetches details for a single conference instance // https://www.twilio.com/docs/voice/api/conference-resource#fetch-a-conference-resource func (twilio *Twilio) GetConference(conferenceSid string) (*Conference, *Exception, error) { - res, err := twilio.get(twilio.buildUrl(fmt.Sprintf("Conferences/%s.json", conferenceSid))) + return twilio.GetConferenceWithContext(context.Background(), conferenceSid) +} + +func (twilio *Twilio) GetConferenceWithContext(ctx context.Context, conferenceSid string) (*Conference, *Exception, error) { + res, err := twilio.get(ctx, twilio.buildUrl(fmt.Sprintf("Conferences/%s.json", conferenceSid))) if err != nil { return nil, nil, err } @@ -101,12 +106,16 @@ func (twilio *Twilio) GetConference(conferenceSid string) (*Conference, *Excepti // UpdateConference to end it or play an announcement // https://www.twilio.com/docs/voice/api/conference-resource#update-a-conference-resource func (twilio *Twilio) UpdateConference(conferenceSid string, options *ConferenceOptions) (*Conference, *Exception, error) { + return twilio.UpdateConferenceWithContext(context.Background(), conferenceSid, options) +} + +func (twilio *Twilio) UpdateConferenceWithContext(ctx context.Context, conferenceSid string, options *ConferenceOptions) (*Conference, *Exception, error) { form, err := query.Values(options) if err != nil { return nil, nil, err } - res, err := twilio.post(form, twilio.buildUrl(fmt.Sprintf("Conferences/%s.json", conferenceSid))) + res, err := twilio.post(ctx, form, twilio.buildUrl(fmt.Sprintf("Conferences/%s.json", conferenceSid))) if err != nil { return nil, nil, err } @@ -131,7 +140,11 @@ type getParticipantsResponse struct { // GetConferenceParticipants fetches details for all conference participants resource // https://www.twilio.com/docs/voice/api/conference-participant-resource#read-multiple-participant-resources func (twilio *Twilio) GetConferenceParticipants(conferenceSid string) ([]*ConferenceParticipant, *Exception, error) { - res, err := twilio.get(twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants.json", conferenceSid))) + return twilio.GetConferenceParticipantsWithContext(context.Background(), conferenceSid) +} + +func (twilio *Twilio) GetConferenceParticipantsWithContext(ctx context.Context, conferenceSid string) ([]*ConferenceParticipant, *Exception, error) { + res, err := twilio.get(ctx, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants.json", conferenceSid))) if err != nil { return nil, nil, err } @@ -154,7 +167,11 @@ func (twilio *Twilio) GetConferenceParticipants(conferenceSid string) ([]*Confer // GetConferenceParticipant fetches details for a conference participant resource // https://www.twilio.com/docs/voice/api/conference-participant-resource#fetch-a-participant-resource func (twilio *Twilio) GetConferenceParticipant(conferenceSid, callSid string) (*ConferenceParticipant, *Exception, error) { - res, err := twilio.get(twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid))) + return twilio.GetConferenceParticipantWithContext(context.Background(), conferenceSid, callSid) +} + +func (twilio *Twilio) GetConferenceParticipantWithContext(ctx context.Context, conferenceSid, callSid string) (*ConferenceParticipant, *Exception, error) { + res, err := twilio.get(ctx, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid))) if err != nil { return nil, nil, err } @@ -176,12 +193,16 @@ func (twilio *Twilio) GetConferenceParticipant(conferenceSid, callSid string) (* // AddConferenceParticipant adds a Participant to a conference by dialing out a new call // https://www.twilio.com/docs/voice/api/conference-participant-resource#create-a-participant-agent-conference-only func (twilio *Twilio) AddConferenceParticipant(conferenceSid string, participant *ConferenceParticipantOptions) (*ConferenceParticipant, *Exception, error) { + return twilio.AddConferenceParticipantWithContext(context.Background(), conferenceSid, participant) +} + +func (twilio *Twilio) AddConferenceParticipantWithContext(ctx context.Context, conferenceSid string, participant *ConferenceParticipantOptions) (*ConferenceParticipant, *Exception, error) { form, err := query.Values(participant) if err != nil { return nil, nil, err } - res, err := twilio.post(form, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants.json", conferenceSid))) + res, err := twilio.post(ctx, form, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants.json", conferenceSid))) if err != nil { return nil, nil, err } @@ -202,12 +223,16 @@ func (twilio *Twilio) AddConferenceParticipant(conferenceSid string, participant // UpdateConferenceParticipant // https://www.twilio.com/docs/voice/api/conference-participant-resource#create-a-participant-agent-conference-only func (twilio *Twilio) UpdateConferenceParticipant(conferenceSid string, callSid string, participant *ConferenceParticipantOptions) (*ConferenceParticipant, *Exception, error) { + return twilio.UpdateConferenceParticipantWithContext(context.Background(), conferenceSid, callSid, participant) +} + +func (twilio *Twilio) UpdateConferenceParticipantWithContext(ctx context.Context, conferenceSid string, callSid string, participant *ConferenceParticipantOptions) (*ConferenceParticipant, *Exception, error) { form, err := query.Values(participant) if err != nil { return nil, nil, err } - res, err := twilio.post(form, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid))) + res, err := twilio.post(ctx, form, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid))) if err != nil { return nil, nil, err } @@ -227,7 +252,11 @@ func (twilio *Twilio) UpdateConferenceParticipant(conferenceSid string, callSid // DeleteConferenceParticipant func (twilio *Twilio) DeleteConferenceParticipant(conferenceSid string, callSid string) (*Exception, error) { - res, err := twilio.delete(twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid))) + return twilio.DeleteConferenceParticipantWithContext(context.Background(), conferenceSid, callSid) +} + +func (twilio *Twilio) DeleteConferenceParticipantWithContext(ctx context.Context, conferenceSid string, callSid string) (*Exception, error) { + res, err := twilio.delete(ctx, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid))) if err != nil { return nil, err } diff --git a/fax.go b/fax.go index cf17d81..fa00756 100644 --- a/fax.go +++ b/fax.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -56,7 +57,11 @@ type FaxResourcesList struct { } func (t *Twilio) CancelFax(faxSid string) (*Exception, error) { - resp, err := t.post(url.Values{"Status": []string{"cancelled"}}, "https://fax.twilio.com/v1/Faxes/"+faxSid) + return t.CancelFaxWithContext(context.Background(), faxSid) +} + +func (t *Twilio) CancelFaxWithContext(ctx context.Context, faxSid string) (*Exception, error) { + resp, err := t.post(ctx, url.Values{"Status": []string{"cancelled"}}, "https://fax.twilio.com/v1/Faxes/"+faxSid) if err != nil { return nil, err } @@ -73,7 +78,11 @@ func (t *Twilio) CancelFax(faxSid string) (*Exception, error) { } func (t *Twilio) DeleteFax(faxSid string) (*Exception, error) { - resp, err := t.delete("https://fax.twilio.com/v1/Faxes/" + faxSid) + return t.DeleteFaxWithContext(context.Background(), faxSid) +} + +func (t *Twilio) DeleteFaxWithContext(ctx context.Context, faxSid string) (*Exception, error) { + resp, err := t.delete(ctx, "https://fax.twilio.com/v1/Faxes/" + faxSid) if err != nil { return nil, err } @@ -90,7 +99,11 @@ func (t *Twilio) DeleteFax(faxSid string) (*Exception, error) { } func (t *Twilio) GetFax(faxSid string) (*FaxResource, *Exception, error) { - resp, err := t.get("https://fax.twilio.com/v1/Faxes/" + faxSid) + return t.GetFaxWithContext(context.Background(), faxSid) +} + +func (t *Twilio) GetFaxWithContext(ctx context.Context, faxSid string) (*FaxResource, *Exception, error) { + resp, err := t.get(ctx, "https://fax.twilio.com/v1/Faxes/" + faxSid) if err != nil { return nil, nil, err } @@ -116,6 +129,10 @@ func (t *Twilio) GetFax(faxSid string) (*FaxResource, *Exception, error) { // GetFaxes gets faxes for a Twilio account. // See https://www.twilio.com/docs/fax/api/faxes#fax-list-resource func (t *Twilio) GetFaxes(to, from, createdOnOrBefore, createdAfter string) ([]*FaxResource, *Exception, error) { + return t.GetFaxesWithContext(context.Background(), to, from, createdOnOrBefore, createdAfter) +} + +func (t *Twilio) GetFaxesWithContext(ctx context.Context, to, from, createdOnOrBefore, createdAfter string) ([]*FaxResource, *Exception, error) { values := url.Values{} if to != "" { values.Set("To", to) @@ -130,7 +147,7 @@ func (t *Twilio) GetFaxes(to, from, createdOnOrBefore, createdAfter string) ([]* values.Set("DateCreatedAfter", createdAfter) } - resp, err := t.get("https://fax.twilio.com/v1/Faxes") + resp, err := t.get(ctx, "https://fax.twilio.com/v1/Faxes") if err != nil { return nil, nil, err } @@ -144,7 +161,7 @@ func (t *Twilio) GetFaxes(to, from, createdOnOrBefore, createdAfter string) ([]* return nil, exc, err } - lr := t.newListResources() + lr := t.newListResources(ctx) if err := json.Unmarshal(respBody, lr); err != nil { return nil, nil, err } @@ -161,6 +178,10 @@ func (t *Twilio) GetFaxes(to, from, createdOnOrBefore, createdAfter string) ([]* // SendFax uses Twilio to send a fax. // See https://www.twilio.com/docs/fax/api/faxes#list-post for more information. func (t *Twilio) SendFax(to, from, mediaUrl, quality, statusCallback string, storeMedia bool) (*FaxResource, *Exception, error) { + return t.SendFaxWithContext(context.Background(), to, from, mediaUrl, quality, statusCallback, storeMedia) +} + +func (t *Twilio) SendFaxWithContext(ctx context.Context, to, from, mediaUrl, quality, statusCallback string, storeMedia bool) (*FaxResource, *Exception, error) { values := url.Values{} values.Set("To", to) values.Set("From", from) @@ -175,7 +196,7 @@ func (t *Twilio) SendFax(to, from, mediaUrl, quality, statusCallback string, sto values.Set("StoreMedia", "true") } - resp, err := t.post(values, "https://fax.twilio.com/v1/Faxes") + resp, err := t.post(ctx, values, "https://fax.twilio.com/v1/Faxes") if err != nil { return nil, nil, err } diff --git a/gotwilio.go b/gotwilio.go index 7facbe7..80ea878 100644 --- a/gotwilio.go +++ b/gotwilio.go @@ -4,6 +4,7 @@ package gotwilio import ( "encoding/json" "fmt" + "context" "net/http" "net/url" "path" @@ -111,8 +112,8 @@ func (twilio *Twilio) getBasicAuthCredentials() (string, string) { return twilio.AccountSid, twilio.AuthToken } -func (twilio *Twilio) post(formValues url.Values, twilioUrl string) (*http.Response, error) { - req, err := http.NewRequest("POST", twilioUrl, strings.NewReader(formValues.Encode())) +func (twilio *Twilio) post(ctx context.Context, formValues url.Values, twilioUrl string) (*http.Response, error) { + req, err := http.NewRequestWithContext(ctx, "POST", twilioUrl, strings.NewReader(formValues.Encode())) if err != nil { return nil, err } @@ -122,8 +123,8 @@ func (twilio *Twilio) post(formValues url.Values, twilioUrl string) (*http.Respo return twilio.do(req) } -func (twilio *Twilio) get(twilioUrl string) (*http.Response, error) { - req, err := http.NewRequest("GET", twilioUrl, nil) +func (twilio *Twilio) get(ctx context.Context, twilioUrl string) (*http.Response, error) { + req, err := http.NewRequestWithContext(ctx, "GET", twilioUrl, nil) if err != nil { return nil, err } @@ -132,8 +133,8 @@ func (twilio *Twilio) get(twilioUrl string) (*http.Response, error) { return twilio.do(req) } -func (twilio *Twilio) delete(twilioUrl string) (*http.Response, error) { - req, err := http.NewRequest("DELETE", twilioUrl, nil) +func (twilio *Twilio) delete(ctx context.Context, twilioUrl string) (*http.Response, error) { + req, err := http.NewRequestWithContext(ctx, "DELETE", twilioUrl, nil) if err != nil { return nil, err } diff --git a/phonenumbers.go b/phonenumbers.go index 87c63f6..30d87b6 100644 --- a/phonenumbers.go +++ b/phonenumbers.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "net/http" "net/url" @@ -164,6 +165,10 @@ type getIncomingPhoneNumbersResponse struct { // GetIncomingPhoneNumbers reads multiple IncomingPhoneNumbers from the Twilio REST API, with optional filtering // https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource#read-multiple-incomingphonenumber-resources func (twilio *Twilio) GetIncomingPhoneNumbers(request GetIncomingPhoneNumbersRequest) ([]*IncomingPhoneNumber, *Exception, error) { + return twilio.GetIncomingPhoneNumbersWithContext(context.Background(), request) +} + +func (twilio *Twilio) GetIncomingPhoneNumbersWithContext(ctx context.Context, request GetIncomingPhoneNumbersRequest) ([]*IncomingPhoneNumber, *Exception, error) { // convert request to url.Values for encoding into querystring form, err := query.Values(request) if err != nil { @@ -178,7 +183,7 @@ func (twilio *Twilio) GetIncomingPhoneNumbers(request GetIncomingPhoneNumbersReq } reqURL.RawQuery = form.Encode() - res, err := twilio.get(reqURL.String()) + res, err := twilio.get(ctx, reqURL.String()) if err != nil { return nil, nil, err } @@ -200,13 +205,17 @@ func (twilio *Twilio) GetIncomingPhoneNumbers(request GetIncomingPhoneNumbersReq // CreateIncomingPhoneNumber creates an IncomingPhoneNumber resource via the Twilio REST API. // https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource#create-an-incomingphonenumber-resource func (twilio *Twilio) CreateIncomingPhoneNumber(options IncomingPhoneNumber) (*IncomingPhoneNumber, *Exception, error) { + return twilio.CreateIncomingPhoneNumberWithContext(context.Background(), options) +} + +func (twilio *Twilio) CreateIncomingPhoneNumberWithContext(ctx context.Context, options IncomingPhoneNumber) (*IncomingPhoneNumber, *Exception, error) { // convert options to HTTP form form, err := query.Values(options) if err != nil { return nil, nil, err } - res, err := twilio.post(form, twilio.buildUrl("IncomingPhoneNumbers.json")) + res, err := twilio.post(ctx, form, twilio.buildUrl("IncomingPhoneNumbers.json")) if err != nil { return nil, nil, err } @@ -228,13 +237,17 @@ func (twilio *Twilio) CreateIncomingPhoneNumber(options IncomingPhoneNumber) (*I // UpdateIncomingPhoneNumber updates an IncomingPhoneNumber resource via the Twilio REST API. // https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource#update-an-incomingphonenumber-resource func (twilio *Twilio) UpdateIncomingPhoneNumber(sid string, options IncomingPhoneNumber) (*IncomingPhoneNumber, *Exception, error) { + return twilio.UpdateIncomingPhoneNumberWithContext(context.Background(), sid, options) +} + +func (twilio *Twilio) UpdateIncomingPhoneNumberWithContext(ctx context.Context, sid string, options IncomingPhoneNumber) (*IncomingPhoneNumber, *Exception, error) { // convert options to HTTP form form, err := query.Values(options) if err != nil { return nil, nil, err } - res, err := twilio.post(form, twilio.buildUrl("IncomingPhoneNumbers/"+sid+".json")) + res, err := twilio.post(ctx, form, twilio.buildUrl("IncomingPhoneNumbers/"+sid+".json")) if err != nil { return nil, nil, err } @@ -256,8 +269,12 @@ func (twilio *Twilio) UpdateIncomingPhoneNumber(sid string, options IncomingPhon // DeleteIncomingPhoneNumber deletes an IncomingPhoneNumber resource via the Twilio REST API. // https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource#delete-an-incomingphonenumber-resource func (twilio *Twilio) DeleteIncomingPhoneNumber(sid string) (*Exception, error) { + return twilio.DeleteIncomingPhoneNumberWithContext(context.Background(), sid) +} + +func (twilio *Twilio) DeleteIncomingPhoneNumberWithContext(ctx context.Context, sid string) (*Exception, error) { resourceName := sid + ".json" - res, err := twilio.delete(twilio.buildUrl("IncomingPhoneNumbers/" + resourceName)) + res, err := twilio.delete(ctx, twilio.buildUrl("IncomingPhoneNumbers/" + resourceName)) if err != nil { return nil, err } diff --git a/proxy_participant.go b/proxy_participant.go index f21d0a8..1ca729d 100644 --- a/proxy_participant.go +++ b/proxy_participant.go @@ -2,6 +2,7 @@ package gotwilio import ( + "context" "encoding/json" "errors" "fmt" @@ -87,10 +88,14 @@ type ProxyMessage struct { // AddParticipant adds Participant to Session func (session *ProxySession) AddParticipant(req ParticipantRequest) (response Participant, exception *Exception, err error) { + return session.AddParticipantWithContext(context.Background(), req) +} + +func (session *ProxySession) AddParticipantWithContext(ctx context.Context, req ParticipantRequest) (response Participant, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants") - res, err := session.twilio.post(participantFormValues(req), twilioUrl) + res, err := session.twilio.post(ctx, participantFormValues(req), twilioUrl) if err != nil { return response, exception, err } @@ -116,10 +121,14 @@ func (session *ProxySession) AddParticipant(req ParticipantRequest) (response Pa } func (session *ProxySession) ListParticipants() (response []Participant, exception *Exception, err error) { + return session.ListParticipantsWithContext(context.Background()) +} + +func (session *ProxySession) ListParticipantsWithContext(ctx context.Context) (response []Participant, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants") - res, err := session.twilio.get(twilioUrl) + res, err := session.twilio.get(ctx, twilioUrl) if err != nil { return response, exception, err } @@ -146,10 +155,14 @@ func (session *ProxySession) ListParticipants() (response []Participant, excepti } func (session *ProxySession) GetParticipant(participantID string) (response Participant, exception *Exception, err error) { + return session.GetParticipantWithContext(context.Background(), participantID) +} + +func (session *ProxySession) GetParticipantWithContext(ctx context.Context, participantID string) (response Participant, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants", participantID) - res, err := session.twilio.get(twilioUrl) + res, err := session.twilio.get(ctx, twilioUrl) if err != nil { return response, exception, err } @@ -177,10 +190,14 @@ func (session *ProxySession) GetParticipant(participantID string) (response Part // Participants cannot be changed once added. To add a new Participant, delete a Participant and add a new one. func (session *ProxySession) DeleteParticipant(participantID string) (exception *Exception, err error) { + return session.DeleteParticipantWithContext(context.Background(), participantID) +} + +func (session *ProxySession) DeleteParticipantWithContext(ctx context.Context, participantID string) (exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants", participantID) - res, err := session.twilio.delete(twilioUrl) + res, err := session.twilio.delete(ctx, twilioUrl) if err != nil { return exception, err } @@ -201,6 +218,10 @@ func (session *ProxySession) DeleteParticipant(participantID string) (exception //// INTERACTIONS func (session *ProxySession) CreateInteraction(participantSid string, msg ProxyMessage) (response Interaction, exception *Exception, err error) { + return session.CreateInteractionWithContext(context.Background(), participantSid, msg) +} + +func (session *ProxySession) CreateInteractionWithContext(ctx context.Context, participantSid string, msg ProxyMessage) (response Interaction, exception *Exception, err error) { if msg.Body == "" { return response, exception, errors.New("Message Body Must exist") } @@ -217,7 +238,7 @@ func (session *ProxySession) CreateInteraction(participantSid string, msg ProxyM formValues.Set("Callback", msg.Callback) } - res, err := session.twilio.post(formValues, twilioUrl) + res, err := session.twilio.post(ctx, formValues, twilioUrl) if err != nil { return response, exception, err } @@ -242,10 +263,14 @@ func (session *ProxySession) CreateInteraction(participantSid string, msg ProxyM } func (session *ProxySession) GetInteractions() (response InteractionList, exception *Exception, err error) { + return session.GetInteractionsWithContext(context.Background()) +} + +func (session *ProxySession) GetInteractionsWithContext(ctx context.Context) (response InteractionList, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Interactions") - res, err := session.twilio.get(twilioUrl) + res, err := session.twilio.get(ctx, twilioUrl) if err != nil { return response, exception, err } diff --git a/proxy_service.go b/proxy_service.go index 28e4a19..414ff1e 100644 --- a/proxy_service.go +++ b/proxy_service.go @@ -2,6 +2,7 @@ package gotwilio import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -45,10 +46,14 @@ type ProxyService struct { // Create a new Twilio Service func (twilio *Twilio) NewProxyService(service ProxyServiceRequest) (response *ProxyService, exception *Exception, err error) { + return twilio.NewProxyServiceWithContext(context.Background(), service) +} + +func (twilio *Twilio) NewProxyServiceWithContext(ctx context.Context, service ProxyServiceRequest) (response *ProxyService, exception *Exception, err error) { twilioUrl := ProxyBaseUrl + "/Services" - res, err := twilio.post(proxyServiceFormValues(service), twilioUrl) + res, err := twilio.post(ctx, proxyServiceFormValues(service), twilioUrl) if err != nil { return response, exception, err } @@ -75,10 +80,14 @@ func (twilio *Twilio) NewProxyService(service ProxyServiceRequest) (response *Pr } func (twilio *Twilio) GetProxyService(sid string) (response *ProxyService, exception *Exception, err error) { + return twilio.GetProxyServiceWithContext(context.Background(), sid) +} + +func (twilio *Twilio) GetProxyServiceWithContext(ctx context.Context, sid string) (response *ProxyService, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s", ProxyBaseUrl, "Services", sid) - res, err := twilio.get(twilioUrl) + res, err := twilio.get(ctx, twilioUrl) if err != nil { return response, exception, err } @@ -105,10 +114,14 @@ func (twilio *Twilio) GetProxyService(sid string) (response *ProxyService, excep } func (twilio *Twilio) UpdateProxyService(sid string, service ProxyServiceRequest) (response *ProxyService, exception *Exception, err error) { + return twilio.UpdateProxyServiceWithContext(context.Background(), sid, service) +} + +func (twilio *Twilio) UpdateProxyServiceWithContext(ctx context.Context, sid string, service ProxyServiceRequest) (response *ProxyService, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s", ProxyBaseUrl, "Services", sid) - res, err := twilio.post(proxyServiceFormValues(service), twilioUrl) + res, err := twilio.post(ctx, proxyServiceFormValues(service), twilioUrl) if err != nil { return response, exception, err } @@ -135,10 +148,14 @@ func (twilio *Twilio) UpdateProxyService(sid string, service ProxyServiceRequest } func (twilio *Twilio) DeleteProxyService(sid string) (exception *Exception, err error) { + return twilio.DeleteProxyServiceWithContext(context.Background(), sid) +} + +func (twilio *Twilio) DeleteProxyServiceWithContext(ctx context.Context, sid string) (exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s", ProxyBaseUrl, "Services", sid) - res, err := twilio.delete(twilioUrl) + res, err := twilio.delete(ctx, twilioUrl) if err != nil { return exception, err } diff --git a/proxy_session.go b/proxy_session.go index b61328a..ec389be 100644 --- a/proxy_session.go +++ b/proxy_session.go @@ -2,6 +2,7 @@ package gotwilio import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -47,10 +48,14 @@ type ProxySession struct { // Create a new Twilio Service func (twilio *Twilio) NewProxySession(serviceID string, req ProxySessionRequest) (response *ProxySession, exception *Exception, err error) { + return twilio.NewProxySessionWithContext(context.Background(), serviceID, req) +} + +func (twilio *Twilio) NewProxySessionWithContext(ctx context.Context, serviceID string, req ProxySessionRequest) (response *ProxySession, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions") - res, err := twilio.post(proxySessionFormValues(req), twilioUrl) + res, err := twilio.post(ctx, proxySessionFormValues(req), twilioUrl) if err != nil { return response, exception, err } @@ -80,10 +85,14 @@ func (twilio *Twilio) NewProxySession(serviceID string, req ProxySessionRequest) } func (twilio *Twilio) GetProxySession(serviceID, sessionID string) (response *ProxySession, exception *Exception, err error) { + return twilio.GetProxySessionWithContext(context.Background(), serviceID, sessionID) +} + +func (twilio *Twilio) GetProxySessionWithContext(ctx context.Context, serviceID, sessionID string) (response *ProxySession, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions", sessionID) - res, err := twilio.get(twilioUrl) + res, err := twilio.get(ctx, twilioUrl) if err != nil { return response, exception, err } @@ -113,10 +122,14 @@ func (twilio *Twilio) GetProxySession(serviceID, sessionID string) (response *Pr } func (twilio *Twilio) UpdateProxySession(serviceID, sessionID string, req ProxySessionRequest) (response *ProxySession, exception *Exception, err error) { + return twilio.UpdateProxySessionWithContext(context.Background(), serviceID, sessionID, req) +} + +func (twilio *Twilio) UpdateProxySessionWithContext(ctx context.Context, serviceID, sessionID string, req ProxySessionRequest) (response *ProxySession, exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions", sessionID) - res, err := twilio.post(proxySessionFormValues(req), twilioUrl) + res, err := twilio.post(ctx, proxySessionFormValues(req), twilioUrl) if err != nil { return response, exception, err } @@ -146,10 +159,14 @@ func (twilio *Twilio) UpdateProxySession(serviceID, sessionID string, req ProxyS } func (twilio *Twilio) DeleteProxySession(serviceID, sessionID string) (exception *Exception, err error) { + return twilio.DeleteProxySessionWithContext(context.Background(), serviceID, sessionID) +} + +func (twilio *Twilio) DeleteProxySessionWithContext(ctx context.Context, serviceID, sessionID string) (exception *Exception, err error) { twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", serviceID, "Sessions", sessionID) - res, err := twilio.delete(twilioUrl) + res, err := twilio.delete(ctx, twilioUrl) if err != nil { return exception, err } diff --git a/queue.go b/queue.go index 9fb82a9..96f00bc 100644 --- a/queue.go +++ b/queue.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "net/http" "net/url" @@ -17,6 +18,10 @@ type QueueResponse struct { } func (twilio *Twilio) CreateQueue(friendlyName string) (*QueueResponse, *Exception, error) { + return twilio.CreateQueueWithContext(context.Background(), friendlyName) +} + +func (twilio *Twilio) CreateQueueWithContext(ctx context.Context, friendlyName string) (*QueueResponse, *Exception, error) { var queueResponse *QueueResponse var exception *Exception twilioUrl := twilio.buildUrl("Queues.json") @@ -24,7 +29,7 @@ func (twilio *Twilio) CreateQueue(friendlyName string) (*QueueResponse, *Excepti formValues := url.Values{} formValues.Set("FriendlyName", friendlyName) - res, err := twilio.post(formValues, twilioUrl) + res, err := twilio.post(ctx, formValues, twilioUrl) if err != nil { return queueResponse, exception, err } diff --git a/sms.go b/sms.go index 9a28f26..2c66638 100644 --- a/sms.go +++ b/sms.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -67,15 +68,23 @@ func (twilio *Twilio) SendWhatsApp(from, to, body, statusCallback, applicationSi // SendWhatsAppMedia uses Twilio to send a WhatsApp message with Media enabled. // See https://www.twilio.com/docs/sms/whatsapp/tutorial/send-and-receive-media-messages-whatsapp-python func (twilio *Twilio) SendWhatsAppMedia(from, to, body string, mediaURL []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { + return twilio.SendWhatsAppMediaWithContext(context.Background(), from, to, body, mediaURL, statusCallback, applicationSid) +} + +func (twilio *Twilio) SendWhatsAppMediaWithContext(ctx context.Context, from, to, body string, mediaURL []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { formValues := initFormValues(whatsapp(to), body, mediaURL, statusCallback, applicationSid) formValues.Set("From", whatsapp(from)) - return twilio.sendMessage(formValues) + return twilio.sendMessage(ctx, formValues) } // SendSMS uses Twilio to send a text message. // See http://www.twilio.com/docs/api/rest/sending-sms for more information. func (twilio *Twilio) SendSMS(from, to, body, statusCallback, applicationSid string, opts ...*Option) (smsResponse *SmsResponse, exception *Exception, err error) { + return twilio.SendSMSWithContext(context.Background(), from, to, body, statusCallback, applicationSid, opts...) +} + +func (twilio *Twilio) SendSMSWithContext(ctx context.Context, from, to, body, statusCallback, applicationSid string, opts ...*Option) (smsResponse *SmsResponse, exception *Exception, err error) { formValues := initFormValues(to, body, nil, statusCallback, applicationSid) formValues.Set("From", from) @@ -85,16 +94,20 @@ func (twilio *Twilio) SendSMS(from, to, body, statusCallback, applicationSid str } } - smsResponse, exception, err = twilio.sendMessage(formValues) + smsResponse, exception, err = twilio.sendMessage(ctx, formValues) return } // GetSMS uses Twilio to get information about a text message. // See https://www.twilio.com/docs/api/rest/sms for more information. func (twilio *Twilio) GetSMS(sid string) (smsResponse *SmsResponse, exception *Exception, err error) { + return twilio.GetSMSWithContext(context.Background(), sid) +} + +func (twilio *Twilio) GetSMSWithContext(ctx context.Context, sid string) (smsResponse *SmsResponse, exception *Exception, err error) { twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/SMS/Messages/" + sid + ".json" - res, err := twilio.get(twilioUrl) + res, err := twilio.get(ctx, twilioUrl) if err != nil { return smsResponse, exception, err } @@ -122,37 +135,49 @@ func (twilio *Twilio) GetSMS(sid string) (smsResponse *SmsResponse, exception *E // SendSMSWithCopilot uses Twilio Copilot to send a text message. // See https://www.twilio.com/docs/api/rest/sending-messages-copilot func (twilio *Twilio) SendSMSWithCopilot(messagingServiceSid, to, body, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { + return twilio.SendSMSWithCopilotWithContext(context.Background(), messagingServiceSid, to, body, statusCallback, applicationSid) +} + +func (twilio *Twilio) SendSMSWithCopilotWithContext(ctx context.Context, messagingServiceSid, to, body, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { formValues := initFormValues(to, body, nil, statusCallback, applicationSid) formValues.Set("MessagingServiceSid", messagingServiceSid) - smsResponse, exception, err = twilio.sendMessage(formValues) + smsResponse, exception, err = twilio.sendMessage(ctx, formValues) return } // SendMMS uses Twilio to send a multimedia message. func (twilio *Twilio) SendMMS(from, to, body string, mediaUrl []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { + return twilio.SendMMSWithContext(context.Background(), from, to, body, mediaUrl, statusCallback, applicationSid) +} + +func (twilio *Twilio) SendMMSWithContext(ctx context.Context, from, to, body string, mediaUrl []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { formValues := initFormValues(to, body, mediaUrl, statusCallback, applicationSid) formValues.Set("From", from) - smsResponse, exception, err = twilio.sendMessage(formValues) + smsResponse, exception, err = twilio.sendMessage(ctx, formValues) return } // SendMMSWithCopilot uses Twilio Copilot to send a multimedia message. // See https://www.twilio.com/docs/api/rest/sending-messages-copilot func (twilio *Twilio) SendMMSWithCopilot(messagingServiceSid, to, body string, mediaUrl []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { + return twilio.SendMMSWithCopilotWithContext(context.Background(), messagingServiceSid, to, body, mediaUrl, statusCallback, applicationSid) +} + +func (twilio *Twilio) SendMMSWithCopilotWithContext(ctx context.Context, messagingServiceSid, to, body string, mediaUrl []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) { formValues := initFormValues(to, body, mediaUrl, statusCallback, applicationSid) formValues.Set("MessagingServiceSid", messagingServiceSid) - smsResponse, exception, err = twilio.sendMessage(formValues) + smsResponse, exception, err = twilio.sendMessage(ctx, formValues) return } // Core method to send message -func (twilio *Twilio) sendMessage(formValues url.Values) (smsResponse *SmsResponse, exception *Exception, err error) { +func (twilio *Twilio) sendMessage(ctx context.Context, formValues url.Values) (smsResponse *SmsResponse, exception *Exception, err error) { twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/Messages.json" - res, err := twilio.post(formValues, twilioUrl) + res, err := twilio.post(ctx, formValues, twilioUrl) if err != nil { return smsResponse, exception, err } diff --git a/usage.go b/usage.go index b8ae689..9136a54 100644 --- a/usage.go +++ b/usage.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -44,6 +45,10 @@ type UsageResponse struct { } func (twilio *Twilio) GetUsage(category, startDate, endDate string, includeSubaccounts bool) (*UsageResponse, *Exception, error) { + return twilio.GetUsageWithContext(context.Background(), category, startDate, endDate, includeSubaccounts) +} + +func (twilio *Twilio) GetUsageWithContext(ctx context.Context, category, startDate, endDate string, includeSubaccounts bool) (*UsageResponse, *Exception, error) { formValues := url.Values{} formValues.Set("category", category) formValues.Set("start_date", startDate) @@ -54,7 +59,7 @@ func (twilio *Twilio) GetUsage(category, startDate, endDate string, includeSubac var exception *Exception twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/Usage/Records.json" - res, err := twilio.get(twilioUrl + "?" + formValues.Encode()) + res, err := twilio.get(ctx, twilioUrl + "?" + formValues.Encode()) if err != nil { return nil, nil, err } diff --git a/video.go b/video.go index f639f1c..d33bb8d 100644 --- a/video.go +++ b/video.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -128,10 +129,14 @@ type ListVideoRoomOptions struct { // See https://www.twilio.com/docs/video/api/rooms-resource // for more information. func (twilio *Twilio) CreateVideoRoom(options *createRoomOptions) (videoResponse *VideoResponse, exception *Exception, err error) { + return twilio.CreateVideoRoomWithContext(context.Background(), options) +} + +func (twilio *Twilio) CreateVideoRoomWithContext(ctx context.Context, options *createRoomOptions) (videoResponse *VideoResponse, exception *Exception, err error) { twilioUrl := twilio.VideoUrl + "/v1/Rooms" formValues := createRoomOptionsToFormValues(options) - res, err := twilio.post(formValues, twilioUrl) + res, err := twilio.post(ctx, formValues, twilioUrl) if err != nil { return videoResponse, exception, err } @@ -165,6 +170,10 @@ func (twilio *Twilio) CreateVideoRoom(options *createRoomOptions) (videoResponse // See https://www.twilio.com/docs/video/api/rooms-resource // for more information. func (twilio *Twilio) ListVideoRooms(options *ListVideoRoomOptions) (videoResponse *ListVideoReponse, exception *Exception, err error) { + return twilio.ListVideoRoomsWithContext(context.Background(), options) +} + +func (twilio *Twilio) ListVideoRoomsWithContext(ctx context.Context, options *ListVideoRoomOptions) (videoResponse *ListVideoReponse, exception *Exception, err error) { q := &url.Values{} if !options.DateCreatedAfter.Equal(time.Time{}) { q.Set("DateCreatedAfter", options.DateCreatedAfter.Format(time.RFC3339)) @@ -181,7 +190,7 @@ func (twilio *Twilio) ListVideoRooms(options *ListVideoRoomOptions) (videoRespon twilioUrl := twilio.VideoUrl + "/v1/Rooms?" + q.Encode() - res, err := twilio.get(twilioUrl) + res, err := twilio.get(ctx, twilioUrl) if err != nil { return videoResponse, exception, err } @@ -211,9 +220,13 @@ func (twilio *Twilio) ListVideoRooms(options *ListVideoRoomOptions) (videoRespon // See https://www.twilio.com/docs/video/api/rooms-resource // for more information. func (twilio *Twilio) GetVideoRoom(nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) { + return twilio.GetVideoRoomWithContext(context.Background(), nameOrSid) +} + +func (twilio *Twilio) GetVideoRoomWithContext(ctx context.Context, nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) { twilioUrl := twilio.VideoUrl + "/v1/Rooms/" + nameOrSid - res, err := twilio.get(twilioUrl) + res, err := twilio.get(ctx, twilioUrl) if err != nil { return videoResponse, exception, err } @@ -243,11 +256,15 @@ func (twilio *Twilio) GetVideoRoom(nameOrSid string) (videoResponse *VideoRespon // See https://www.twilio.com/docs/video/api/rooms-resource // for more information. func (twilio *Twilio) EndVideoRoom(nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) { + return twilio.EndVideoRoomWithContext(context.Background(), nameOrSid) +} + +func (twilio *Twilio) EndVideoRoomWithContext(ctx context.Context, nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) { twilioUrl := twilio.VideoUrl + "/v1/Rooms/" + nameOrSid formValues := url.Values{} formValues.Set("Status", fmt.Sprintf("%s", Completed)) - res, err := twilio.post(formValues, twilioUrl) + res, err := twilio.post(ctx, formValues, twilioUrl) if err != nil { return videoResponse, exception, err } diff --git a/voice.go b/voice.go index 76f0a15..75194c9 100644 --- a/voice.go +++ b/voice.go @@ -1,6 +1,7 @@ package gotwilio import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -98,11 +99,15 @@ func NewCallbackParameters(url string) *CallbackParameters { // GetCall uses Twilio to get information about a voice call. // See https://www.twilio.com/docs/voice/api/call func (twilio *Twilio) GetCall(sid string) (*VoiceResponse, *Exception, error) { + return twilio.GetCallWithContext(context.Background(), sid) +} + +func (twilio *Twilio) GetCallWithContext(ctx context.Context, sid string) (*VoiceResponse, *Exception, error) { var voiceResponse *VoiceResponse var exception *Exception twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/Calls/" + sid + ".json" - res, err := twilio.get(twilioUrl) + res, err := twilio.get(ctx, twilioUrl) if err != nil { return nil, nil, err } @@ -125,6 +130,10 @@ func (twilio *Twilio) GetCall(sid string) (*VoiceResponse, *Exception, error) { // Place a voice call with a list of callbacks specified. func (twilio *Twilio) CallWithUrlCallbacks(from, to string, callbackParameters *CallbackParameters) (*VoiceResponse, *Exception, error) { + return twilio.CallWithUrlCallbacksWithContext(context.Background(), from, to, callbackParameters) +} + +func (twilio *Twilio) CallWithUrlCallbacksWithContext(ctx context.Context, from, to string, callbackParameters *CallbackParameters) (*VoiceResponse, *Exception, error) { formValues := url.Values{} formValues.Set("From", from) formValues.Set("To", to) @@ -210,31 +219,39 @@ func (twilio *Twilio) CallWithUrlCallbacks(from, to string, callbackParameters * } } - return twilio.voicePost("Calls.json", formValues) + return twilio.voicePost(ctx, "Calls.json", formValues) } // Place a voice call with an ApplicationSid specified. func (twilio *Twilio) CallWithApplicationCallbacks(from, to, applicationSid string) (*VoiceResponse, *Exception, error) { + return twilio.CallWithApplicationCallbacksWithContext(context.Background(), from, to, applicationSid) +} + +func (twilio *Twilio) CallWithApplicationCallbacksWithContext(ctx context.Context, from, to, applicationSid string) (*VoiceResponse, *Exception, error) { formValues := url.Values{} formValues.Set("From", from) formValues.Set("To", to) formValues.Set("ApplicationSid", applicationSid) - return twilio.voicePost("Calls.json", formValues) + return twilio.voicePost(ctx, "Calls.json", formValues) } // Update an existing call func (twilio *Twilio) CallUpdate(callSid string, formValues url.Values) (*VoiceResponse, *Exception, error) { - return twilio.voicePost("Calls/"+callSid+".json", formValues) + return twilio.CallUpdateWithContext(context.Background(), callSid, formValues) +} + +func (twilio *Twilio) CallUpdateWithContext(ctx context.Context, callSid string, formValues url.Values) (*VoiceResponse, *Exception, error) { + return twilio.voicePost(ctx, "Calls/"+callSid+".json", formValues) } // This is a private method that has the common bits for placing or updating a voice call. -func (twilio *Twilio) voicePost(resourcePath string, formValues url.Values) (*VoiceResponse, *Exception, error) { +func (twilio *Twilio) voicePost(ctx context.Context, resourcePath string, formValues url.Values) (*VoiceResponse, *Exception, error) { var voiceResponse *VoiceResponse var exception *Exception twilioUrl := twilio.buildUrl(resourcePath) - res, err := twilio.post(formValues, twilioUrl) + res, err := twilio.post(ctx, formValues, twilioUrl) if err != nil { return voiceResponse, exception, err }