Skip to content

Commit

Permalink
Fix issue #2 by switching to JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Freiberg committed Nov 8, 2013
1 parent 0fda6c6 commit a0ce1cb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 51 deletions.
10 changes: 4 additions & 6 deletions gotwilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package gotwilio

import (
"encoding/xml"
"net/http"
"net/url"
"strings"
Expand All @@ -18,11 +17,10 @@ type Twilio struct {

// Exception is a representation of a twilio exception.
type Exception struct {
XMLName xml.Name `xml:"TwilioResponse"`
Status int `xml:"RestException>Status"` // HTTP specific error code
Message string `xml:"RestException>Message"` // HTTP error message
Code int `xml:"RestException>Code"` // Twilio specific error code
MoreInfo string `xml:"RestException>MoreInfo"` // Additional info from Twilio
Status int `json:"status"` // HTTP specific error code
Message string `json:"message"` // HTTP error message
Code int `json:"code"` // Twilio specific error code
MoreInfo string `json:"more_info"` // Additional info from Twilio
}

// Create a new Twilio struct.
Expand Down
35 changes: 17 additions & 18 deletions sms.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gotwilio

import (
"encoding/xml"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -10,20 +10,19 @@ import (

// SmsResponse is returned after a text/sms message is posted to Twilio
type SmsResponse struct {
XMLName xml.Name `xml:"TwilioResponse"`
Sid string `xml:"SMSMessage>Sid"`
DateCreated string `xml:"SMSMessage>DateCreated"`
DateUpdate string `xml:"SMSMessage>DateUpdated"`
DateSent string `xml:"SMSMessage>DateSent"`
AccountSid string `xml:"SMSMessage>AccountSid"`
To string `xml:"SMSMessage>To"`
From string `xml:"SMSMessage>From"`
Body string `xml:"SMSMessage>Body"`
Status string `xml:"SMSMessage>Status"`
Direction string `xml:"SMSMessage>Direction"`
ApiVersion string `xml:"SMSMessage>ApiVersion"`
Price float32 `xml:"SMSMessage>Price"`
Url string `xml:"SMSMessage>Uri"`
Sid string `json:"sid"`
DateCreated string `json:"date_created"`
DateUpdate string `json:"date_updated"`
DateSent string `json:"date_sent"`
AccountSid string `json:"account_sid"`
To string `json:"to"`
From string `json:"from"`
Body string `json:"body"`
Status string `json:"status"`
Direction string `json:"direction"`
ApiVersion string `json:"api_version"`
Price *float32 `json:"price,omitempty"`
Url string `json:"uri"`
}

// Returns SmsResponse.DateCreated as a time.Time object
Expand All @@ -49,7 +48,7 @@ func (sms *SmsResponse) DateSentAsTime() (time.Time, error) {
func (twilio *Twilio) SendSMS(from, to, body, statusCallback, applicationSid string) (*SmsResponse, *Exception, error) {
var smsResponse *SmsResponse
var exception *Exception
twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/SMS/Messages"
twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/SMS/Messages.json"

formValues := url.Values{}
formValues.Set("From", from)
Expand All @@ -75,14 +74,14 @@ func (twilio *Twilio) SendSMS(from, to, body, statusCallback, applicationSid str

if res.StatusCode != http.StatusCreated {
exception = new(Exception)
err = xml.Unmarshal(responseBody, exception)
err = json.Unmarshal(responseBody, exception)

// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return smsResponse, exception, err
}

smsResponse = new(SmsResponse)
err = xml.Unmarshal(responseBody, smsResponse)
err = json.Unmarshal(responseBody, smsResponse)
return smsResponse, exception, err
}
53 changes: 26 additions & 27 deletions voice.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gotwilio

import (
"encoding/xml"
"encoding/json"
"net/http"
"net/url"
"strconv"
Expand All @@ -25,30 +25,29 @@ type CallbackParameters struct {

// VoiceResponse contains the details about successful voice calls.
type VoiceResponse struct {
XMLName xml.Name `xml:"TwilioResponse"`
Sid string `xml:"Call>Sid"`
DateCreated string `xml:"Call>DateCreated"`
DateUpdated string `xml:"Call>DateUpdated"`
ParentCallSid string `xml:"Call>ParentCallSid"`
AccountSid string `xml:"Call>AccountSid"`
To string `xml:"Call>To"`
ToFormatted string `xml:"Call>ToFormatted"`
From string `xml:"Call>From"`
FromFormatted string `xml:"Call>FromFormatted"`
PhoneNumberSid string `xml:"Call>PhoneNumberSid"`
Status string `xml:"Call>Status"`
StartTime string `xml:"Call>StartTime"`
EndTime string `xml:"Call>EndTime"`
Duration int `xml:"Call>Duration"`
Price float32 `xml:"Call>Price"`
Direction string `xml:"Call>Direction"`
AnsweredBy string `xml:"Call>AnsweredBy"`
ApiVersion string `xml:"Call>ApiVersion"`
Annotation string `xml:"Call>Annotation"`
ForwardedFrom string `xml:"Call>ForwardedFrom"`
GroupSid string `xml:"Call>GroupSid"`
CallerName string `xml:"Call>CallerName"`
Uri string `xml:"Call>Uri"`
Sid string `json:"sid"`
DateCreated string `json:"date_created"`
DateUpdated string `json:"date_updated"`
ParentCallSid string `json:"parent_call_sid"`
AccountSid string `json:"account_sid"`
To string `json:"to"`
ToFormatted string `json:"to_formatted"`
From string `json:"from"`
FromFormatted string `json:"from_formatted"`
PhoneNumberSid string `json:"phone_number_sid"`
Status string `json:"status"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Duration int `json:"duration"`
Price *float32 `json:"price,omitempty"`
Direction string `json:"direction"`
AnsweredBy string `json:"answered_by"`
ApiVersion string `json:"api_version"`
Annotation string `json:"annotation"`
ForwardedFrom string `json:"forwarded_from"`
GroupSid string `json:"group_sid"`
CallerName string `json:"caller_name"`
Uri string `json:"uri"`
// TODO: handle SubresourceUris
}

Expand Down Expand Up @@ -119,15 +118,15 @@ func (twilio *Twilio) CallWithApplicationCallbacks(from, to, applicationSid stri
func (twilio *Twilio) voicePost(formValues url.Values) (*VoiceResponse, *Exception, error) {
var voiceResponse *VoiceResponse
var exception *Exception
twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/Calls"
twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/Calls.json"

res, err := twilio.post(formValues, twilioUrl)
if err != nil {
return voiceResponse, exception, err
}
defer res.Body.Close()

decoder := xml.NewDecoder(res.Body)
decoder := json.NewDecoder(res.Body)

if res.StatusCode != http.StatusCreated {
exception = new(Exception)
Expand Down

0 comments on commit a0ce1cb

Please sign in to comment.